Showing posts with label parent. Show all posts
Showing posts with label parent. Show all posts

Monday, March 26, 2012

ERROR: A variable may only be added once to either the read lock list or the write lock list.

Hi,
I have set of 2 DTS packages, one of which calls the other by forming a command-line (dtexec) using a Execute Process task.

From the parent package-> Execute Process Task->
dtsexec /F etc... /<pkg variable> = "servername"

Each of the parent and the called package have a variable: "User::DWServerSQLInstance" which is mapped to the SQL server connection manager server name property using an expression. The outer package has the above variable and so does the inner called package (which gets assigned through the command line from the outerpackage call to inner)

I "sometimes" get the following error:

OnError,I4,TESTDOM\Administrator,ACDWAggregation,{A1F8E43F-15F1-4685-8C18-6866AB31E62B},{77B2F3C7-6756-46EB-8C01-D880598FB4B3},5/22/2006 5:10:28 PM,5/22/2006 5:10:28 PM,-1073659822,0x,The variable "User::DWServerSQLInstance" is already on the read list. A variable may only be added once to either the read lock list or the write lock list.

Help would be appreciated!

I have seen other posts on this but, not able to relate the solution to my scenario.

Do you have multiple tasks (in the same package) executing in parallel, and using same variable, e.g. in property mappings?

If yes, I think this problem is fixed by this post-SP1 fixes collection:
http://support.microsoft.com/Default.aspx?id=918222

Alternatively, you may change the package so the tasks are executed sequentially by creating precedence constraints.|||

Hi Brian,
The child Package uses the User Variable: DWSQLInstanceName mentioned above to initialize 4 SQL Server connections (2 OLEDB & 2 ADO .NET) -- Only <<Server Name>> property using expressions.

I also checked that this error seems to be only on Multi-proc machine. Don't get the same on my laptop.

I cannot seralize the tasks since, the error is coming @. package level while evaluating the package expressions (i.e. for the connections) when the package starts.

What is the best way to still use expressions for connection server names but, get around this error?
Is this what is fixed in SP1?

Also, it seems that the packages does not fail when it encounters the error which I am getting. I have MaxErrorCount = 1 for Package. The package goes ahead.

|||I believe the issue with connections sharing the variable is fixed in the commulative hotfix package I've mentioned above. It should be installed on top of SP1: first, install SP1 then install all the component packages from the hotfix package in the order they are listed in the article.

http://www.microsoft.com/downloads/details.aspx?FamilyId=CB6C71EA-D649-47FF-9176-E7CAC58FD4BC
and then
http://support.microsoft.com/Default.aspx?id=918222|||This was indeed a bug and the post SP1 hotfix "fixes" it.

However, you can still create the bug. It's harder but stil there...|||

Thanks Michael and Crispin for the help on this!

I think we will need to upgrade to SP1 and install the Hotfix bundle for SP1. I think this should fix the issue which I am facing i.e. use of same variable for setting connection "Server Name" - Property in the expressions.

Thanks,
Gaurav

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

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

Friday, February 17, 2012

Error while Inserting!

Hey folks,
I am trying to updat a table by inserting values from another table into the parent table. The SQL being used is:
"insert into "LogSchema"."_rt1182" select * from "LogSchema"."_rt557" where recordID not in (select * from "LogSchema"."_rt1182") ORDER BY TIMESTAMP"
I have used the above statement and was successful, but with these two particluar tables I am getting an error message" Wrong number of values to INSERT"
Please lemme know, what this means and how to correct this problem!
TIAYou've used this code, and it worked at least once?

-PatP|||Originally posted by Pat Phelan
You've used this code, and it worked at least once?

-PatP
Yes the code worked for other tables, but am facing the problem with these two particluar tables namely _rt1182 and _rt557!
Any thoughts?
TIA|||How about this:

insert into [LogSchema].[_rt1182] (<put_your_field_list_here>) select <put_your_field_list_here> from [LogSchema].[_rt557] r1 left outer join [LogSchema].[_rt1182] r2 on r1.recordID = r2.recordID where r2.recordID is null ORDER BY TIMESTAMP