I have a table that has a field of type uniqueidentifier which is going to be populated by a call to NEWID().
What I’d like to do is get back the GUID that was created right after I INSERT a record into the table.
If I run some SQL like the following from the SQL Management Studio it, very helpfully, prints out the value of my GUID
DECLARE @GUID uniqueidentifier;SET @GUID = NEWID();SELECT @GUID as guid;
My question is, how can I get at the value of @GUID from Classic ASP using VBScript?
I tried this
<%
conn.open Application("ConnectionString")
sql = "DECLARE @GUID uniqueidentifier;SET @GUID = NEWID();SELECT @GUID as guid;"
set res = conn.execute(sql)
response.write res.fields.count
response.write res("guid")
%>
but it doesn’t seem to work. The res object doesn’t have any fields so attempting to read the guid field errors out.
The error that I get is Item cannot be found in the collection corresponding to the requested name or ordinal. at the line where I try to read the value in res("guid")
It looks like you have simplified your query. The snippet shown works as is.
If however, you are running a stored proc or multi-statement query, conn.execute can return multiple recordsets, which you can step through using
RecordSet.nextRecordSetuntil you get to the last one.Status messages can also interfere with recordsets, so make sure you use
SET NOCOUNT ONin multi-statement batches.