Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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, March 6, 2016 5:38 AM
Points: 66, Visits: 299
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: Today @ 1:58 PM
Points: 14,412, Visits: 37,726
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
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, March 6, 2016 5:38 AM
Points: 66, Visits: 299
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