In this script, I call a scalar function written for Sql Server 2005.
This script was tested in Windows Server 2008.
I use VBScript, ADO and ODBC to make this call.
This scalar function is called IsIpBlocked
It has only one parameter, which is of varchar type.
The return value, which is read and printed from this script, is of bit data type.
Therefore, if 1 is returned "true" is printed else if 0 is returned "false" is printed, and if null is returned "false" is printed.
I saved this file with a vbs extension.
Later, I called this file from the command prompt.
I saved this file as udf.vbs, so to execute this script, from the command prompt I executed:
wscript udf.vbs 127.0.0.1
The ip address you see, is an argument passed to this script, which is turn is passed as a parameter to this scalar function.
This script is shown as follows:
set cn = CreateObject("ADODB.Connection")
set cmd = CreateObject("ADODB.Command")
cn.open "Driver={SQL Native Client};Server=your_server;Database=your_database; Trusted_Connection=yes;"
Set cmd.ActiveConnection = cn
cmd.CommandText = "IsIpBlocked"
cmd.CommandType = 4
' Ask the server about the parameters for this scalar function
cmd.Parameters.Refresh
' Assign a value to the 2nd parameter.
' because this scalar function only has one parameter
' in this case, it is an ip address
' so, its only parameter is varchar data type
' Index of parameter 0 represents return value.
cmd(1) = wscript.arguments(0)
cmd.execute
wscript.echo "the answer is: " & cmd(0)
cn.close()
As an alternative, you can connect to your database in Sql Server, using a Sql login.
This can be achieved with the connection string shown as follows:
"Driver={SQL Native Client};Server=your_server;Database=your_database; Uid=your_username;Pwd=your_password;"
A few more notes: if your scalar function call fails then find out, by asking your Dba, the schema or database owner to which your scalar function belongs to. Typically, it is "dbo", but because of security, architecture, design, etc reasons it might not be "dbo".
Therefore, in my case, I can change line of code from cmd.CommandText = "IsIpBlocked" to cmd.CommandText = "dbo.IsIpBlocked"
Moreover, to change this script from a Windows Administrative script to a Classic Asp script, change "CreateObject" to "Server.CreateObject". You also need to change "wscript.echo" to, for example, "response.write" method call. Finally, change "wscript.arguments" to, for example, "request.querystring" call in case of using get method, or "request.form" call in case of using post method. An example of a Classic Asp script running in Windows 7 or 2008, against a Sql Azure database, is found at http://sqlpoetry.blogspot.com/2010/06/calling-stored-procedure-in-sql-azure.html
Finally, if you need to call a user-defined scalar function with Vb.Net and Ado.Net then please go to http://sqlpoetry.blogspot.com/2009/11/example-of-calling-sql-server-2008-user.html
Good luck,
Tonci Korsano Saavedra
Please, visit my Resume Web Site and my .Net Framework Blog
This script was tested in Windows Server 2008.
I use VBScript, ADO and ODBC to make this call.
This scalar function is called IsIpBlocked
It has only one parameter, which is of varchar type.
The return value, which is read and printed from this script, is of bit data type.
Therefore, if 1 is returned "true" is printed else if 0 is returned "false" is printed, and if null is returned "false" is printed.
I saved this file with a vbs extension.
Later, I called this file from the command prompt.
I saved this file as udf.vbs, so to execute this script, from the command prompt I executed:
wscript udf.vbs 127.0.0.1
The ip address you see, is an argument passed to this script, which is turn is passed as a parameter to this scalar function.
This script is shown as follows:
set cn = CreateObject("ADODB.Connection")
set cmd = CreateObject("ADODB.Command")
cn.open "Driver={SQL Native Client};Server=your_server;Database=your_database; Trusted_Connection=yes;"
Set cmd.ActiveConnection = cn
cmd.CommandText = "IsIpBlocked"
cmd.CommandType = 4
' Ask the server about the parameters for this scalar function
cmd.Parameters.Refresh
' Assign a value to the 2nd parameter.
' because this scalar function only has one parameter
' in this case, it is an ip address
' so, its only parameter is varchar data type
' Index of parameter 0 represents return value.
cmd(1) = wscript.arguments(0)
cmd.execute
wscript.echo "the answer is: " & cmd(0)
cn.close()
As an alternative, you can connect to your database in Sql Server, using a Sql login.
This can be achieved with the connection string shown as follows:
"Driver={SQL Native Client};Server=your_server;Database=your_database; Uid=your_username;Pwd=your_password;"
A few more notes: if your scalar function call fails then find out, by asking your Dba, the schema or database owner to which your scalar function belongs to. Typically, it is "dbo", but because of security, architecture, design, etc reasons it might not be "dbo".
Therefore, in my case, I can change line of code from cmd.CommandText = "IsIpBlocked" to cmd.CommandText = "dbo.IsIpBlocked"
Moreover, to change this script from a Windows Administrative script to a Classic Asp script, change "CreateObject" to "Server.CreateObject". You also need to change "wscript.echo" to, for example, "response.write" method call. Finally, change "wscript.arguments" to, for example, "request.querystring" call in case of using get method, or "request.form" call in case of using post method. An example of a Classic Asp script running in Windows 7 or 2008, against a Sql Azure database, is found at http://sqlpoetry.blogspot.com/2010/06/calling-stored-procedure-in-sql-azure.html
Finally, if you need to call a user-defined scalar function with Vb.Net and Ado.Net then please go to http://sqlpoetry.blogspot.com/2009/11/example-of-calling-sql-server-2008-user.html
Good luck,
Tonci Korsano Saavedra
Please, visit my Resume Web Site and my .Net Framework Blog