Friday, February 08, 2013

Error: "No column name was specified for column 1 of 'SOURCE'" in SQL Server Merge statementwhen I was working with Merge statement with SQL Server 2008 then while writing this I got this error and after couple of min i got the issues

Declare

@Id Int= 10,

@Name Varchar(20)='Kesharwani',

@Salary Int= 30000

MERGEINTO [Temp] asTARGET

USING (SELECT @Id,@Name,@Salary)AS

SOURCEONTARGET.empId =SOURCE.empId

WHENMATCHEDTHEN

UPDATESET Name=SOURCE.Name, Salary =SOURCE.Salary

WHENNOTMATCHEDTHEN

INSERT (empId,Name,Salary)

VALUES (SOURCE.empId,SOURCE.Name,SOURCE.Salary);

While running above query sometime I got this error: No column name was specified for column 1 of 'SOURCE'.

The problem in the above query the alias name not there on the columns in Select statement, to resolve this error above query can be written in following way