sql server - Make ADO generate parameters for a temporary stored procedure? -
i'm trying ado recognize parameters stored procedure on sql sever. if normal stored procedure, works fine:
conn.execute "create proc normalsp (@i int output) set @i = 3" cmd.commandtype = adcmdstoredproc cmd.activeconnection = conn cmd.commandtext = "normalsp" cmd.parameters.refresh 'parameters list has @return_value , @i cmd.execute debug.print cmd("@i")
but if try same thing temporary stored procedure, can't parameters:
conn.execute "create proc #tempsp (@i int output) set @i = 3" cmd.commandtype = adcmdstoredproc cmd.activeconnection = conn cmd.commandtext = "#tempsp" cmd.parameters.refresh 'parameters list remains empty cmd.execute 'error: command expects parameter '@i', not supplied debug.print cmd("@i") 'error: item cannot found in collection
what more need parameters.refresh
work temp sp normal sp?
asking 2 black boxes (legacy ado, oledb/odbc provider) why don't interpretation of context should expose not provide satisfactory answer, since original intent unclear...
assuming can accept short-lived, global temporary stored procedure ask temporary parameters (and in contradiction presumably downvote above), connect right db in first place:
complete, repeat-safe sample (using sql 2012 native client):
public sub doitemporarilyhaveparametersforado() dim conn adodb.connection dim cmd adodb.command dim prm adodb.parameter set conn = new adodb.connection set cmd = new adodb.command conn.connectionstring = "driver={sql server native client 11.0};server=(local);database=tempdb;trusted_connection=yes;" conn.cursorlocation = aduseserver conn.open conn.execute "if object_id('tempsp') not null drop proc tempsp" conn.execute "create proc tempsp (@i int output) set @i = 3;" cmd.activeconnection = conn cmd.commandtype = adcmdstoredproc cmd.commandtext = "tempsp" cmd.parameters.refresh each prm in cmd.parameters debug.print prm.name & ": " & prm.value next prm debug.print "cmd.parameters.count: " & cmd.parameters.count cmd.execute debug.print cmd("@i") conn.execute "if object_id('tempsp') not null drop proc tempsp" conn.close set prm = nothing set cmd = nothing set conn = nothing end sub
immediate window output:
@return_value: @i: cmd.parameters.count: 2 3
some useful clarification msft:
temporary stored procedures useful when connecting earlier versions of sql server not support reuse of execution plans transact-sql statements or batches. applications connecting sql server 2000 , higher should use sp_executesql system stored procedure instead of temporary stored procedures.
....
if stored procedure not prefixed # or ## created directly in tempdb database, stored procedure automatically deleted when sql server shut down because tempdb re-created every time sql server started. procedures created directly in tempdb exist after creating connection terminated.
meaning of course if throw breakpoint vba sample round line 27, flip on ssms, connect (local)
, switch tempdb
, , exec tempsp
, told
procedure or function 'tempsp' expects parameter '@i', not supplied.
instead of
could not find stored procedure 'tempsp'.
before , after sample execution.
Comments
Post a Comment