views:

2443

answers:

3

In VBScript (ASP environment), is it possible to pass a parameter with a null value to a stored procedure?

A: 

If you're building strings, and checking/guarding against SQL Injection, you can simply use the word null in your SQL string, or comma delimited EXEC statement.

'calling a stored proc.
strSQL = "EXEC UpdateCustomer @CustomerID=" & iCustomerID + ",@PhoneNumber=null"

Here's how to use null in a manually built string to send to the DB

strSQL = "UPDATE Customer SET PhoneNumber = null WHERE CustomerID = " + iCustomerID
p.campbell
Unfortunately i have to use stored procedure to maintain consistency in my project. Is there a way that i can use stored procedures?
burnt1ce
@Burnt: are you able to edit your question to show how you're using stored procedures? The answer here shows one way to use stored procedures, perhaps you're using another way. Please include some code, it'll definitely help.
p.campbell
A: 

Passing null to a stored procedure, using a command object.

Set cn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
cn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Data Source=.\Test"
Set cmd.ActiveConnection = cn
cmd.CommandText = "TestTable.sp_ModifyData"
cmd.CommandType = 4
cmd.NamedParameters = True

set cnParam = cmd.CreateParameter("@RowID",3,3,,-1)
cmd.Parameters.Append cnParam
set cnParam = cmd.CreateParameter("@AddRemoveModify",3,1,,0)
cmd.Parameters.Append cnParam
set cnParam = cmd.CreateParameter("@Value1",3,1,,0)
cmd.Parameters.Append cnParam
set cnParam = cmd.CreateParameter("@Value2",8,1,-1,"Test")
cmd.Parameters.Append cnParam
set cnParam = cmd.CreateParameter("@value3",5,1,,null)
cmd.Parameters.Append cnParam
set cnParam = cmd.CreateParameter("@value4",5,1,,0)
cmd.Parameters.Append cnParam
set cnParam = cmd.CreateParameter("@value5",8,1,-1,"")
cmd.Parameters.Append cnParam
cmd.Execute
cn.Close
Set cmd = Nothing
Set cn = Nothing

Sorry I didn't put much thought into naming the fields in my database.

Tester101
A: 

Try vbNullString or vbNullChar. You may also need adParamNullable.

set cnParam = cmd.CreateParameter("@value3",5,1,,vbNullString)  
cnParam.Attributes = adParamNullable  
cmd.Parameters.Append cnParam

Update:

Actually this worked for me:

set cnParam = cmd.CreateParameter("@value3",5,1,,Null)  
cnParam.Attributes = adParamNullable  
cmd.Parameters.Append cnParam

Huh, this worked too:

set cnParam = cmd.CreateParameter("@value3",5,1,,Null)  
cmd.Parameters.Append cnParam

Go figure.

Kuyenda