SQL Server 2008 R2 - All Script Into One

Hi Experts,

This is more of a knowledge based question.

I have about 5 scripts that I would like to save as only one script, can someone help me?

Thanks for your help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If all five scripts in the same directory you can use a dos command:
copy *.sql combined.sql

Open in new window

Scott PletcherSenior DBACommented:
That depends.  For example, do the scripts DECLARE the same variables or temp tables?  That would be an issue.  The same label name would also be an issue (unless new batches are started).  Etc.
Vitor MontalvãoMSSQL Senior EngineerCommented:
If you want to run all the scripts in a preferred order then I'll recommend you to turn on the SQLCMD mode in SSMS and use the :r command to call the scripts.
You can even save that script and call it from command prompt using the SQLCMD utility:
SQLCMD -E -dDatabaseName -ic:\Scripts\MainScript.sql
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Amour22015Author Commented:
I should have mentioned that all I want to do is SAVE all scripts into one.
Vitor MontalvãoMSSQL Senior EngineerCommented:
I think everyone understood what you want and Chauu gave you a good solution to achieve that but ScottPletcher alerted you for some issues that can happen with that solution so I gave you another option to accomplish that by running all scripts each one within their own context so you can avoid the issues that you was alerted to.
I would be grateful to have this kind of support from experts.
Amour22015Author Commented:
I am grateful to have support from experts

I am knew to all this.

I thought there was an easy solution.

One of my co-workers did it so easy the last time but I forgot how to do this and don't want to ask him again.  It seems that what he did was not so complicated.

Lets make sure we are talking about the same thing?

When I save One script I do:
File>Save All
but that saves only one at a time.

I want to save all the scripts I have up in the window(s) into one so co-worker can take that script and move it into live.

I tried Chauu  and got error "Incorrect syntax near '*'.

Please help and thanks
Vitor MontalvãoMSSQL Senior EngineerCommented:
Chaau's solution is to run in command prompt (DOS).
If you want to do it in SSMS then you need to open a new query window and go to each script and copy all lines to the new query window and then save the new query as a new script. It's a copy/paste solution where you need to do it a script at a time pasting the contents to the end of the new script.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amour22015Author Commented:
I noticed that under "copy" it says: could not find stored procedure 'Copy'

Also I was told to make sure and take out all the "USE" out of each SPR.

Thanks for any help
Vitor MontalvãoMSSQL Senior EngineerCommented:
Once again:
"Chaau's solution is to run in command prompt (DOS)." not in SQL Server Management Studio (SSMS).
Amour22015Author Commented:
This is what I am looking for but I don't remember my co-worker doing that.  I will see when he goes to bring that script into live.  Thanks for helping me.  Wanted to let you all know that I am grateful to have support from experts and am not trying to disagree only trying to understand.
Amour22015Author Commented:
Once again:
"Chaau's solution is to run in command prompt (DOS)." not in SQL Server Management Studio (SSMS).

I started working in computers in 1984 so I know about the DOS prompt but seems like technology has gone past me lately.  What I learn today is gone tomorrow, we should have all been paid like doctors?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.