Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 tsql if else statement

Author  Topic 

desikankannan
Posting Yak Master

152 Posts

Posted - 2011-06-30 : 14:00:16
Hi,
i have 4 tables
consignor
consignee
other
invoice
in invoice, i have 7 fields invoiceid,invoiceno,consignorid,conosigneeid,billingid,billtype
billingid is get from anyone of the table consignor,consignee,other

what is want is that ,i want to display,billid,billname how can i display billname
because billname from 3 table(consignor,consignee,other)
billid, billno,billname,in my invoice table i have field called billtype, if billtype = 'E' then its from consignee table if billtype = 'R' then its from consignor table ,if billtype ='O' then its from the other table


looking for suggestions

Desikankannan

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-06-30 : 14:19:57
select
inv.billingid,
COALESCE(t1.billname,t2.billname,t3.billname,'no bill found') as billname
from invoice inv
left join consignor t1 on t1.billingid=inv.billingid and inv.billtype='R'
left join consignee t2 on t2.billingid=inv.billingid and inv.billtype='E'
left join other t3 on t3.billingid=inv.billingid and inv.billtype='O'


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

latch
Yak Posting Veteran

62 Posts

Posted - 2011-06-30 : 14:37:58
I never tried it but it may be of use:


DECLARE @TableName nvarchar(100), @SQLString nvarchar(4000)

SET @TableName = ''
SET @SQLString ='SELECT billname FROM '
SELECT E.EmployeeID,
CASE E.EmployeeType
WHEN 1 THEN
@TableName = 'Tbl1'
WHEN 2 THEN
@TableName = 'Tbl2'
WHEN 3 THEN
@TableName = 'Tbl3'
END
FROM EMPLOYEE E

IF ISNULL(@TableName,'') <> ''

BEGIN

SET @SQLString = @SQLString + ' ' + @TableName

END

IF @SQLString IS NOT NULL

EXEC sp_executesql @SQLString


source:

http://www.bigresource.com/Tracker/Track-ms_sql-4DxzhNbH/
Go to Top of Page
   

- Advertisement -