Error in insert query | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Error in insert query

What is wrong in this query insert into db1.dbo.tbl1
select top 1 * from db2.dbo.tbl1 left join db1.dbo.tbl3 on db1.dbo.tbl3.id=blog.dbo.tbl1.id
Insert Error: Column name or number of supplied values does not match table definition.
The error is saying the column return from the select statement does not matched the columns in the table tbl1. Don’t use select *. Always specify the column name explicitly insert into db1.dbo.tbl1 (col1, col2, col3)
select top 1 cola, colb, colc
from db2.dbo.tbl1 left join db1.dbo.tbl3 on db1.dbo.tbl3.id=blog.dbo.tbl1.id
KH
quote:Originally posted by Hover What is wrong in this query insert into db1.dbo.tbl1
select top 1 * from db2.dbo.tbl1 left join db1.dbo.tbl3 on db1.dbo.tbl3.id=blog.dbo.tbl1.id
Insert Error: Column name or number of supplied values does not match table definition.
As the error message clearly says: Column name or number of supplied values does not match table definition.
Rule of thumbs: Always use column list when selecting/inserting instead of *. Thanks, Name
———
Dilli Grg (1 row(s) affected)
]]>