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
2009-07-21 15:41:56
Unfortunately i have to use stored procedure to maintain consistency in my project. Is there a way that i can use stored procedures?
burnt1ce
2009-07-21 16:03:04
@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
2009-07-21 16:17:18
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
2009-07-21 18:35:01
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
2009-11-17 05:30:25