Solved

SQL Server 2016 Insert statement not working.  table could not be bound error.

Posted on 2016-08-25
6
24 Views
Last Modified: 2016-08-25
Hello All,
I'm having an issue with an insert statement that I think is ok but giving me a "Table could not be bound" error.  I'm simply trying to insert records from a table on one server to a table on another server.  Originally I used a "Not In" statement that worked on the QCID fields, but since we updated to SQL 2016 and the database has grown the 'Not In' times out and crashes the job.  Here is what I have so far.:

INSERT INTO [Server].[Database].dbo.tblOrderItemSize (QCID,JoxID,Parent,Whs,OrderNum,LineNum,[Assembly],GlassSize,SashSize,FrameSize,AssyLoc,QNADesc,DateStamp,GridType,GridLayout, IGThick) 

SELECT QCID,JoxID,Parent,Whs,OrderNum,LineNum,[Assembly],GlassSize,SashSize,FrameSize,AssyLoc,QNADesc,DateStamp,GridType,GridLayout, IGThick

FROM tblImportSize

WHERE  (Whs = 105) AND (GlassSize is not null) AND (NOT EXISTS (Select * from [Server].[Database].dbo.tblOrderItemSize WHERE tblImportSize.QCID = [Server].Database.dbo.tblOrderItemSize.QCID))

Open in new window


This statement gives me a "Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "[Server].[Database].dbo.tblOrderItemSize.QCID" could not be bound." error


Any help would be greatly appreciated.  Thank you in advance!
Jeff
0
Comment
Question by:jeff-h
  • 3
  • 2
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41770542
<wild knee-jerk reaction>  

By any chance is QCID an identity column that does not allow values to be explicitly inserted into it unless SET IDENTITY_INSERT OFF is executed?
0
 

Author Comment

by:jeff-h
ID: 41770567
On the source table it is an identity column but on the destination table it is not.  When I use
WHERE ( Whs = 105 ) AND ( GlassSize IS NOT NULL ) AND (QCID > (SELECT MAX (QCID)FROM [Server].Database.dbo.tblOrderItemSize))

Open in new window

It goes right through.  The problem is that I was missing records this way.
0
 
LVL 8

Expert Comment

by:Randy Peterson
ID: 41770569
So the issue is in  the not exist clause of your statement.

see below...

Select * from [Server].[Database].dbo.tblOrderItemSize WHERE tblImportSize.QCID = [Server].Database.dbo.tblOrderItemSize.QCID

Everything else looks good until the "tblImportSize.QCID = [Server].Database.dbo.tblOrderItemSize.QCID"

You are comparing a QCID to a table value QCID.

One thing you could do is change it to:

Select * from [Server].[Database].dbo.tblOrderItemSize WHERE tblImportSize.QCID in select QCID from [Server].Database.dbo.tblOrderItemSize.QCID

That will match any QCID that is already in the table and won't insert a new one in.

Hope that helps.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:jeff-h
ID: 41770623
OK, I changed the last line to this:  
WHERE  (Whs = 105) AND (GlassSize is not null) AND NOT EXISTS (Select * from ([Server].[Database].dbo.tblOrderItemSize) WHERE (tblImportSize.QCID) IN (Select QCID From [Server].Database.dbo.tblOrderItemSize)

Open in new window

I keep getting Msg 102, Level 15, State 1, Line 7 Incorrect syntax near ')'. error.
0
 
LVL 8

Accepted Solution

by:
Randy Peterson earned 500 total points
ID: 41770665
You are just having a parenthesis error.  Try this:

INSERT INTO [Server].[Database].dbo.tblOrderItemSize (QCID,JoxID,Parent,Whs,OrderNum,LineNum,[Assembly],GlassSize,SashSize,FrameSize,AssyLoc,QNADesc,DateStamp,GridType,GridLayout, IGThick)
SELECT QCID,JoxID,Parent,Whs,OrderNum,LineNum,[Assembly],GlassSize,SashSize,FrameSize,AssyLoc,QNADesc,DateStamp,GridType,GridLayout, IGThick
FROM tblImportSize
WHERE  (Whs = 105) AND (GlassSize is not null) AND (NOT EXISTS (Select * from [Server].[Database].dbo.tblOrderItemSize WHERE tblImportSize.QCID IN (Select QCID From [Server].Database.dbo.tblOrderItemSize)))
0
 

Author Closing Comment

by:jeff-h
ID: 41770682
You saved my day, I can't believe I missed that.  Thanks a million!
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now