March 6, 2013 at 6:15 am
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.
March 6, 2013 at 6:36 am
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
March 6, 2013 at 6:42 am
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.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply