Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Executing multiple queries stored in multiple rows

$
0
0

Hi, 

I have a question. I have a table that contains import scripts for my database. Below is the set up

IDTable DateScript
1TableA NULLINSERT INTO [dbo].[TableA] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
2TableB NULLINSERT INTO [dbo].[TableB] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
3TableC NULLINSERT INTO [dbo].[TableC] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
4TableD NULLINSERT INTO [dbo].[TableD] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
5TableE 5/28/2013INSERT INTO [dbo].[TableE] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
6TableE 5/29/2013INSERT INTO [dbo].[TableE] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
7TableE 5/30/2013INSERT INTO [dbo].[TableE] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
8TableE 5/31/2013INSERT INTO [dbo].[TableE] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
9TableE 6/3/2013INSERT INTO [dbo].[TableE] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
10TableE 6/4/2013INSERT INTO [dbo].[TableE] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
11TableF NULLINSERT INTO [dbo].[TableF] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
12TableF NULLINSERT INTO [dbo].[TableF] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
13TableF NULLINSERT INTO [dbo].[TableF] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…
14TableF NULLINSERT INTO [dbo].[TableF] SELECT * FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0'…

I wrote 4 queries to execute the scripts for table A to D as following:

DECLARE @A varchar(500)
SET @A = (SELECT Script FROM TABLE WHERE [Table] = 'TABLE A')
EXECUTE (@A)


DECLARE @B varchar(500)
SET @B = (SELECT Script FROM TABLE WHERE [Table] = 'TABLE B')
EXECUTE (@B)

DECLARE @C varchar(500)
SET @C = (SELECT Script FROM TABLE WHERE [Table] = 'TABLE C')
EXECUTE (@C)

DECLARE @D varchar(500)
SET @D = (SELECT Script FROM TABLE WHERE [Table] = 'TABLE D')
EXECUTE (@D)

Now I also wish to execute the script from Table E, but only for the Month of May so I decided to write this query

DECLARE @E varchar(500)
SET @E = (SELECT Script FROM TABLE WHERE [Table] = 'TABLE E' AND [Date] BETWEEN '2013-05-01' AND '2013-05-31)
EXECUTE (@E)

I guess this query can't execute because my statement selects more than one script. Now I have data going back to two years and I don't want to write close to 700 statements. Is there a way around it?

Thank you very much in advance!

Cheers


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>