11

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

Cade Roux
  • 88,164
  • 40
  • 182
  • 265
burnt1ce
  • 14,387
  • 33
  • 102
  • 162
  • Burnt, can you provide some sample code on how you'd like to pass nulls... inline string building or typed parameters on your ADO Command object? – p.campbell Jul 21 '09 at 15:38

4 Answers4

10

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
  • 8,042
  • 13
  • 55
  • 78
5

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
  • 4,529
  • 11
  • 46
  • 64
2

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
  • 98,673
  • 67
  • 256
  • 322
  • Unfortunately i have to use stored procedure to maintain consistency in my project. Is there a way that i can use stored procedures? – burnt1ce Jul 21 '09 at 16:03
  • @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 Jul 21 '09 at 16:17
2

Short answer: Set the parameter value to Null (VBScript keyword).

Keith
  • 20,636
  • 11
  • 84
  • 125