I'm trying run basic Insert sql query.
Here is the insert sql.
Insert into tblABC( X1,X2,X3)
Select X1,X2,X3 from tblXX
Problem is tblXX
doesn't have X3
column.
Sometimes X3
will be there and sometimes it will be missing. When column is missing then push a default value such as 0
.
Trying to use Case statement
CASE WHEN EXISTS (SELECT 1 From information_schema.Columns C
Inner Join Sys.Objects O
On ( O.Object_Id = Object_Id(N'tblXX') And O.Name = C.Table_Name)
and C.Column_Name = 'X3') THEN X3
ELSE 0
END
I get an Invalid column name error on X3 column