0

Since a variable can only store a single value.This code throws an error.

Objective: If a certain value isnt there in the other table. Add that value in the table plus other columns.

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Code is here as follows:

declare @something as varchar(1000)
set @something = (select col1 from d)
while @something not in (select distinct col1 from c)  
begin
   insert into c
   (x,y,z)
   values
   (@something, y, z)
   break
end

UPDATE: Found a solution, used an if statement instead.

4
  • Why do you have a BREAK in your WHILE that isn't conditional? This would only loop once, making the WHILE pointless (not that you need a WHILE anyway)l. Commented Nov 27, 2019 at 11:27
  • SET will accept and assign a scalar (single) value from a query. While SELECT could accept multiple returned values. thats...why yor are getting this error Commented Nov 27, 2019 at 11:32
  • Yes I figured that out. Commented Nov 27, 2019 at 11:40
  • @@Swarley i also put my answer...you can check out... Commented Nov 27, 2019 at 11:43

4 Answers 4

2

Remarks:

  1. Your example is incorrect - you do not give information on y, z from where they are inserted

  2. you refer to col1 in

    while @something not in (select distinct col1 from c)
    but then refer to this column as x in

insert into c    (x,y,z)

the reason for error is set @something = (select col1 from d) you can still use that one value if it is ok for you by changing the assignment to this

set @something = (select top 1 col1 from d)

Solution In general, your query is incorrect as you using row-by-row approach instead of set based. Which is a way to go in SQL.

insert into c(col1)--,y,z)
select distinct col1 --, y, z 
from d
where not exists (select col1 from c = d.col1)

if you have duplicates in d for col1 then you need to handle then with distinct. Anyway, the actual result/insert depends on your data and what is in other columns (y,z ...)

Sign up to request clarification or add additional context in comments.

1 Comment

@Swarley . . . The insert . . . select is the right way to do this.
2

You're getting Error Just because of SET accept only single value...

SET will accept and assign a scalar (single) value from a query. While SELECT could accept multiple returned values. thats...why yor are getting this error

CREATE TABLE #Temp1
(Col VARCHAR(100))

insert into #Temp1(col1) select  col1 from c

INSERT INTO c(x,y,z) SELECT Col,y,z FROM #Temp1 WHERE Col <>(select distinct col1 from c) ---Here y,z you can pick from your self...it's not from temp table...

DROP TABLE #Temp1

Try this query...

1 Comment

yup I created a temp table for the workaround.Thanks :)
1

Try to use a table variable instead:

DECLARE @MyTable TABLE (
-- specify the desired columns along with the data types
)

Then, use your script.

1 Comment

OP doesn't need a WHILE either; demonstrating a simple INSERT multi row statement would be great for them.
0

Please try this query ,I hope it will work fine for you.

DECLARE @Something TABLE (Col VARCHAR(1000))
INSERT INTO @Something
SELECT col1
FROM d
WHERE Col NOT IN (
        SELECT DISTINCT col1
        FROM c
        )
BEGIN
    INSERT INTO c (x,y,z)
    VALUES
    SELECT Col,x,y
    FROM @Something
END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.