Ronda Pederson's Facebook profile

<May 2013>
SMTWTFS
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678



RSS

Subscribe to the RSS feed for this blog.

Search


(single terms only)

Categories

  • RSS
  • RSS
  • RSS
  • RSS
  • RSS
  • RSS
  • RSS
  • RSS
  • RSS
  • Quote

    "I don't want to hate but that's all you've left me with,
    A bitter aftertaste and a fantasy of how we all could live,
    No regrets, they don't work,
    No regrets now, they only hurt,
    Sing me a love song,
    Drop me a line,
    Suppose it's just a point of view,
    \But they tell me I'm doing fine,
    If I could just stop hating you,
    I'd feel sorry for us instead"

    —"No Regrets" - Robbie Williams

    Blogs I Read

    Links




    Fun

     
       |  Return Values from Stored Procedure
    [ ]

    When dealing with multiple tables and you need to connect them with common information such as the ID of an order it is often necessary to insert a record, get back the ID and then go on to do other things utilizing that ID. The easiest way is using the @@Identity of the first table.

    Example Sproc might look something like this:
    Create Procedure "InsertnewOrder"

    As

    Insert into Orders(orderDate, orderStatus) Values(getdate(),'Shopping')

    RETURN (@@IDENTITY)

    In our page we could do something like this to get back the ID of the new record:
    Dim myCon As SqlConnection
    myCon = New SqlConnection(conString)
    Dim myCmd As SqlCommand
    myCmd = New SqlCommand("InsertnewOrder", myCon)
    myCmd.CommandType = CommandType.StoredProcedure
    myCmd.Parameters.Add("@return", SqlDbType.Int)
    myCmd.Parameters("@return").Direction = ParameterDirection.ReturnValue
    myCon.Open()
    myCmd.ExecuteNonQuery()
    Session("orderID") = myCmd.Parameters("@return").Value.ToString()

    That is about it... pretty simple huh?
    posted at 11:32 AM | [1] |