Reputation: 1259
I am learning SQL case
statements and have the following 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. 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.
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?
Upvotes: 2
Views: 118
Reputation: 4091
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.
Upvotes: 3