|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Saturday, June 08, 2013 5:41 AM
Points: 14,
Visits: 80
|
|
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 For more information contact masoudk1990@yahoo.com
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 2:32 PM
Points: 11,789,
Visits: 28,060
|
|
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
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Saturday, June 08, 2013 5:41 AM
Points: 14,
Visits: 80
|
|
Thank you very much
Computer Enterprise Masoud Keshavarz For more information contact masoudk1990@yahoo.com
|
|
|
|