sql-server-2008t-sqlinline-code

Is Dynamic SQL The Only Way To Run This?


Each store has its own database (which I agree is bad practice), and I need to get data from all of those individual databases that meet certain criteria. I have come up with this, thus far, but I get an error of

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

I think this will show what I need to achieve, is dynamic SQL the best way to perform this task? If so, what do I do to remove my error. If not, what idea/thoughts/re-writes can more advanced SQL users provide.

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

Solution

  • Instead of while etc. you can just do it with one update statement.

     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'
    

    The full version is below:

    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'