SQL problem, does t...
 

Subscribe now and choose from over 30 free gifts worth up to £49 - Plus get £25 to spend in our shop

[Closed] SQL problem, does this even exist?

10 Posts
6 Users
0 Reactions
73 Views
Posts: 0
Free Member
Topic starter
 

I have 4 scripts to run in SQL 2008 R2, easy enough, run each one after each other.

The question is, is it possible to run all 4 scripts together in one window but pausing to execute after each one?

Obviousley this may be science fiction, but it seems tottally plausable in my mind


 
Posted : 06/04/2012 4:42 pm
 mrmo
Posts: 10708
Free Member
 

when you say four scripts, are you writing them in SSMS and then running them? or are you importing files?

What i discovered this week was that if i write multiple scripts in one SQL file it works.


 
Posted : 06/04/2012 4:47 pm
Posts: 5300
Full Member
 

Not really familiar with SQL 2008, but generally you would just follow each script with a semi-colon; and each would be executed in sequence.


 
Posted : 06/04/2012 4:48 pm
Posts: 0
Free Member
Topic starter
 

we're writing scripts to set up databases in SQL for sofware we use, 4 databases, but one needs to execute before the next, but we would like it to be one long script rather than 4 scripts to be executed one after the other

so:

run script one > execute > run script two > execute and so on


 
Posted : 06/04/2012 4:51 pm
Posts: 0
Free Member
 mrmo
Posts: 10708
Free Member
 

as a newby with limited experience i would do the following.

cut and paste each of the four scripts into one file, do it in the order you need the databases to run, and then execute.

Did something similar last week creating some views.


 
Posted : 06/04/2012 4:58 pm
Posts: 349
Free Member
 

I don't see why you couldn't just stick them in the same file sequentially in the order that you want them to run, just whack "GO" between each script and it should definitely work... That's how we script out creating tables etc... where objects later in the script depend on ones earlier.


 
Posted : 06/04/2012 5:04 pm
Posts: 0
Free Member
Topic starter
 

when you say four scripts, are you writing them in SSMS and then running them? or are you importing files?

Yes

I don't see why you couldn't just stick them in the same file sequentially in the order that you want them to run, just whack "GO" between each script and it should definitely work... That's how we script out creating tables etc... where objects later in the script depend on ones earlier.

Thats what I thought, but aparantley I'm too thick to know this myself so I've been tasked to find out, so I'm taking the cheating method and asking the hive mind.......office politics, don't you love it


 
Posted : 06/04/2012 5:25 pm
Posts: 349
Free Member
 

I'm fairly sure that should work, if you're in doubt then can you test in a sandbox or something?


 
Posted : 06/04/2012 5:30 pm
 nbt
Posts: 12403
Full Member
 

yes that will work. e.g.

select * from table1
go
select * from table2
go
select * from table3
go
select * from table4
go

or even change dbs, e.g.

use Database1
go
select * from table1
go
use Database2
go
select * from table2
go
use Database3
go
select * from table3
go
use Database4
go
select * from table4
go


 
Posted : 06/04/2012 5:44 pm
 nbt
Posts: 12403
Full Member
 

if in doubt, stick a transaction round it, at the end check for errors and rollback if you find any, else commit


 
Posted : 06/04/2012 5:45 pm