I'm using a stored procedure to add fields to an existing table.
These fields must have check constraints and I need to use one T-SQL batch.
In Sql2000 Ok. In Sql2005, if table exists, I get error "invalid column xxxxx" in Add Constraint statement before new column creation.
the code is
Declare @.Setup bit
Set @.Setup = 1
if @.Setup = 1 Begin
--Alter Table MyTable Add MyField Numeric(1, 0) Not Null Default 3
Exec mySp_Add_Column 'MyTable', 'MyField', 'Numeric (1, 0) Not Null', '3'
If IsNull(ObjectProperty(Object_Id('xCK_MyTable_MyField'), 'IsConstraint'), 0) = 0
Alter Table MyTable Add Constraint xCK_MyTable_MyField Check (MyField >= 1 And MyField <= 3)
End Else Begin
-- drop column
End
GO
If MyTable does not exist and, naturally, I add it before of check constraints (using another Sp which add tables) ok.
If I add FK to new fields, ok.
Now I have to split batch in two parts as workaround...
Can anyone tell me if this is a bug or a "fix" for previous versions?
Many thanks,
Giulio
Interesting, it probably is an undocumented "fix" that exists pretty much because it is illogical to add a check constraint when something doesn't exist, so when they parse it (before they execute it) it fails.
I would personally just add the check constraint creation to the ADD column proc, if I were you. It would just require a parameter, and it would be easy enough. The parameter I would add would be @.checkCriteria. Then pass in 'MyField >= 1 and MyField <= 3)'
Then you can formulate the name (or at worst add another parameter as override) and build the statement in the proc. You can add the constraint when you are adding the column too:
create table test
(
testId int primary key
)
go
alter table test
add newColumn varchar(2000) not null
constraint ckTestNewColumn check (newColumn = 1)
|||Another workaround might be this:
If IsNull(ObjectProperty(Object_Id('xCK_MyTable_MyField'), 'IsConstraint'), 0) = 0
exec('
Alter Table MyTable Add Constraint xCK_MyTable_MyField Check (MyField >= 1 And MyField <= 3)')
Steve Kass
Drew University
www.stevekass.com
Thanks Louis and Steve.
I've just resolved problem. Since in Sql 2000 this problem did not exist (using same T-Sql code) I'd like to know if it's a bug...
Really, if I use Alter Table, instead of mySp_Add_Column to add new columns, I get error too... So if I can understand parser can't know what SP wants to do, why doesn't parser understand I'll add new column before of check constraints? Why can I add FKs on new fields while I can't add CKs?
It seems a parser bug...
Bye,
Giulio
|||Really? When I execute:
create table test
(
testId int primary key
)
alter table test
add newColumn varchar(2000) not null
constraint ckTestNewColumn check (newColumn = 1)
in a single batch, it works just fine. What error are you getting?
|||ok, in this way it works...
Only when I add a column to an existing table and, after, I add a check constraint, all in one batch, I get error...
And only after I installed SqlSrv 2k5... on SqlSrv 2k it works fine...
No comments:
Post a Comment