question

liton avatar image
liton asked

combine scripts into one

I have 10 scripts all of them are similar except for the file name and table name. Each script does a bulk insert to 10 different tables from 10 different files. I want consolidate all the scripts to one. Is there a way to do this other than putting a GO at the end of each script? I've heard someone mentioning I can do dynamic sql but don't know how.
scriptdynamic
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Grant Fritchey avatar image
Grant Fritchey answered
The GO statement is for working with a single file within SQL Server Management Studio query window to break the queries into discrete batches. It's not how I would look at putting scripts together. If anything it breaks them apart. Without seeing the code, I can't tell if dynamic SQL is a good solution or not. The concept of a well constructed dynamic SQL statement is that you're building a string with the query inside of it. You can still use parameters as long as you use sp_executesql which means you can see plan reuse. Here's the [books online entry on sp_executesql][1]. For such a general question, that's the best answer I can come up with. [1]: http://msdn.microsoft.com/en-us/library/ms175170(v=SQL.105).aspx
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.