Umar Ahsan
Umar Ahsan

Reputation: 37

Getting The multi-part identifier could not be bound error in SQL Server 2012

I am getting this error after I execute the query. Both tables have a column ID. I am using Microsoft SQL Server 2012

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "CDATA.ID" could not be bound.

Code:

INSERT INTO CDATA(Name, Mobile, Email, [Address], [Date]) 
    SELECT Name, Mobile, Email, [Address], [Date] 
    FROM CustomerData
    WHERE CustomerData.ID != CDATA.[ID]

Upvotes: 1

Views: 109

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 176024

To check if record already exists in table you may use NOT EXISTS:

INSERT INTO CDATA(Name, Mobile, Email, [Address], [Date])
 SELECT Name, Mobile, Email, [Address], [Date]
 FROM CustomerData
 WHERE NOT EXISTS (SELECT 1 FROM CData WHERE  CustomerData.ID = CDATA.[ID]);

Alternatively EXCEPT:

INSERT INTO CDATA(Name, Mobile, Email, [Address], [Date])
SELECT Name, Mobile, Email, [Address], [Date]
FROM CustomerData
EXCEPT
SELECT Name, Mobile, Email, [Address], [Date]
FROM CDATA;

Upvotes: 3

Related Questions