动态 SQL 是 运行 的唯一方法吗?

Is Dynamic SQL The Only Way To Run This?

每个商店都有自己的数据库(我同意这是不好的做法),我需要从所有满足特定条件的单独数据库中获取数据。到目前为止,我已经想到了这个,但是我得到了

的错误

Msg 102, Level 15, State 1, Line 81
Incorrect syntax near ')'.

我认为这将显示我需要实现的目标,动态 SQL 是执行此任务的最佳方式吗?如果是这样,我该怎么做才能消除我的错误。如果没有,更高级的SQL用户可以提供什么idea/thoughts/re-writes。

Create Table #storeinformation
(
    storeID int IDENTITY(1,1) PRIMARY KEY
    ,storename varchar(150)
    ,storetype varchar(150)
)
Insert INTO #storeinformation Values ('A', 'Corner'), ('B', 'Strip Mall'), ('C', 'Corner'), ('D', 'Corner')
Create Table #storeA
(
    storeID varchar(100)
    ,employeeid int
    ,tobaccosales decimal(16,4)
    ,foodsales decimal(16,4)
    ,lotterysales decimal(16,4)
    ,gsmapproval varchar(10)
    ,BDSMapproval varchar(10)
    ,RSMapproval varchar(10)
)
Insert INTO #storeA VALUES ('A', 16, '14.23', '18.36', '18.22', NULL, NULL, NULL),('A', 43, '110.23', '181.36', '183.22', NULL, NULL, NULL),('A', 55, '124.23', '182.36', '183.22', 'Yes', 'Yes', 'Yes')
Create Table #storeB
(
    storeID varchar(100)
    ,employeeid int
    ,tobaccosales decimal(16,4)
    ,foodsales decimal(16,4)
    ,lotterysales decimal(16,4)
    ,gsmapproval varchar(10)
    ,BDSMapproval varchar(10)
    ,RSMapproval varchar(10)
)
Insert INTO #storeB VALUES ('B', 11, '14.23', '18.36', '18.22', 'Yes', NULL, NULL),('B', 13, '110.23', '181.36', '183.22', NULL, NULL, NULL),('B', 52, '124.23', '182.36', '183.22', NULL, 'Yes', NULL)
Create Table #storeC
(
    storeID varchar(100)
    ,employeeid int
    ,tobaccosales decimal(16,4)
    ,foodsales decimal(16,4)
    ,lotterysales decimal(16,4)
    ,gsmapproval varchar(10)
    ,BDSMapproval varchar(10)
    ,RSMapproval varchar(10)
)
Insert INTO #storeC VALUES ('C', 6, '14.23', '18.36', '18.22', NULL, NULL, 'Yes'),('C', 4, '110.23', '181.36', '183.22', NULL, NULL, NULL),('C', 5, '124.23', '182.36', '183.22', NULL, 'Yes', NULL)
Create Table #storeD
(
    storeID varchar(100) 
    ,employeeid int
    ,tobaccosales decimal(16,4)
    ,foodsales decimal(16,4)
    ,lotterysales decimal(16,4)
    ,gsmapproval varchar(10)
    ,BDSMapproval varchar(10)
    ,RSMapproval varchar(10)
)
Insert INTO #storeD VALUES ('D', '116', '14.23', '18.36', '18.22', 'Yes', 'Yes', 'Yes'),('D', '143', '10.23', '81.36', '18.22', 'Yes', NULL, 'Yes'),('D', '155', '12.23', '1.36', '183.22', 'Yes', NULL, NULL)

Create Table #FullOnFrontalData
(
    storename varchar(100)
    ,tobaccosales decimal(16,4)
    ,foodsales decimal(16,4)
    ,lotterysales decimal(16,4)
    ,evaluated varchar(10)
)

Insert Into #FullOnFrontalData (storename)
Select storename 
FROM #storeinformation
Where storetype = 'Corner'

Declare @storecount int, @dbname varchar(100)

Set @storecount = (Select Count(*) from #FullOnFrontalData)

while @storecount > 0
BEGIN

    Set @dbname = (Select Top 1 storename from #FullOnFrontalData where evaluated is null)

    EXEC('Update #FullOnFrontalData
          Set tobaccosales = (Select SUM(tobaccosales) FROM '+@dbname))
    EXEC('Update #FullOnFrontalData
         foodsales = (Select SUM(foodsales) FROM '+@dbname))
    EXEC('Update #FullOnFrontalData
          lotterysales = (Select SUM(lotterysales) FROM '+@dbname))

    update #FullOnFrontalData
    set evaluated = 1
    where storename = @dbname               

    set @storecount = @storecount - 1

END

Select * from #FullOnFrontalData

而不是 while 等,你可以只用一个更新语句来完成。

 insert into #FullOnFrontalData 
     select Sinf.storeid, tab, fod, lott, 1 from (
     select storeid, sum(tobaccosales) tab ,  sum(foodsales) fod, sum(lotterysales)  lott
     from #storeA 
     group by  storeid
     union all 
      select storeid, sum(tobaccosales) ,  sum(foodsales), sum(lotterysales) 
     from #storeB 
      group by  storeid
      union all 
      select storeid, sum(tobaccosales) ,  sum(foodsales), sum(lotterysales) 
     from #storeC
      group by  storeid
      union all 
      select storeid, sum(tobaccosales) ,  sum(foodsales), sum(lotterysales) 
     from #storeD 
      group by  storeid ) SInv
      join  #storeinformation SInf
      on Sinv.storeid = Sinf.storename and
        Sinf.storetype = 'Corner'

完整版如下:

Create Table #storeinformation
(
    storeID int IDENTITY(1,1) PRIMARY KEY
    ,storename varchar(150)
    ,storetype varchar(150)
)
Insert INTO #storeinformation Values ('A', 'Corner'), ('B', 'Strip Mall'), ('C', 'Corner'), ('D', 'Corner')
Create Table #storeA
(
    storeID varchar(100)
    ,employeeid int
    ,tobaccosales decimal(16,4)
    ,foodsales decimal(16,4)
    ,lotterysales decimal(16,4)
    ,gsmapproval varchar(10)
    ,BDSMapproval varchar(10)
    ,RSMapproval varchar(10)
)
Insert INTO #storeA VALUES ('A', 16, '14.23', '18.36', '18.22', NULL, NULL, NULL),('A', 43, '110.23', '181.36', '183.22', NULL, NULL, NULL),('A', 55, '124.23', '182.36', '183.22', 'Yes', 'Yes', 'Yes')
Create Table #storeB
(
    storeID varchar(100)
    ,employeeid int
    ,tobaccosales decimal(16,4)
    ,foodsales decimal(16,4)
    ,lotterysales decimal(16,4)
    ,gsmapproval varchar(10)
    ,BDSMapproval varchar(10)
    ,RSMapproval varchar(10)
)
Insert INTO #storeB VALUES ('B', 11, '14.23', '18.36', '18.22', 'Yes', NULL, NULL),('B', 13, '110.23', '181.36', '183.22', NULL, NULL, NULL),('B', 52, '124.23', '182.36', '183.22', NULL, 'Yes', NULL)
Create Table #storeC
(
    storeID varchar(100)
    ,employeeid int
    ,tobaccosales decimal(16,4)
    ,foodsales decimal(16,4)
    ,lotterysales decimal(16,4)
    ,gsmapproval varchar(10)
    ,BDSMapproval varchar(10)
    ,RSMapproval varchar(10)
)
Insert INTO #storeC VALUES ('C', 6, '14.23', '18.36', '18.22', NULL, NULL, 'Yes'),('C', 4, '110.23', '181.36', '183.22', NULL, NULL, NULL),('C', 5, '124.23', '182.36', '183.22', NULL, 'Yes', NULL)
Create Table #storeD
(
    storeID varchar(100) 
    ,employeeid int
    ,tobaccosales decimal(16,4)
    ,foodsales decimal(16,4)
    ,lotterysales decimal(16,4)
    ,gsmapproval varchar(10)
    ,BDSMapproval varchar(10)
    ,RSMapproval varchar(10)
)
Insert INTO #storeD VALUES ('D', '116', '14.23', '18.36', '18.22', 'Yes', 'Yes', 'Yes'),('D', '143', '10.23', '81.36', '18.22', 'Yes', NULL, 'Yes'),('D', '155', '12.23', '1.36', '183.22', 'Yes', NULL, NULL)

Create Table #FullOnFrontalData
(
    storename varchar(100)
    ,tobaccosales decimal(16,4)
    ,foodsales decimal(16,4)
    ,lotterysales decimal(16,4)
    ,evaluated varchar(10)
)


insert into #FullOnFrontalData 
 select Sinf.storeid, tab, fod, lott, 1 from (
 select storeid, sum(tobaccosales) tab ,  sum(foodsales) fod, sum(lotterysales)  lott
 from #storeA 
 group by  storeid
 union all 
  select storeid, sum(tobaccosales) ,  sum(foodsales), sum(lotterysales) 
 from #storeB 
  group by  storeid
  union all 
  select storeid, sum(tobaccosales) ,  sum(foodsales), sum(lotterysales) 
 from #storeC
  group by  storeid
  union all 
  select storeid, sum(tobaccosales) ,  sum(foodsales), sum(lotterysales) 
 from #storeD 
  group by  storeid ) SInv
  join  #storeinformation SInf
  on Sinv.storeid = Sinf.storename and
    Sinf.storetype = 'Corner'