Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

On Error Do Next Query Expand / Collapse
Author
Message
Posted Wednesday, March 6, 2013 6:15 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Sunday, October 12, 2014 6:53 AM
Points: 64, Visits: 289
Hi.
I have a lot of independence queries. And I want to execute all of them in single script. Queries looks like below:


ALTER TABLE Table1
ADD
[Field1] nvarchar(4),
[Field2] nvarchar(2)

ALTER TABLE Table2
ADD CONSTRAINT Foo PRIMARY KEY ([ID])

ALTER TABLE Table3
ADD [Field1] nvarchar(50)


Look at second query. If we already have a primary key with name 'Foo' it throw error, and 3th query don't execute.

I want if one or more queries have problem they don't halt process and let remaining queries execute.

Thank you for help.


___________________________________
Computer Enterprise Masoud Keshavarz
I don't care about hell.
If I go there I've played enough Diablo to know how to fight my way out.
Post #1427340
Posted Wednesday, March 6, 2013 6:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:09 PM
Points: 12,905, Visits: 32,166
you could place GO statements between each command, but you still have to manually ignore the errors...makes it hard to determine which are "ignorable" errors and which are real.

instead, consider checking for the object before you create it:

IF NOT EXISTS(select 1 from sys.columns where name='Field1' and object_name(object_id) = 'Table1')
ALTER TABLE Table1 ADD [Field1] nvarchar(4);

IF NOT EXISTS(select 1 from sys.columns where name='Field2' and object_name(object_id) = 'Table1')
ALTER TABLE Table1 ADD [Field2] nvarchar(2);

IF NOT EXISTS(select * from sys.objects where name='Foo' and type_desc = 'PRIMARY_KEY_CONSTRAINT' and object_name(parent_object_id) = 'Table2')
ALTER TABLE Table2 ADD CONSTRAINT Foo PRIMARY KEY ([ID])

IF NOT EXISTS(select 1 from sys.columns where name='Field1' and object_name(object_id) = 'Table3')
ALTER TABLE Table1 ADD [Field1] nvarchar(50);



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1427348
Posted Wednesday, March 6, 2013 6:42 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Sunday, October 12, 2014 6:53 AM
Points: 64, Visits: 289
Thank you very much

___________________________________
Computer Enterprise Masoud Keshavarz
I don't care about hell.
If I go there I've played enough Diablo to know how to fight my way out.
Post #1427354
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse