加入多个表?

时间:2013-12-19 12:13:08

标签: sql sql-server

declare  @table1 table (sno int,transferbirds int,noofbirds int,numofbirds int,mortality int)
insert into @table1 

select sno,Branch,isnull((select transferbirds from  K_RT_RetailsDetails  where branch in (PN.sno),0) as Totalbirds,    
isnull(select noofbirds from K_RT_WarehouseDetails where  branch in (PN.sno),0) as Meatbirds, 
isnull(select noofbirds from K_RT_MasterBirdsTransferDet where  frombranch in (PN.sno),0) as TransferBirds,
isnull(select mortality from K_RT_DailyEntry where branch in (PN.sno),0)as Mortality,
isnull(select noofbirds from K_RT_BirdsTransfer where branch in (PN.sno),0) as Tobranchtransferbirds
from K_RT_MasterRetailStores PN       

select * from @table1 

我收到错误,语法不正确,','。 1行,2行,3行,4行也..请帮助我

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

    declare  @table1 table (sno int,transferbirds int,noofbirds int,numofbirds int,mortality int)
insert into @table1 

select sno,Branch,
isnull((select transferbirds from  K_RT_RetailsDetails  where branch in (PN.sno)),0) as Totalbirds,    
isnull((select noofbirds from K_RT_WarehouseDetails where  branch in (PN.sno)),0) as Meatbirds, 
isnull((select noofbirds from K_RT_MasterBirdsTransferDet where  frombranch in (PN.sno)),0) as TransferBirds,
isnull((select mortality from K_RT_DailyEntry where branch in (PN.sno)),0)as Mortality,
isnull((select noofbirds from K_RT_BirdsTransfer where branch in (PN.sno)),0) as Tobranchtransferbirds
from K_RT_MasterRetailStores PN       

select * from @table1 

实际上内部的select语句用括号括起来