Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a column and update it in same stored procedure in SQL Server 2008

if I have a stored procedure say

CREATE PROCURE w AS

ALTER TABLE t ADD x char(1)

UPDATE t set x =1

Even when it lets me create that stored procedure (if I create it when x exists), when it runs, there is an error on the UPDATE statement because column x doesn't exist.

What's the conventional way to deal with this, it must come up all the time? I can work around it by putting the UPDATE inside EXEC, is there another/better way?

Thanks

like image 215
TortTupper Avatar asked Jun 09 '10 11:06

TortTupper


1 Answers

ALTER TABLE in the context of 1st TRANSACTION and UPDATE in the context of 2nd:

CREATE PROCEDURE w
AS
   BEGIN TRAN
      ALTER TABLE ..
   COMMIT

   BEGIN TRAN
      UPDATE ..
   COMMIT
END
like image 91
abatishchev Avatar answered Sep 25 '22 13:09

abatishchev