Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output parameter returning NULL

Status
Not open for further replies.

coco86

Programmer
Mar 13, 2002
296
DE
I have a stored procedure to copy a purchase order and all the order items associated with it but I need to know the new number when it's done. I found a sample on the web and modified it so that it looks like this
Code:
CREATE Procedure TestCopy
  @ID int
, @autonum int OUTPUT
AS
 begin tran
 insert tblPO (OrderID, CompanyID, VendorID, 
               OrderedBy,InputAmount, Shipto, ShipVia, 
               Terms, FOB, TaxID, ContactName, Phone, 
               Fax, POComments)
 Select OrderID, CompanyID, VendorID, OrderedBy, 
        InputAmount, Shipto, ShipVia, Terms, 
        FOB, TaxID, ContactName, Phone, Fax, 
        POComments
 from tblPO where POID = @ID
 set @autonum = @@identity
 Insert tblPOItems (POID, ItemName, ItemQty, ItemDescrip, 
                    ItemPrice) 
 select @autonum, ItemName, ItemQty, ItemDescrip, 
        ItemPrice 
 from tblPOItems where POID = @ID
 commit tran
Then I call it from my application like this:
Code:
Public Function copyPO(ByVal OldID As Long) As Long
 Dim NewID As Long
 Dim conn As New ADODB.Connection

 conn.Open
 conn.testcopy OldID, NewID
 conn.Close
 copyPO = NewID
End Function
The PO gets copied, but NewID is 0. If I run the stored procedure from Query Analyzer, I get NULL.

I can't quite figure it out because @autonum is getting set correctly, otherwise my line items wouldn't have the correct number and they do. I've never worked with output parameters before so maybe I'm doing something wrong.

-Coco

[auto]
 
Yes, I'm sorry, I should have said that.

-Coco

[auto]
 
wait, I'm sorry, no it's not. The identity field in this case is POID

-Coco

[auto]
 
Looks like you need to set your @autonum below the tblPOItems insert statment.

Thanks

J. Kusch
 
Wouldn't that give me the Identity field (POItemID) from the tblPOItems?

-Coco

[auto]
 
YES ... If you are wanting the Identity of the tblPO, one of the fields has to be set as an Identity field or the @@Identity will always come back null.

Thanks

J. Kusch
 
I tried taking out the second half of the SP so I ended up with this:
Code:
CREATE Procedure TestCopy
  @ID int
, @autonum int OUTPUT
AS
 begin tran
 insert tblPO (OrderID, CompanyID, VendorID, 
               OrderedBy,InputAmount, Shipto, ShipVia, 
               Terms, FOB, TaxID, ContactName, Phone, 
               Fax, POComments)
 Select OrderID, CompanyID, VendorID, OrderedBy, 
        InputAmount, Shipto, ShipVia, Terms, 
        FOB, TaxID, ContactName, Phone, Fax, 
        POComments
 from tblPO where POID = @ID
 set @autonum = @@identity 
 commit tran
I was thinking that if I could get the ID number, I could simply use a second SP to copy the line items. However, the results were the same.

-Coco

[auto]
 
I believe your issue stems from the fact that your tblPO, from what I am reading, DOES NOT have an Identity field set. If there is no Identity field set on the table you are inserting into ... @@Identity will always be Null.

Thanks

J. Kusch
 
Here is the verbiage straight from BOL (Books OnLine) ...

@@IDENTITY
Returns the last-inserted identity value.

Syntax
@@IDENTITY

Return Types
numeric

Remarks
After an INSERT, SELECT INTO, or bulk copy statement completes, @@IDENTITY contains the last identity value generated by the statement. If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL. If multiple rows are inserted, generating multiple identity values, @@IDENTITY returns the last identity value generated. If the statement fires one or more triggers that perform inserts that generate identity values, calling @@IDENTITY immediately after the statement returns the last identity value generated by the triggers. If a trigger is fired after an insert action on a table that has an identity column, and the trigger inserts into another table that does not have an identity column, @@IDENTITY will return the identity value of the first insert. The @@IDENTITY value does not revert to a previous setting if the INSERT or SELECT INTO statement or bulk copy fails, or if the transaction is rolled back.




Thanks

J. Kusch
 
Thanks for the information but that doesn't really address my issue. What I really need to know is why if I specify this:
Code:
  @ID int
, @autonum int OUTPUT
and this:
Code:
set @autonum = @@identity
and then call this:
Code:
conn.testcopy OldID, NewID
do I get a value of 0 for NewID? Even though this:
Code:
Insert tblPOItems (POID, ItemName, ItemQty, ItemDescrip, 
                    ItemPrice) 
 select @autonum, ItemName, ItemQty, ItemDescrip, 
        ItemPrice 
 from tblPOItems where POID = @ID
works?


-Coco

[auto]
 
I finally got this to work. After locating another similar post I tried changing my code to this:

Code:
Public Function copyPO(ByVal OldID As Long) As Long
    Dim NewID As Long
    Dim conn As New ADODB.Connection
    Dim cmdStoredProc As ADODB.Command
    
    conn.ConnectionString = MS_CONN_STRING
    conn.CursorLocation = adUseClient
    conn.Open
    Set cmdStoredProc = New ADODB.Command
    Set cmdStoredProc.ActiveConnection = conn
    
    With cmdStoredProc
        .CommandText = "testcopy"
        .CommandType = adCmdStoredProc
        .Parameters.Append .CreateParameter("@ID", 
                       adInteger, adParamInput, , OldID)
        .Parameters.Append .CreateParameter("@autonum", 
                       adInteger, adParamOutput, , NewID)
        .Execute
    End With
    
    copyPO = NewID
End Function
but it still didn't work. After more research and several unsuccessful modifications to the SP, I made this change:
Code:
    With cmdStoredProc
        .CommandText = "testcopy"
        .CommandType = adCmdStoredProc
        Set p1 = .CreateParameter("@ID", adInteger, 
                      adParamInput, , OldID)
        Set p2 = .CreateParameter("@autonum", adInteger, 
                      adParamOutput, , Null)
        .Parameters.Append p1
        .Parameters.Append p2
        .Execute
    End With
    
    copyPO = p2.Value
and I finally got the new PO number. I still don't understand exactly why the other methods didn't work for me as they seem to work for others but at least I now have a working function so I'm happy.

-Coco

[auto]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top