Solved

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

Posted on 2016-08-25
6
41 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 66

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

695 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