Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


On Error Do Next Query


On Error Do Next Query

Author
Message
masoudk1990
masoudk1990
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
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.
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14967 Visits: 38985
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!

masoudk1990
masoudk1990
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search