gpt4 book ai didi

sql-server - 在存储过程中添加一列

转载 作者:行者123 更新时间:2023-12-02 18:24:24 24 4
gpt4 key购买 nike

我可以使用一个存储过程来向表中添加新列并在后续处理该列吗?例如,我有以下存储过程:

...

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com