gpt4 book ai didi

sql - 如何在查询中使用 SQL 变量(SQL Server)?

转载 作者:行者123 更新时间:2023-12-05 00:27:32 28 4
gpt4 key购买 nike

我写了下面的 SQL 存储过程,它一直给我错误@pid = SELECT MAX(... 整个过程是:

Alter PROCEDURE insert_partyco
@pname varchar(200)
AS
BEGIN
DECLARE @pid varchar(200);

@pid = SELECT MAX(party_id)+1 FROM PARTY;
INSERT INTO party(party_id, name) VALUES(@pid, @pname)
SELECT SCOPE_IDENTITY() as PARTY_ID
END
GO

谁能告诉我我做错了什么?

最佳答案

Alter PROCEDURE insert_partyco
@pname varchar(200)
AS
BEGIN
DECLARE @pid varchar(200);

SELECT @pid = MAX(party_id)+1 FROM PARTY;
INSERT INTO party(party_id, name) VALUES(@pid, @pname)
SELECT SCOPE_IDENTITY() as PARTY_ID
END

这比使用 SELECTSET 有优势,因为您可以在一个语句中选择多个变量中的表达式:

SELECT @var1 = exp1, @var2 = expr2 ... etc

关于sql - 如何在查询中使用 SQL 变量(SQL Server)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19198501/

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