Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I have a Table in SQL named [TRN_ADVISOR] WHERE SOME DATA OF DIFFERENT BRANCHID IS PRESENT AND I WANT TO INSERT THOSE RECORDS INTO ANOTHER TABLE XYZ.BUT IT THROWS AN ERROR :

Msg 116, Level 16, State 1, Line 3
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Msg 213, Level 16, State 1, Line 1
Column name or number of supplied values does not match table definition.

THE STRUCTURE OF THE TABLE AND THE INSERT QUERY IS GIVEN BELOW:

SQL
CREATE TABLE [dbo].[XYZ](
    [SEQID] [int] NULL,
    [COMPANYID] [int] NULL,
    [BRANCHID] [int] NULL,
    [COUNT] [int] NULL
) ON [PRIMARY]


INSERT INTO XYZ VALUES (SELECT 111,1,[BRANCHID],COUNT(*)
  FROM [TRN_ADVISOR]
  GROUP BY [BRANCHID])



PLEASE HELP ASAP.

THANKS AND REGARDS
DEEPAK
Posted
Comments
jim lahey 19-Sep-11 8:59am    
Don't shout so much!

That means that the column name you're trying to use doesn't exist in the TRN_ADVISOR table. BTW, why are you doing a COUNT(*) in that insert statement?
 
Share this answer
 
If you're selecting from another table, you don't need the VALUES keyword, you can just INSERT INTO \ SELECT FROM as follows...

SQL
INSERT INTO XYZ (SEQID, COMPANYID, BRANCHID, COUNT)
SELECT 111,1,[BRANCHID],COUNT(*)
FROM [TRN_ADVISOR]
GROUP BY [BRANCHID]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900