Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Subquery returns multiple values

Author  Topic 

chaaru_akilan
Starting Member

16 Posts

Posted - 2012-10-22 : 07:08:23
My Subquery will return multiple values.

Name
ABC
DEF

I need to insert this into another table as separate rows, like

ID Name
1 ABC
2 DEF

I tried to give the subquery like

INSERT into Table
(
col1 INT,
col2 VARCHAR
)
VALUES
(
ID,
(Subquery returning ABC,DEF)
)

But it will give errror indicating subquery returns multiple values.

Thanks in advance.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-22 : 07:20:47
You have to change your query so that it does not return multiple values. Where does ID come from? May be you can change your subquery to include ID as well and use it like this:
INSERT INTO [Table]
(col1, col2)
SELECT ID,NameFROM SomeQuery
The other option is to insert only one value, but I don't think that is what you want:
INSERT into Table
(
col1 INT,
col2 VARCHAR
)
VALUES
(
ID,
(SELECT TOP 1 ABC,DEF)
)
Go to Top of Page

chaaru_akilan
Starting Member

16 Posts

Posted - 2012-10-22 : 08:53:40
I changed my Subquery and did modifications similar to the first query of urs and it worked. Thank you
Go to Top of Page
   

- Advertisement -