RE: Returning guid from inserted row in SQL server 2005

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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

---------------------------------------------------------------------------------------------------------------------------------------------------



.



Relevant Pages

  • Re: programatically add table in access
    ... In SQL Server you can default a guid datatype with a newGUIDbuiltin function, but check the documentation for Access. ... Autonumber is a field attribute, as is the format property which you would use to set the displayed decimal places. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Setting control value based on a SQL Select statement
    ... I don't understand what you mean by « So as an alternative I use a SQL ... Select statement where GUID = linked server GUID in the hope I can return ... The form is bound to a SQL view and the control is bound to a field ... Each time you select a client from the client combo box a GUID is ...
    (microsoft.public.access.adp.sqlserver)
  • Not finding GUID value after migration sql 2005 to 2000
    ... I've developed a web app using sql 2005. ... a guid value in the ASP.NET ... which works fine on my 2005 server but breaks on the 2000 server. ... // Retrieve information through the stored GUID ...
    (comp.databases.ms-sqlserver)
  • RE: RowFilter Failing
    ... rowFilter acts the same way as a where clause in SQL and SQL WHERE clause ... doesnt support GUID and image types. ... PatientsDisplayImageTypeID (GUID), PatientID, ImageTypeID ... PatientsDisplayImageTypes", conSLDB) ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: The Web site that is referenced here is not in the configuration database.
    ... a wrong GUID in the SQLServerAgent can cause such 324 ... SQL Server Agent feature is not available/ supported on a WMSDE ... This newsgroup only focuses on SBS technical issues. ... you may want to contact Microsoft CSS directly. ...
    (microsoft.public.windows.server.sbs)