简体   繁体   中英

How to fix this error "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >="

CREATE PROCEDURE Task2_Create1397_1396
AS
DECLARE @Check INT
DECLARE @Sal INT

--SET @Check = (SELECT COUNT(Title)FROM Buy)
SET @Sal = (
        SELECT YEAR(Tarikh)
        FROM Buy
        )

IF (@Sal = 1398)
    INSERT INTO Buy
    SELECT Title
        ,Type1
        ,Tedat
        ,DATEADD(DAY, - 2, DATEADD(YEAR, - 1, Tarikh))
        ,Descrip
    FROM Buy

IF (@Sal = 1397)
    INSERT INTO Buy
    SELECT Title
        ,Type1
        ,Tedat
        ,DATEADD(DAY, - 2, DATEADD(YEAR, - 1, Tarikh))
        ,Descrip
    FROM Buy

The only place you have a subquery is:

SET @Sal = (SELECT YEAR(Tarikh) FROM Buy);

This code -- even if you limited it to one row -- makes no sense. This returns one row for each row in Buy . Which one do you want? You don't specify which year you want, so I suspect you want:

SET @Sal = (SELECT MAX(YEAR(Tarikh)) FROM Buy);

Next, it is highly unusual to set a parameter named @SAL to a "year" value -- that doesn't work in English, although it might work in another language. I'm just pointing this out.

The next portion of your code is overly complicated and poorly written:

  • The two IF blocks are the same (well, except for the THEN conditions).
  • You are using INSERT with no column list. That is barely acceptable in ad-hoc code. It should not be allowed in permanent code.
  • You can actually eliminate the IF entirely.

So, I would recommend:

INSERT INTO Buy (Title, Type1, Tedat, Tarikh, Descrip)
    SELECT Title, Type1, Tedat, DATEADD(DAY, - 2, DATEADD(YEAR, - 1, Tarikh)),
           Descrip
    FROM Buy
    WHERE @Sal IN (1398, 1397);

Your table doesn't appear to have a primary key. That would also be a problem, and I would suggest that you fix the data model as well.

Just for your technical error - you need to change this

SET @Sal = (SELECT YEAR(Tarikh) FROM Buy)

into

SET @Sal = (SELECT Top 1 YEAR(Tarikh) FROM Buy)

You can use here order by clause also in the above select statement.

Apart from this you need to check why it is returning more than one value in select while assigning this returning value into a variable.

You can restructure your if statement twice into one as shown below.

IF (@Sal = 1398)
BEGIN
    INSERT INTO Buy
    SELECT Title
        ,Type1
        ,Tedat
        ,DATEADD(DAY, - 2, DATEADD(YEAR, - 1, Tarikh))
        ,Descrip
    FROM Buy -- <add where condition here for if block>
END
ELSE IF (@Sal = 1397)
BEGIN
    INSERT INTO Buy
    SELECT Title
        ,Type1
        ,Tedat
        ,DATEADD(DAY, - 2, DATEADD(YEAR, - 1, Tarikh))
        ,Descrip
    FROM Buy -- <add where condition here for another if block>
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM