[英]Case Statement in SQL Server
I am learning SQL case
statements and have the following stored procedure
. 我正在学习SQL
case
语句,并具有以下stored procedure
。
Select PT.[ID] 'TransactionID', PT.BatchNumber, PT.SequenceNumber, PT.TransactionDate,
PT.TerminalID, PT.TotalAmount, PT.TransactionTypeID, TT.TransactionType,
PT.PAN 'EmbossLine',PT.PreBalanceAmount, PT.PostBalanceAmount, RefTxnID, SettlementDate,PaidCash, CreditAmount, DiscountAmount,
RefPAN, Remarks, PT.Product,
case PT.Product when 1 then 'Taxi' end 'ProductName'
case PT.Product when 2 then 'Airport Lounge' end 'ProductName'
into #Temp
from POS_Transactions PT inner join TransactionType TT on TT.TransactionTypeID = PT.TransactionTypeID
where
PT.[ID] not in (Select distinct isnull(TransactionID,0) from Testcards)
and (PT.TransactionDate >= @DateFrom)
and (PT.TransactionDate < @DateTo)
and (PT.TransactionTypeID = @TransactionTypeID or @TransactionTypeID = -999)
select T.*, C.EmbossLine+' ' as 'EmbossLine', C.EmbossLine as 'EmbossLine1',
C.EmbossName, PM.MerchantID, PM.MerchantName1, C.AccountNumber, C.VehicleNumber
from #Temp T
inner join Card C on C.EmbossLine= T.EmbossLine
inner join Terminal on Terminal.TerminalID = T.TerminalID
inner join Merchant PM on PM.MerchantID = Terminal.MerchantID
where C.Status <>'E3'
and C.CardID not in (Select distinct isnull(CardID,0) from Testcards)
and (PM.MerchantID =@MerchantID or @MerchantID='-999')
and (C.EmbossLine like '%'+@EmbossLine+'%' or @EmbossLine like '-999')
and (C.EmbossName like '%'+@EmbossName+'%' or @EmbossName like '-999')
order by T.TransactionDate, MerchantName1, T.BatchNumber, T.SequenceNumber
drop table #Temp
When I create
it, command is executed succesfully. create
它时,命令会成功执行。 However when I call it, it throws following error 但是,当我调用它时,它会引发以下错误
Column names in each table must be unique.
每个表中的列名必须唯一。 Column name 'ProductName' in table '#Temp' is specified more than once.
表'#Temp'中的列名'ProductName'被多次指定。
I think I have problem in the syntax in these lines 我认为这些行的语法有问题
case PT.Product when 1 then 'Taxi' end 'ProductName'
case PT.Product when 2 then 'Airport Lounge' end 'ProductName'
Can someone identify? 有人可以识别吗?
Your line is indeed a problem. 您的电话确实是个问题。
I suspect you're after something like: 我怀疑您正在追寻类似的东西:
case PT.Product
when 1 then 'Taxi'
when 2 then 'Airport Lounge'
end 'ProductName'
In your syntax you make two different cases, resulting in two columns being selected, both called the same. 在您的语法中,您有两种不同的情况,导致选择了两列,两者均称为相同。
Above the case can return two different values into the one row. 上面的案例可以将两个不同的值返回到一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.