RE: Returning guid from inserted row in SQL server 2005
- From: Terry <TerryL@xxxxxxxxxxxxx>
- Date: Mon, 23 Jul 2007 07:34:02 -0700
Sorry,
That should be:
SELECT MyKey = @@IDENTITY
--
Terry
"Steve" wrote:
Hi All.
Can someone help get the rowGUID value after an insert
I am using VB.net 2005 and SQL server Express 2005
The column GUID is an uniqueidentifier column with default value
(newsequentialid())
The following is my current function which is clumsy to say the least
I am sure there is a more elegant solution
Regards
Steve
----------------------------------------------------------------------------------------------------------
Public Function SaveParamDataGetGUID(ByVal mycmd As SqlCommand, ByVal
mytable As String) As Guid
Dim con As New SqlConnection, guid As Guid, sql As String = ""
Dim myint As Int16 = 0
Try
con.ConnectionString = connectionstring
con.Open()
mycmd.Connection = con
mycmd.ExecuteNonQuery()
sql = "select max(mykey) as maxkey from " & mytable
mycmd.CommandText = sql
myint = mycmd.ExecuteScalar
sql = "select guid from " & mytable & " where mykey = " & myint
mycmd.CommandText = sql
guid = mycmd.ExecuteScalar
Return guid
Catch ex As SqlException
mymsgbox(ex.Message)
Return Nothing
Finally
If Not IsNothing(mycmd) Then
mycmd.Dispose()
mycmd = Nothing
End If
If Not IsNothing(con) Then
con.Close()
con.Dispose()
con = Nothing
End If
End Try
End Function
---------------------------------------------------------------------------------------------------------------------------------------------------
- References:
- Prev by Date: self-inflicted authentication issue
- Next by Date: Re: Timer_Tick event
- Previous by thread: RE: Returning guid from inserted row in SQL server 2005
- Next by thread: Creating a new instance
- Index(es):
Relevant Pages
|