Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Alter Table not immediate inside stored procedure

Author  Topic 

Dev Null
Starting Member

39 Posts

Posted - 2005-05-17 : 21:56:38
I have to write a stored procedure which, among other things, creates a primary key constraint on a table.

I know; thats weird. (If you care, its because the table schema is built - and occasionally re-built - by an external application which I can't control.)

I can create my primary key with the statements:
ALTER TABLE AVAILABILITY 
ALTER COLUMN TIMESTAMP DATETIME NOT NULL
GO
ALTER TABLE AVAILABILITY
ADD CONSTRAINT AVAILABILITY_PK PRIMARY KEY CLUSTERED ( TIMESTAMP )
GO

but if I drop that into a stored procedure, I have to remove the "GO"s, and the add constraint line then complains that it can't create a primary key on a nullable column; presumably because the addition of the NOT NULL constraint hasn't yet taken effect. Is there a way to force it to? I've tried variations on using EXEC and transactions, but to no avail...

Thanks for any advice,

- rob.

mr_mist
Grunnio

1870 Posts

Posted - 2005-05-18 : 05:30:08
You'd need to do the ALTERs in seperate procedures and execute them serially from another procedure.

-------
Moo. :)
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2005-05-18 : 09:38:41
Or use dynamic sql for all table access
see
http://www.nigelrivett.net/SQLTsql/AccessTempTablesAcrossSPs.html
It's the same concept.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -