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

时间:2016-01-12 03:32:04

标签: sql-server-2008 tsql inline-code

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

  

Msg 102,Level 15,State 1,Line 81
  ')'附近的语法不正确。

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

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

1 个答案:

答案 0 :(得分:0)

而不是等等,你可以用一个更新语句来做。

 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'