I have a script that runs a stored procedure in my SQL server database, the problem is the stored procedure takes a uniqueidentifier parameter. I have a function that grabs a session id from the database (which is an nvarchar), so VBScript makes it a string and I need to convert it to pass it to the stored procedure.
Function GetOpenSession Set conn = CreateObject('ADODB.Connection') Set rs = CreateObject('ADODB.Recordset') conn.Open 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Data Source=' & Source rs.CursorLocation = 3 rs.Open 'SELECT top 1 OpenSession FROM OpenSessions with (nolock)' , conn, 3, 3 If rs.RecordCount = 0 Then MsgBox 'No Connection' Else GetOpenSession = rs.Fields(0).Value End If End Function
I get ‘Invalid character value for cast specification’ when I try to execute the stored procedure.
set cnParam = cmd.CreateParameter('@ActiveSession',72,1,,GetOpenSession) cmd.Parameters.Append cnParam
I can’t change anything in the database, so I need a way to overcome this in my script.
Depending on the data type of the SELECT OpenSession, you may be able to cast/convert it in the query and VBScript may possibly keep the data type as a GUID:
When you use
GetOpenSessionorrs.Fields(0).Value, hopefully VBScript will keep it as a GUID.The other possibility seems to be a Win32 API using
CoCreateGuidandStringFromGUID2. An example is found here, but it requires external Win32 functions and a Type for GUID.