我的数据库触发器从列中获取日期,并为其添加60天,并将其存储到另一列中。
当我在查询窗口中执行代码并且它抛出以下错误时,它会按预期执行。
Msg 512,Level 16,State 1,Line 4
子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。
我的代码:
DECLARE @NextDate date
SELECT @NextDate = (SELECT DATEADD(day, 10, Today) FROM Test)
INSERT INTO Test (Notes, Today)
VALUES ('Testing in Query2', GETDATE())
DECLARE @newint int
SET @newint = SCOPE_IDENTITY()
UPDATE Test
SET Someday = @NextDate
WHERE ID = @newint
RESULT
但不断给出错误结果。
Msg 512,Level 16,State 1,Line 4
子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。
答案 0 :(得分:1)
INSERT INTO @NextDate SELECT DATEADD(day,10,Today) FROM Test
答案 1 :(得分:1)
在您的查询中
SELECT @NextDate = (SELECT DATEADD(day,10,Today) FROM Test)
子查询返回多个值,并且您无法将多个值分配给一个变量。这导致您查询中的问题。
作为Dominic Deepan.d建议使用where条件
SELECT @NextDate = (SELECT DATEADD(day,10,Today) FROM Test WHERE ID = @newint)
或者以其他方式尝试相同
SELECT @NextDate = DATEADD(day,10,Today) FROM Test WHERE ID = @newint
答案 2 :(得分:0)
好吧最后我把它整理出来,傻傻的我:D
INSERT INTO Test(Notes,Today)
values ('Testing in Query3',GETDATE())
DECLARE @newint int
SET @newint = SCOPE_IDENTITY()
DECLARE @NextDate date
SELECT @NextDate = (SELECT DATEADD(day,10,Today) FROM Test WHERE ID = @newint)
UPDATE Test
SET Someday = @NextDate
WHERE ID = @newint
GO
我juz必须把WHERE放在这一行
SELECT @NextDate = (SELECT DATEADD(day,10,Today) FROM Test WHERE ID = @newint)