5

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

4

4 に答える 4

4

ALTER TABLE1st TRANSACTIONUPDATEのコンテキストおよび 2nd のコンテキスト:

CREATE PROCEDURE w
AS
   BEGIN TRAN
      ALTER TABLE ..
   COMMIT

   BEGIN TRAN
      UPDATE ..
   COMMIT
END
于 2010-06-09T11:12:50.620 に答える
2

このように列を追加してからその値を更新するのではなく、デフォルト値を持つ列を追加できます

CREATE PROCEDURE w AS 

ALTER TABLE t ADD x char(1) NOT NULL CONSTRAINT abc DEFAULT 1
于 2010-06-09T11:12:34.353 に答える