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.
| Author |
Topic |
|
desikankannan
Posting Yak Master
152 Posts |
Posted - 2011-06-30 : 14:00:16
|
| Hi,i have 4 tablesconsignorconsigneeotherinvoicein invoice, i have 7 fields invoiceid,invoiceno,consignorid,conosigneeid,billingid,billtypebillingid is get from anyone of the table consignor,consignee,otherwhat is want is that ,i want to display,billid,billname how can i display billnamebecause 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 tablelooking for suggestionsDesikankannan |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-06-30 : 14:19:57
|
selectinv.billingid,COALESCE(t1.billname,t2.billname,t3.billname,'no bill found') as billnamefrom invoice invleft 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. |
 |
|
|
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' ENDFROM EMPLOYEE EIF ISNULL(@TableName,'') <> ''BEGINSET @SQLString = @SQLString + ' ' + @TableNameENDIF @SQLString IS NOT NULLEXEC sp_executesql @SQLStringsource:http://www.bigresource.com/Tracker/Track-ms_sql-4DxzhNbH/ |
 |
|
|
|
|
|
|
|