简体   繁体   中英

SQL INSERT - how to execute a list of queries automatically

I've never done this, so apologies if I'm being quite quite vague.

Scenario

I need to run a long series of INSERT SQL queries. This data is inserted in a table for being processed by a web service's client, ie the data is uploaded on a different server and the table gets cleared as the process progresses.

What I've tried

I have tried to add a delay to each Insert statement like so

WAITFOR DELAY '00:30:00'

INSERT INTO TargetTable (TableName1, Id, Type) SELECT 'tablename1', ID1 , 1 FROM tablename1

WAITFOR DELAY '00:30:00'

INSERT INTO TargetTable (TableName2, Id, Type) SELECT 'tablename2', ID2 , 1 FROM tablename2

But this has the disadvantage of assuming that a query will finish executing in 30 minutes, which may not be the case.

Question

I have run the queries manually in the past, but that's excruciatingly tedious. So I would like to write a program that does that for me. The program should:

  1. Run each query in the order given

  2. Wait to run the next query until the previous one has been processed, ie until the target table is clear .

I'm thinking of a script that I can copy into the command prompt console, SQL itself or whatever and run.

How do I go about this? Windows service application? Powershell function?

I would appreciate any pointers to get me started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM