Question

My database trigger takes a date from a column and adds 60 days to it and stores it into another column.

And it does as expected when I execute the code in query window and it throws the following error.

Msg 512, Level 16, State 1, Line 4
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

My code:

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

Result when i execute

But keeps giving the error with the result.

Msg 512, Level 16, State 1, Line 4
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Was it helpful?

Solution 3

Well Finally i sorted it out, Silly me :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

I juz had to put WHERE in this line

SELECT @NextDate = (SELECT DATEADD(day,10,Today) FROM Test WHERE ID = @newint)

OTHER TIPS

INSERT INTO @NextDate SELECT DATEADD(day,10,Today) FROM Test

In Your Query

SELECT @NextDate = (SELECT DATEADD(day,10,Today) FROM Test)

the sub query returning more than one value and you cant assign the multiple values to one variable. this is causing the problem in you query.

As Dominic Deepan.d Suggested use the where condition

SELECT @NextDate = (SELECT DATEADD(day,10,Today) FROM Test WHERE ID = @newint)

Or else try the same in another way

 SELECT  @NextDate = DATEADD(day,10,Today) FROM Test WHERE ID = @newint
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top