Friday, February 24, 2012

Error while using OUTPUT clause - The multi-part identifier could not be bound

I was trying to copy child records of one parent record into another, and wanted to report back new child record id and corresponding child record id that was used to create it. I ran into run-time error with OUTPUT clause. Following is a script that will duplicate the situation I ran into:

CREATE TABLE Parent(

ParentID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,

ParentName VARCHAR(50) NOT NULL)

GO

CREATE TABLE Child(

ChildID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,

ParentID INT NOT NULL REFERENCES Parent(ParentID),

ChildName VARCHAR(50) NOT NULL)

GO

INSERT INTO Parent(ParentName) VALUES('Parent 1')

INSERT INTO Parent(ParentName) VALUES('Parent 2')

GO

INSERT INTO Child(ParentID, ChildName) VALUES(1, 'Child 1')

INSERT INTO Child(ParentID, ChildName) VALUES(1, 'Child 2')

GO

At this stage, there Child table looks like:

ChildID

ParentID

ChildName

1

1

Child 1

2

1

Child 2

What I want to do is copy Parent 1’s children to Parent 2, and report back which source ChildID that was used to create the new child records. So I wrote the query:

DECLARE @.LinkTable TABLE (FromChildID INT, ToChildID INT)

INSERT INTO Child(ParentID, ChildName)

OUTPUT c.ChildID, inserted.ChildID INTO @.LinkTable

SELECT 2, c.ChildName

FROM Child c

WHERE c.ParentID = 1

SELECT * FROM @.LinkTable

In the end I was expecting Child table to look like:

ChildID

ParentID

ChildName

1

1

Child 1

2

1

Child 2

3

2

Child 1

4

2

Child 2

and OUTPUT clause to return me:

FromChildID

ToChildID

1

3

Child record with ID 3 was created using ID of 1.

2

4

Child record with ID 4 was created using ID of 2.

But infact I’m getting following error:

Msg 4104, Level 16, State 1, Line 9

The multi-part identifier "c.ChildID" could not be bound.

Any ideas on how to fix the OUTPUT clause in the query to return me the expected output?

Thanks

Yogesh

This is not possible because the INSERT statement doesn’t have a FROM clause. The UPDATE and DELETE however has the non-standard TSQL specific FROM clause as part of the DML statement itself so you can reference those tables in the OUTPUT clause. You can only do INSERT….VALUES or INSERT..SELECT or INSERT…EXECUTE. So you can only reference the inserted table. And the INSERT DML itself cannot reference other tables (except you can now use CTE in SQL Server 2005 and reference it as target table of insert which is non-standard also).

See the link below for the OUTPUT clause usage (also documents where you can use from_table_name in the OUTPUT clause column list):

http://msdn2.microsoft.com/en-us/ms177564(SQL.90).aspx

In your example, you need to reference the inserted table columns:

INSERT INTO Child(ParentID, ChildName)

OUTPUT inserted.ParentID, inserted.ChildID INTO @.LinkTable

SELECT 2, c.ChildName

FROM Child c

WHERE c.ParentID = 1

|||

Thanks a bunch Umachandar for your prompt reply. I really need the output as I explained earlier. I played with CTE as per your suggestion for a while but I could not get records inserted into my @.LinkTable table variable as I wanted. Finally as a last option I tried with cursors, and got the source and target child id output.

I’m not terribly worried about poor performance of cursor because this stored procedure will be called only once in 6 months when no one else but admin of my application is logged in. What are your thoughts on using this in production app given these circumstances?

DECLARE

@.ChildID int,

@.ChildName varchar(50)

DECLARE child_cur CURSOR FORWARD_ONLY READ_ONLY FOR

SELECT ChildID, ChildName

FROM Child

WHERE ParentID = 1

DECLARE @.LinkTable TABLE (FromChildID INT, ToChildID INT)

OPEN child_cur

FETCH child_cur INTO @.ChildID, @.ChildName

WHILE @.@.fetch_status = 0

BEGIN

INSERT INTO Child(ParentID, ChildName)

OUTPUT @.ChildID, inserted.ChildID INTO @.LinkTable

VALUES (2, @.ChildName)

FETCH child_cur INTO @.ChildID, @.ChildName

END

CLOSE child_cur

DEALLOCATE child_cur

SELECT * FROM @.LinkTable

This code gave my expected output:

FromChildID

ToChildID

1

3

2

4

|||

I didn't mean to imply that using a CTE as target for the insert statement will work. It is just not possible to reference other tables in the OUTPUT clause of INSERT DML. Your code looks fine except that I don't see the need for OUTPUT clause at all. You can just do below:

DECLARE

@.ChildID int,

@.ChildName varchar(50)

DECLARE child_cur CURSOR FORWARD_ONLY READ_ONLY FOR

SELECT ChildID, ChildName

FROM Child

WHERE ParentID = 1

DECLARE @.LinkTable TABLE (FromChildID INT, ToChildID INT)

OPEN child_cur

WHILE (1=1)

BEGIN

FETCH child_cur INTO @.ChildID, @.ChildName

IF @.@.FETCH_STATUS < 0 BREAK

INSERT INTO Child(ParentID, ChildName) VALUES (2, @.ChildName)

INSERT INTO @.LinkTable VALUES (@.ChildID, SCOPE_IDENTITY())

END

CLOSE child_cur

DEALLOCATE child_cur

SELECT * FROM @.LinkTable

Alternatively, if the ChildName is unique per parent (your schema doesn't seem to imply if this is the case) then you can simply get the link table values by doing following query:

SELECT c1.ChildID AS FromChildID, c2.ChildID AS ToChildID

FROM Child as c1

JOIN Child as c2

ON c2.ChildName = c1.ChildName

WHERE c2.ParentID = 2

AND c1.ParentID = 1

So by putting the necessary constraints on your data model you can answer most questions efficiently.

|||

I have similer problem, I need to return with OUTPUT clause field from the SELECT clause in insert statement.
If I'll do it as suggested using CURSOR, it going to take a lot of time.

CREATE TABLE [dbo].[ProductsMapping](

[OldProductID] [int] NOT NULL,

[NewProductID] [int] NOT NULL,

[ProductName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

[IsNEQ] [bit] NOT NULL,

[IsNew] [bit] NOT NULL,

CONSTRAINT [PK_ProductsMapping] PRIMARY KEY NONCLUSTERED

(

[OldProductID] ASC

,[NewProductID] ASC

)WITH FILLFACTOR = 90 ON [PRIMARY]

) ON [PRIMARY]

CREATE TABLE [dbo].[Products](

[ProductID] [int] IDENTITY(1,1) NOT NULL,

[ProductName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

CONSTRAINT [PK_Products] PRIMARY KEY NONCLUSTERED

(

[ProductID] ASC

)WITH FILLFACTOR = 90 ON [PRIMARY]

) ON [PRIMARY]

INSERT INTO ProductsMapping

(OldProductID

,NewProductID

,ProductName

,IsNEQ

,IsNew)

SELECT o.ProductID OldProductID

,isnull(n.ProductID,o.ProductID) NewProductID

,o.ProductName

,(isnull(n.ProductID,0)-o.ProductID) IsNEQ

,isnull(n.ProductID,0) IsNew

FROM LINKEDSRV.DB.dbo.Products o

LEFT OUTER JOIN Products n ON n.ProductName = o.ProductName

DECLARE @.NewProducts TABLE (

[OldProductID] [int] NOT NULL,

[NewProductID] [int] NOT NULL,

[ProductName] [varchar](50))

DECLARE @.OldProductID int

DECLARE @.ProductName varchar(50)

DECLARE NewProduct_cur CURSOR FORWARD_ONLY READ_ONLY FOR

SELECT w.ProductName

,w.OldProductID

FROM ProductsMapping w

WHERE w.IsNew=1

OPEN NewProduct_cur

FETCH NewProduct_cur INTO @.ProductName, @.OldProductID

WHILE @.@.fetch_status = 0

BEGIN

INSERT INTO Products(ProductName)

OUTPUT INSERTED.ProductID AS NewProductID

,INSERTED.ProductName AS ProductName

,@.OldProductID AS OldProductID

INTO @.NewProducts

VALUES (@.ProductName)

END

CLOSE NewProduct_cur

DEALLOCATE NewProduct_cur

UPDATE ProductsMapping

SET NewProductID=t.NewProductID

FROM ProductsMapping w

INNER JOIN @.NewProducts t ON t.OldProductID=w.OldProductID

AND IsNew=1

I also tried CTE and it doesn't work.

Do you have any suggestion how to do it with out cursor?

THNX

Jermy

No comments:

Post a Comment