我可以使用一个存储过程来向表中添加新列并在后续处理该列吗?例如,我有以下存储过程:
...
alter table tb1
add col1 varchar(1) null
insert into tb1(col1)
values ('Y')
我收到一个错误提示
col1 is invalid.
最佳答案
尝试使用默认值“Y”创建表,而不是随后插入值。
alter table tb1 add col1 varchar(1) not null DEFAULT ('Y')
根据 GO
文档,您需要在两行之间添加 GO
来创建表格:
SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of SQL Server.
但是,存储过程中不能有 GO
语句。
编辑
或者,您可以使用 EXEC
语句来执行代码:
EXEC ('alter table tb1 add col1 varchar(1) null')
EXEC ('update tb1 set col1 = ''Y''')
关于sql-server - 在存储过程中添加一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12358412/