Showing posts with label return. Show all posts
Showing posts with label return. Show all posts

Wednesday, March 21, 2012

Problem with @@identity return in stored procedure insert.

I'm having a problem I do an insert into a table but I want to return the value of the identity field of that insert so I can email a confirmation. For some reason this code doesn't work.
Below is the stored procedure I'm calling and below that the code I'm using. What am I doing wrong. The value I have returned is null when it should be a number. Any suggestions. Why does finalMagicNum2 come back null when it should grab the identity field of the inserted record.

CREATE PROCEDURE addMagicRecTest
(
@.theSequence int,
@.theSubject int,
@.theFirstName nvarchar(50)=null
@.theLastName nvarchar(75)=null
)

AS

INSERT INTO employees([Sequence],subject,firstname,lastname)
VALUES(@.theSequence,@.theSubject,@.theFirstName,@.theLastName)
SELECT @.@.identity AS finalNum

magicDataConnect = ConfigurationSettings.AppSettings("myDataConnect")
Response.Write(magicDataConnect)
magicCommand = New SqlDataAdapter("addMagicRecTest", magicDataConnect)
magicCommand.ConnectionType = CommandType.StoredProcedure
magicCommand.SelectCommand.CommandType = CommandType.StoredProcedure

' Sequence ID for request
magicCommand.SelectCommand.Parameters.Add(New SqlParameter("@.theSequence", SqlDbType.NVarChar, 8))
magicCommand.SelectCommand.Parameters("@.theSequence").Value = "41833"

' Subject for new Wac Ticket
magicCommand.SelectCommand.Parameters.Add(New SqlParameter("@.theSubject", SqlDbType.NVarChar, 8))
magicCommand.SelectCommand.Parameters("@.theSubject").Value = "1064"

' First Name Field
magicCommand.SelectCommand.Parameters.Add(New SqlParameter("@.theFirstName", SqlDbType.NVarChar, 50))
magicCommand.SelectCommand.Parameters("@.theFirstName").Value = orderFirstName

' Last Name Field
magicCommand.SelectCommand.Parameters.Add(New SqlParameter("@.theLastName", SqlDbType.NVarChar, 75))
magicCommand.SelectCommand.Parameters("@.theLastName").Value = orderLastName

DSMagic = new DataSet()
magicCommand.Fill(DSMagic,"employees")

If DSMagic.Tables("_smdba_._telmaste_").Rows.Count > 0 Then
finalMagicNum2 = DSMagic.Tables("_smdba_._telmaste_").Rows(0)("finalMagic").toString
End If

I need finalMagicNum2I usually just have the stored proc return the @.@.identity like so:


CREATE PROCEDURE addMagicRecTest

(

@.theSequence int,

@.theSubject int,

@.theFirstName nvarchar(50)=null

@.theLastName nvarchar(75)=null

)

AS

INSERT INTO employees([Sequence],subject,firstname,lastname)

VALUES(@.theSequence,@.theSubject,@.theFirstName,@.theLastName)

Return @.@.Identity

Your stored proc is not returning finalnum and your code has no output param set up in it.

Sam|||Try this code in .NET


DSMagic = new DataSet()

magicCommand.Fill(DSMagic,"employees")

If DSMagic.Tables(0).Rows.Count > 0 Then

finalMagicNum2 = DSMagic.Tables(0).Rows(0).item("finalnum").toString
'OR
'finalMagicNum2 = DSMagic.Tables(0).Rows(0).item(0).toString

End If

Hope this help

Monday, March 12, 2012

problem when using temp table to hold data that return from another procedure

Hello,

We have a Query-Hierarchy procedure take two personID and return the persons that in between of them if they are in the same hierarchy. The procedure works fine. But this procedure was called in many other procedures. We use a temp table to hold the data that return from Query_Hierarchy:

create Table #TEMPRESOURCEHI(ResourceID int, Firstname varchar(50), Secondname varchar(50))

insert into #TEMPRESOURCEHI exec ResourceDB.dbo.Query_Hierarchy @.ResID, @.s_LSOResID

The problem is, we have some procedures using such code to get data, and most of them work. 2 years ago, only one procedure is not working, though Query_Hierarchy return correct resutls which have more than one rows, the temp table only contain one row. And later on, some other procedures get the same problem as well, I know some of them are working before, now is broken as well, the temp table always only get one row inserted no even Query_Hierarchy return more than one rows.

For example, after
insert into #TEMPRESOURCEHI exec ResourceDB.dbo.Query_Hierarchy @.ResID, @.s_LSOResID

If I select * from #TEMPRESOURCEHI so I can get the result in query analyzer, it contains one row.
I also try directly call Query_Hierarchy to let the result show in query anaylyzer as well, it return correctly with more than one row.

We have no idea why such problem occurs and why occurs randomly, we can't see any problem from our code and hope someone can have a look and give some suggestions...

Thanks in advanced

The Query_Hierarchy code is as below:
========================================
CREATE PROCEDURE dbo.Query_Hierarchy
@.FirstID int, --ResourceID of the 'leaf'
@.LastID int --ResourceID of the ancestor
AS
DECLARE @.S TABLE (ResourceID int, FirstName VARCHAR (50), LastName VARCHAR (50))
DECLARE @.R TABLE (ResourceID int)

INSERT INTO @.S (ResourceID, FirstName, LastName)
(SELECT Resource.[ID], Person.FirstName, Person.LastName
FROM Resource
INNER JOIN Person ON PersonID = Person.[ID]
WHERE Resource.[ID] = @.FirstID)
WHILE (@.@.ROWCOUNT > 0)
BEGIN
INSERT INTO @.S (ResourceID, FirstName, LastName)
(SELECT ParentID, Person.FirstName, Person.LastName
FROM @.S
INNER JOIN ResourceAssociation ON ResourceID = ChildID
INNER JOIN Resource ON ResourceAssociation.ParentID = Resource.[ID]
LEFT OUTER JOIN Person ON Resource.PersonID = Person.[ID]
WHERE ParentID NOT IN (SELECT ResourceID FROM @.S) AND ParentID <> @.LastID)
END

INSERT INTO @.S (ResourceID, FirstName, LastName)
(SELECT Resource.[ID], Person.FirstName, Person.LastName
FROM Resource
LEFT OUTER JOIN Person ON PersonID = Person.[ID]
WHERE Resource.[ID] = @.LastID)

--Now walk the tree from the top down and filter out any nodes that don't belong to the root given by @.LastID. Easy.
INSERT INTO @.R (ResourceID)
VALUES (@.LastID)
WHILE (@.@.ROWCOUNT > 0)
BEGIN
INSERT INTO @.R (ResourceID)
(SELECT ChildID FROM @.R INNER JOIN ResourceAssociation ON ResourceID = ParentID
WHERE ChildID NOT IN (SELECT ResourceID FROM @.R))
END

SELECT ResourceID, FirstName, LastName FROM @.S
WHERE ResourceID IN (SELECT ResourceID FROM @.R)
AND FirstName is not null
AND LastName is not null

GO
========================================

Try setting the NOCOUNT ON at the beginning of the procedure that loads the temp table. I ran into a similar situation a couple of years ago loading recordsets and only getting one result. When we set nocount on the problem resolved.

Code Snippet

SET NOCOUNT ON

create Table #TEMPRESOURCEHI(ResourceID int, Firstname varchar(50), Secondname varchar(50))

insert into #TEMPRESOURCEHI exec ResourceDB.dbo.Query_Hierarchy @.ResID, @.s_LSOResID

Try this out and see if it resolves your issue. If not try setting the NOCOUNT ON in the ResourceDB.dbo.Query_Hierarchy stored procedure.


problem when using temp table to hold data that return from another procedure

Hello,

We have a Query-Hierarchy procedure take two personID and return the persons that in between of them if they are in the same hierarchy. The procedure works fine. But this procedure was called in many other procedures. We use a temp table to hold the data that return from Query_Hierarchy:

create Table #TEMPRESOURCEHI(ResourceID int, Firstname varchar(50), Secondname varchar(50))

insert into #TEMPRESOURCEHI exec ResourceDB.dbo.Query_Hierarchy @.ResID, @.s_LSOResID

The problem is, we have some procedures using such code to get data, and most of them work. 2 years ago, only one procedure is not working, though Query_Hierarchy return correct resutls which have more than one rows, the temp table only contain one row. And later on, some other procedures get the same problem as well, I know some of them are working before, now is broken as well, the temp table always only get one row inserted no even Query_Hierarchy return more than one rows.

For example, after
insert into #TEMPRESOURCEHI exec ResourceDB.dbo.Query_Hierarchy @.ResID, @.s_LSOResID

If I select * from #TEMPRESOURCEHI so I can get the result in query analyzer, it contains one row.
I also try directly call Query_Hierarchy to let the result show in query anaylyzer as well, it return correctly with more than one row.

We have no idea why such problem occurs and why occurs randomly, we can't see any problem from our code and hope someone can have a look and give some suggestions...

Thanks in advanced

The Query_Hierarchy code is as below:
========================================
CREATE PROCEDURE dbo.Query_Hierarchy
@.FirstID int, --ResourceID of the 'leaf'
@.LastID int --ResourceID of the ancestor
AS
DECLARE @.S TABLE (ResourceID int, FirstName VARCHAR (50), LastName VARCHAR (50))
DECLARE @.R TABLE (ResourceID int)

INSERT INTO @.S (ResourceID, FirstName, LastName)
(SELECT Resource.[ID], Person.FirstName, Person.LastName
FROM Resource
INNER JOIN Person ON PersonID = Person.[ID]
WHERE Resource.[ID] = @.FirstID)
WHILE (@.@.ROWCOUNT > 0)
BEGIN
INSERT INTO @.S (ResourceID, FirstName, LastName)
(SELECT ParentID, Person.FirstName, Person.LastName
FROM @.S
INNER JOIN ResourceAssociation ON ResourceID = ChildID
INNER JOIN Resource ON ResourceAssociation.ParentID = Resource.[ID]
LEFT OUTER JOIN Person ON Resource.PersonID = Person.[ID]
WHERE ParentID NOT IN (SELECT ResourceID FROM @.S) AND ParentID <> @.LastID)
END

INSERT INTO @.S (ResourceID, FirstName, LastName)
(SELECT Resource.[ID], Person.FirstName, Person.LastName
FROM Resource
LEFT OUTER JOIN Person ON PersonID = Person.[ID]
WHERE Resource.[ID] = @.LastID)

--Now walk the tree from the top down and filter out any nodes that don't belong to the root given by @.LastID. Easy.
INSERT INTO @.R (ResourceID)
VALUES (@.LastID)
WHILE (@.@.ROWCOUNT > 0)
BEGIN
INSERT INTO @.R (ResourceID)
(SELECT ChildID FROM @.R INNER JOIN ResourceAssociation ON ResourceID = ParentID
WHERE ChildID NOT IN (SELECT ResourceID FROM @.R))
END

SELECT ResourceID, FirstName, LastName FROM @.S
WHERE ResourceID IN (SELECT ResourceID FROM @.R)
AND FirstName is not null
AND LastName is not null

GO
========================================

Try setting the NOCOUNT ON at the beginning of the procedure that loads the temp table. I ran into a similar situation a couple of years ago loading recordsets and only getting one result. When we set nocount on the problem resolved.

Code Snippet

SET NOCOUNT ON

create Table #TEMPRESOURCEHI(ResourceID int, Firstname varchar(50), Secondname varchar(50))

insert into #TEMPRESOURCEHI exec ResourceDB.dbo.Query_Hierarchy @.ResID, @.s_LSOResID

Try this out and see if it resolves your issue. If not try setting the NOCOUNT ON in the ResourceDB.dbo.Query_Hierarchy stored procedure.