Error when using Parameters in ADODB - sql-server

Dim strSQL As String
strSQL = "INSERT INTO [" & AccountCode & "].[Orders] ( OrderID,OrderDate,BarUTC,OrderUTC,Exchange,Symbol,OrderSignedAmount,OrderPrice,Type,TargetPosition,AccountIdent,Status)" _
& " VALUES ( #OrderID,#OrderDate,#BarUTC,#OrderUTC,#Exchange,#Symbol,#OrderSignedAmount,#OrderPrice,#Type,#TargetPosition,#AccountIdent,#Status)"
Dim cmd As New ADODB.Command
Set cmd.ActiveConnection = objMyConn
cmd.CommandText = strSQL
cmd.CommandType = adCmdText
cmd.NamedParameters = True
SeqNum = SeqNum + 1
AccountIdent = AccountCode + ":" + ModelCode
cmd.Parameters.Append cmd.CreateParameter("#OrderID", adInteger, adParamInput, -1, SeqNum)
cmd.Parameters.Append cmd.CreateParameter("#OrderDate", adDate, adParamInput, 0, Date)
cmd.Parameters.Append cmd.CreateParameter("#BarUTC", adDBTimeStamp, adParamInput, 0, Time)
cmd.Parameters.Append cmd.CreateParameter("#OrderUTC", adDBTimeStamp, adParamInput, 0, Time)
cmd.Parameters.Append cmd.CreateParameter("#Exchange", adVarChar, adParamInput, -1, "Future")
cmd.Parameters.Append cmd.CreateParameter("#Symbol", adVarChar, adParamInput, -1, Symbol)
cmd.Parameters.Append cmd.CreateParameter("#OrderSignedAmount", adInteger, adParamInput, , TargetPosition)
cmd.Parameters.Append cmd.CreateParameter("#OrderPrice", adDouble, adParamInput, , -1)
cmd.Parameters.Append cmd.CreateParameter("#Type", adVarChar, adParamInput, -1, "MARKETIOC")
cmd.Parameters.Append cmd.CreateParameter("#TargetPosition", adInteger, adParamInput, , TargetPosition)
cmd.Parameters.Append cmd.CreateParameter("#AccountIdent", adVarChar, adParamInput, -1, AccountIdent)
cmd.Parameters.Append cmd.CreateParameter("#Status", adVarChar, adParamInput, -1, "NEW")
cmd.Execute
This gives me a "Must declare the scalar variable error "#OrderID"
Why?

You can't name your parameters in the SQL command text as you've tried to do.
If you change all of them simply to the "?" placeholder then your code should work, e.g.
Dim strSQL As String
strSQL = "INSERT INTO [" & AccountCode & "].[Orders] ( OrderID,OrderDate,BarUTC,OrderUTC,Exchange,Symbol,OrderSignedAmount,OrderPrice,Type,TargetPosition,AccountIdent,Status)" _
& " VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?)"
Dim cmd As New ADODB.Command
Set cmd.ActiveConnection = objMyConn
cmd.CommandText = strSQL
cmd.CommandType = adCmdText
cmd.NamedParameters = True
SeqNum = SeqNum + 1
AccountIdent = AccountCode + ":" + ModelCode
cmd.Parameters.Append cmd.CreateParameter("#OrderID", adInteger, adParamInput, -1, SeqNum)
cmd.Parameters.Append cmd.CreateParameter("#OrderDate", adDate, adParamInput, 0, Date)
cmd.Parameters.Append cmd.CreateParameter("#BarUTC", adDBTimeStamp, adParamInput, 0, Time)
cmd.Parameters.Append cmd.CreateParameter("#OrderUTC", adDBTimeStamp, adParamInput, 0, Time)
cmd.Parameters.Append cmd.CreateParameter("#Exchange", adVarChar, adParamInput, -1, "Future")
cmd.Parameters.Append cmd.CreateParameter("#Symbol", adVarChar, adParamInput, -1, Symbol)
cmd.Parameters.Append cmd.CreateParameter("#OrderSignedAmount", adInteger, adParamInput, , TargetPosition)
cmd.Parameters.Append cmd.CreateParameter("#OrderPrice", adDouble, adParamInput, , -1)
cmd.Parameters.Append cmd.CreateParameter("#Type", adVarChar, adParamInput, -1, "MARKETIOC")
cmd.Parameters.Append cmd.CreateParameter("#TargetPosition", adInteger, adParamInput, , TargetPosition)
cmd.Parameters.Append cmd.CreateParameter("#AccountIdent", adVarChar, adParamInput, -1, AccountIdent)
cmd.Parameters.Append cmd.CreateParameter("#Status", adVarChar, adParamInput, -1, "NEW")
cmd.Execute
At that point it doesn't matter what you call them when you create the command parameters. Take note that the parameters are passed by ordinal position not by name!
If you want to name the parameters in your SQL code nicely then you should look into creating a stored procedure, although your dynamic schema name would make that tricky.
You could possibly also use sp_executesql. https://msdn.microsoft.com/en-us/library/ms188001.aspx

Related

How to find duplicate entries in table (SQL Server) in VBA [duplicate]

This question already has answers here:
INSERT VALUES WHERE NOT EXISTS
(6 answers)
Closed 9 months ago.
I am inserting records into a SQL Server table from an Excel sheet through VBA code with the same column headers as in the SQL Server table.
There are 4 columns in the SQL Server table which should not have values to be inserted that already exist. Those columns on the table are SF Payroll ID, Unity Payroll Element Name, ICP/LMC Element Code & ICP/LMC Element Name.
Every time each row is being inserted from the Excel file the code needs to check, that inserting values for columns SF Payroll ID + Unity Payroll Element Name + ICP/LMC Element Code + ICP/LMC Element Name does not already exist.
If the row contains the same value that already exists on the column then that row should be rejected from being uploaded to the SQL Server table and the rest of the rows should be uploaded.
Below is my code which uploads the records into the SQL Server table. Kindly suggest how to code in VBA to validate this.
Private Sub PushToDB_Click()
Dim conn As ADODB.connection
Dim connString As String
connString = "Provider=SQLOLEDB;Server=DEEPAKSQL;Database=Workload;User Id=DHARMA;Password= ********"
Set conn = New ADODB.connection
conn.Open connString
Dim rowCountsheet As Integer
rowCountsheet = ActiveSheet.UsedRange.Rows.Count
Dim tempisheet As Integer
tempisheet = 2
Dim shsheet As Worksheet
Set shsheet = ThisWorkbook.Worksheets("Data copied")
On Error GoTo CleanFail
conn.BeginTrans
Dim rowCount As Integer
rowCount = ActiveSheet.UsedRange.Rows.Count
Dim tempi As Integer
tempi = 2
Dim sql As String
sql = " INSERT INTO [dbo].[ELEMENT_INFO] ([DB_Upload_Action_Key],[Account_Client_Customer_Name],[SF_Account_ID],[Payroll],[SF_Payroll_ID],[Record_Creation_Date],[Record_Creation_Time_At],[Record_Creation_Guardian_Name],[Record_Last_Modified_Date],[Record_Last_Modified_Time_At],[Record_Last_Modified_Guardian_Name]," _
& "[Unity_Payroll_Element_Name],[Element_Description],[Element_Status],[Element_Type]," _
& "[Element_Input_Classification],[Element_Input_Type],[Element_Frequency]," _
& "[ICP_Or_LMC_Element_Code],[ICP_Or_LMC_Element_Name],[Source_Of_Data_Input_HCM_Integration_EUT_T_A_Other],[GL_Code_Debit],[GL_Code_Credit],[GL_Account_Name],[Comments])" & _
"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Data copied")
Dim pctDone As Single
Dim iLabelWidth As Integer
iLabelWidth = 240
Do Until tempi = rowCount + 1
mapping_upload.Hide
frmProgressForm.Show
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = sql
cmd.Parameters.Append cmd.CreateParameter("DB_Upload_Action_Key", adVarChar, adParamInput, 100, sh.Cells(tempi, 1).Value)
cmd.Parameters.Append cmd.CreateParameter("Account_Client_Customer_Name", adVarChar, adParamInput, 250, sh.Cells(tempi, 2).Value)
cmd.Parameters.Append cmd.CreateParameter("SF_Account_ID", adVarChar, adParamInput, 250, sh.Cells(tempi, 3).Value)
cmd.Parameters.Append cmd.CreateParameter("SF_Payroll_ID", adVarChar, adParamInput, 250, sh.Cells(tempi, 6).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Creation_Date", adDBDate, adParamInput, 100, dateLabel.Caption) 'sh.Cells(tempi, 10).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Creation_Time_At", adDBTime, adParamInput, 100, timeLabel.Caption) 'sh.Cells(tempi, 11).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Creation_Guardian_Name", adVarChar, adParamInput, 100, TextBox1.Text) 'sh.Cells(tempi, 12).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Last_Modified_Date", adDBDate, adParamInput, 100, dateLabel.Caption) 'sh.Cells(tempi, 13).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Last_Modified_Time_At", adDBTime, adParamInput, 100, timeLabel.Caption) 'sh.Cells(tempi, 14).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Last_Modified_Guardian_Name", adVarChar, adParamInput, 100, TextBox1.Text) 'sh.Cells(tempi, 15).Value)
cmd.Parameters.Append cmd.CreateParameter("Unity_Payroll_Element_Name", adVarChar, adParamInput, 3500, sh.Cells(tempi, 16).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Description", adVarChar, adParamInput, 5000, sh.Cells(tempi, 17).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Status", adVarChar, adParamInput, 500, sh.Cells(tempi, 18).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Type", adVarChar, adParamInput, 5000, sh.Cells(tempi, 19).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Input_Classification", adVarChar, adParamInput, 5000, sh.Cells(tempi, 32).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Input_Type", adVarChar, adParamInput, 5000, sh.Cells(tempi, 33).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Frequency", adVarChar, adParamInput, 5000, sh.Cells(tempi, 39).Value)
cmd.Parameters.Append cmd.CreateParameter("ICP_Or_LMC_Element_Code", adVarChar, adParamInput, 5000, sh.Cells(tempi, 40).Value)
cmd.Parameters.Append cmd.CreateParameter("ICP_Or_LMC_Element_Name", adVarChar, adParamInput, 5000, sh.Cells(tempi, 41).Value)
cmd.Parameters.Append cmd.CreateParameter("Source_Of_Data_Input_HCM_Integration_EUT_T_A_Other", adVarChar, adParamInput, 5000, sh.Cells(tempi, 42).Value)
cmd.Parameters.Append cmd.CreateParameter("GL_Code_Debit", adVarChar, adParamInput, 5000, sh.Cells(tempi, 43).Value)
cmd.Parameters.Append cmd.CreateParameter("GL_Code_Credit", adVarChar, adParamInput, 5000, sh.Cells(tempi, 44).Value)
cmd.Parameters.Append cmd.CreateParameter("GL_Account_Name", adVarChar, adParamInput, 5000, sh.Cells(tempi, 45).Value)
cmd.Parameters.Append cmd.CreateParameter("Comments", adVarChar, adParamInput, 8000, sh.Cells(tempi, 50).Value)
cmd.Execute
tempi = tempi + 1
pctDone = (tempi - 1) / rowCount
frmProgressForm.lblProgress.Width = iLabelWidth * pctDone
frmProgressForm.FrameProgress.Caption = Format(pctDone, "0%")
DoEvents
Loop
Unload frmProgressForm
MsgBox "Data Loaded Successfully to T-1 DataBase", vbInformation, "SDD04 - Commit Transaction, Okay!"
mapping_upload.Show
conn.CommitTrans
CleanExit:
conn.Close
Exit Sub
CleanFail:
conn.RollbackTrans
MsgBox "Input file error either value exceeds or having wrong type. Transaction was rolled back. " & Err.Description, vbCritical, "SDD04 - Input error"
Unload frmProgressForm
Debug.Print Err.Number, Err.Description
Resume CleanExit
End Sub
You are using a loop to reach every row, and building the INSERT INTO statement. That's right, but I don't see any code trying to check what you need: If that row already exists before inserting it.
So, what you need is to check, for every row, if it exists in your SQL table before inserting it. As you have to do it for every row, think that the first thing you should do in the loop before build the INSERT statement, is to build the SELECT statement to do the check, and then, depending on the result, you will do the INSERT or not.
I suppose you can build a SELECT statement from VBA if you are able to build an INSERT statement. Otherwise you can ask for help about it.
ADDED: 2022-05-20
Here is a code snippet that may help you with the SELECT statement.
Private Sub Test()
Dim cnn As New ADODB.Connection
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Dim ConnectionString As String
Dim strSql As String
ConnectionString = "Provider=SQLOLEDB;Server=DEEPAKSQL;Database=Workload;User Id=DHARMA;Password= ********"
strSql = "SELECT * FROM [dbo].[ELEMENT_INFO] WHERE [SF Payroll ID]=" & Value1 & " AND [Unity Payroll Element Name]='" & Value2 & "' AND ..."
rst.CursorLocation = adUseClient ' Client-side cursor
rst.Open strSql, cnn
If Not rst.RecordCount > 0 Then
'Not Exists -> Do the Insert
Else
'Exists -> Don't do the Insert
End If
rst.Close
Set rst = Nothing
End Sub

ASP Classic vbScript - Execute SQL stored procedure with recordset and returned values

I am trying to execute a stored procedure with input parameters, a recordset returned and a return parameter. When I go to the asp page, I get a 500 error. The stored procedure runs as expected in SQL Server by itself. Can you tell where the errors are in the below code?
' The following line must be changed to reflect your data source info
Conn.Open "Data Source=path; Initial Catalog=database;","username","password"
set cmd = Server.CreateObject("ADODB.Command")
set cmd.ActiveConnection = Conn
' Specify the name of the stored procedure you wish to call
cmd.CommandText = "ap_RF_085"
cmd.CommandType = adCmdStoredProc
' Use the values from the table in the following lines to define parameters
cmd.Parameters.Append cmd.CreateParameter("#RETURN_VALUE", adInteger, adParamReturnValue)
cmd.Parameters.Append cmd.CreateParameter("#Whse_Loc", adVarChar, adParamInput, 6)
cmd.Parameters.Append cmd.CreateParameter("#Whse_Zone", adVarChar, adParamInput, 6)
cmd.Parameters.Append cmd.CreateParameter("#Width_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Height_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Depth_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Type", adVarChar, adParamInput, 1)
cmd.Parameters.Append cmd.CreateParameter("#Site", adVarChar, adParamInput, 8)
cmd.Parameters("#Whse_Loc") = "4H"
cmd.Parameters("#Whse_Zone") = ""
cmd.Parameters("#Width_Char") = ""
cmd.Parameters("#Height_Char") = ""
cmd.Parameters("#Depth_Char") = ""
cmd.Parameters("#Type") = ""
cmd.Parameters("#Site") = ""
cmd.Parameters("#Whse_Loc") = ""
cmd.Parameters("#Whse_Loc") = "MZ"
SET rs = cmd.Execute
returned = cmd.parameters("#RETURN_VALUE").value
RESPONSE.WRITE(returned)
Thanks for your help!
I updated my code to the suggestion below. I am still getting a 500 error. Also the tab in IE11 show "Waiting on [serverName]". Can you tell why from the code what is not being passed correctly?
cmd.Parameters.Append cmd.CreateParameter("#RETURN_VALUE", adInteger, adParamReturnValue)
cmd.Parameters.Append cmd.CreateParameter("#Whse_Loc", adVarChar, adParamInput, 6)
cmd.Parameters.Append cmd.CreateParameter("#Whse_Zone", adVarChar, adParamInput, 6)
cmd.Parameters.Append cmd.CreateParameter("#Width_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Height_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Depth_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Type", adVarChar, adParamInput, 1)
cmd.Parameters.Append cmd.CreateParameter("#Site", adVarChar, adParamInput, 8)
cmd.Parameters("#Whse_Loc") = "4H"
cmd.Parameters("#Whse_Zone") = ""
cmd.Parameters("#Width_Char") = ""
cmd.Parameters("#Height_Char") = ""
cmd.Parameters("#Depth_Char") = ""
cmd.Parameters("#Type") = ""
cmd.Parameters("#Site") = ""
cmd.Parameters("#Whse_Loc") = ""
cmd.Parameters("#Whse_Loc") = "MZ"
SET rs = cmd.Execute
IF NOT rs.EOF THEN data = rs.GETROWS()
CALL rs.CLOSE()
returned = cmd.parameters("#RETURN_VALUE").value
Please, try this: Procedure
DECLARE #RETURN_VALUE XXXXXX = NULL
OUTPUT ASP file SET cmd = server.CreateObject("ADODB.command")
cmd.ActiveConnection = conn cmd.CommandText = "" cmd.CommandType = 4 cmd.CommandTimeout = 3000 cmd.Parameters.refresh
cmd.Execute returned = cmd.Parameters.item("#RETURN_VALUE")
& "" RESPONSE.WRITE(returned)
cmd.close
Set cmd = Nothing

Write Multiple Rows in EXCEL (VB) to SQL Table

Would like to be able to write all rows that have data in them to SQL table, with the below I can only export the 1st row. If I add more rows of data and click the Export Button I will only send the first row. Can some one help me script my request?
Sub Button1_Click()
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim strSQL As String
strSQL = "INSERT INTO dbo.TimeLog" & _
"(EventDate, ID, DeptCode, Opcode, StartTime, FinishTime, Units) " & _
"VALUES (?,?,?,?,?,?,?);"
Set conn = New ADODB.Connection
conn.Open "Provider=SQLOLEDB;Data Source=db\db1;Initial Catalog=Table1;Integrated Security=SSPI;"
'Skip the header row
iRowNo = 2
Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = strSQL
iRowNo = 2
With Sheets("Sheet1")
'Loop until empty cell in EventDate
Do Until .Cells(iRowNo, 1) = ""
cmd.Parameters.Append _
cmd.CreateParameter("pEventDate", adVarChar, adParamInput, 8, .Cells(iRowNo, 1))
cmd.Parameters.Append _
cmd.CreateParameter("pID", adInteger, adParamInput, , .Cells(iRowNo, 2))
cmd.Parameters.Append _
cmd.CreateParameter("pDeptCode", adVarChar, adParamInput, 2, .Cells(iRowNo, 3))
cmd.Parameters.Append _
cmd.CreateParameter("pOpCode", adVarChar, adParamInput, 2, .Cells(iRowNo, 4))
cmd.Parameters.Append _
cmd.CreateParameter("pStartTime", adDBTime, adParamInput, 0, .Cells(iRowNo, 5))
cmd.Parameters.Append _
cmd.CreateParameter("pFinishTime", adDBTime, adParamInput, 0, .Cells(iRowNo, 6))
cmd.Parameters.Append _
cmd.CreateParameter("pUnits", adInteger, adParamInput, , .Cells(iRowNo, 7))
cmd.Execute
iRowNo = iRowNo + 20
Loop
MsgBox "Success!"
End With
conn.Close
Set conn = Nothing
End Sub

Parameter object improperly defined Error, Parameter definitions seem to be correct

I have a stored procedure that takes 6 parameters 5 inputs and 1 output.
All of the parameters seem to be properly defined (parameter name, data type, input or output, size and value). My output size and data type does not seem to be the problem. however I get the error message "Parameter Object is improperly defined Run-Time error 3708" only sometimes. The odd thing is that If I run this code with all other values equal (including CurrentCnn, adCmdStoredProc, etc.)with a value of OptStore or OptCorporate True my code runs fine.
If I have any other Opt* variable True instead then I get this error. all strStorNo values are string values at most 3 chars long. I can test my stored procedure and it returns my count value as expected with the stored procedure values I would pass in for all cases.
here is the vb6:
138 With cmdSP
140 .ActiveConnection = CurrentCNN
142 .CommandType = adCmdStoredProc
144 .CommandText = "HIcountProducts"
146 If blnUseUPCinSP Then
148 .Parameters.Append .CreateParameter("#upc", adVarChar, adParamInput, 13, objProdB.upc)
150 .Parameters.Append .CreateParameter("#cert_code", adVarChar, adParamInput, 15, Null)
152 .Parameters.Append .CreateParameter("#vendor", adInteger, adParamInput, , Null)
Else
154 .Parameters.Append .CreateParameter("#upc", adVarChar, adParamInput, 13, Null)
156 .Parameters.Append .CreateParameter("#cert_code", adVarChar, adParamInput, 15, objProdB.cert_code)
158 .Parameters.Append .CreateParameter("#vendor", adInteger, adParamInput, , Null)
End If
162 If intRetailVer = 2 Then
If OptStore.Value = True Then
.Parameters.Append .CreateParameter("#levelID", adVarChar, adParamInput, 3, strStoreNo)
.Parameters.Append .CreateParameter("#levelType", adVarChar, adParamInput, 1, 3)
ElseIf OptCorporate.Value = True Then
.Parameters.Append .CreateParameter("#levelID", adVarChar, adParamInput, 3, strStoreNo)
.Parameters.Append .CreateParameter("#levelType", adVarChar, adParamInput, 1, 6)
ElseIf OptZones.Value = True Then
.Parameters.Append .CreateParameter("#levelID", adVarChar, adParamInput, 3, strStoreNo)
.Parameters.Append .CreateParameter("#levelType", adVarChar, adParamInput, 1, 2)
ElseIf OptRegions.Value = True Then
.Parameters.Append .CreateParameter("#levelID", adVarChar, adParamInput, 3, strStoreNo)
.Parameters.Append .CreateParameter("#levelType", adVarChar, adParamInput, 1, 1)
ElseIf OptClasses.Value = True Then
.Parameters.Append .CreateParameter("#levelID", adVarChar, adParamInput, 3, strStoreNo)
.Parameters.Append .CreateParameter("#levelType", adVarChar, adParamInput, 1, 1)
End If
End If
.Parameters.Append .CreateParameter("#count", adInteger, adParamOutput, 1, 1)
166 .Execute , , adExecuteNoRecords
End With
168 intRecordCount = cmdSP.Parameters("#count")
170 Set cmdSP = Nothing
Im stumped here, is there something else this parameter object improperly defined could be referencing besides my parameter definitions?
You are defining the #levelType parameter as adVarChar but you are passing an Integer to it. Pass the parameter as String instead.
...
.Parameters.Append .CreateParameter("#levelType", adVarChar, adParamInput, 1, "2")
...
I used "2" instead of just 2.

Only Specify Certain Parameters in Command Objects in ADO with VBScript?

If I have a stored procedure in SQL Server with a number of named input parameters, all with default values as follows:
CREATE PROCEDURE [dbo].[proc_name]
(
#IN_mode CHAR(1) = NULL, /* 'I','U'*/
#IN_external_identity_id INT = -1,
#IN_provider_name VARCHAR(45) = NULL,
#IN_login_id VARCHAR(255) = NULL,
#IN_email VARCHAR(255) = NULL,
#IN_ip VARCHAR(45) = NULL,
#IN_first_name VARCHAR(255) = NULL,
#IN_last_name VARCHAR(255) = NULL,
#IN_json_response_raw VARCHAR(8000) = NULL,
#IN_user_id VARCHAR(255) = NULL,
#IN_name VARCHAR(255) = NULL
)
AS
BEGIN
...
...and in the vbScript I call the procedure like this:
Set oSproc = CreateObject("ADODB.Command")
With oSproc
.ActiveConnection = oConn
.CommandText = "bfsp_insert_NEW_EXTERNAL_IDENTITY"
.CommandType = adCmdStoredProc
With .Parameters
.Append oSproc.CreateParameter("#IN_mode", adChar, adParamInput, 1, "I")
.Append oSproc.CreateParameter("#IN_external_identity_id", adInteger, adParamInput, 0, -1) '-1 because field isn't needed on new insert
.Append oSproc.CreateParameter("#IN_provider_name", adVarChar, adParamInput, 45, "FACEBOOK")
.Append oSproc.CreateParameter("#IN_login_id", adVarChar, adParamInput, 255, getInput("fbNewUserName"))
.Append oSproc.CreateParameter("#IN_email", adVarChar, adParamInput, 255, oFbUser.email)
.Append oSproc.CreateParameter("#IN_ip", adVarChar, adParamInput, 46, getUserIp())
.Append oSproc.CreateParameter("#IN_first_name", adVarChar, adParamInput, 255, oFbUser.first_name)
.Append oSproc.CreateParameter("#IN_last_name", adVarChar, adParamInput, 255, oFbUser.last_name)
.Append oSproc.CreateParameter("#IN_json_response_raw", adVarChar, adParamInput, 8000, oFbUser.rawJson)
.Append oSproc.CreateParameter("#IN_user_id", adVarChar, adParamInput, 255, oFbUser.id)
.Append oSproc.CreateParameter("#IN_name", adVarChar, adParamInput, 255, oFbUser.name)
End With
.Execute
End With
iNewExternalIdentityId = oSproc.parameters("#OUTPUT_originalId").Value 'grabbing the new EXTERNAL_IDENTITY.ID
Set oSproc = Nothing
I noticed that the parameter name in the vbscript create parameter statement doesn't link by name to the variable in the sproc. It seems to be matching up based on ordinal position e.g. param 1 matches param 1, and so on. If I left one out, say, in the middle somewhere, all remaining parameters will be assigned to the wrong input variable in the sproc.
How can I create parameters in vbscript code so that I can send in only the select parameters I need, and still have them match up correctly with the parameters in the procedure?
I am posting a formal answer incorporating the suggestion from #jeroen-mostert.
All that is needed to pass parameters by name is to add the namedParameters property to the command object and set it to True as follows. Line 3 in the snippet below was added, otherwise it is the same as the original:
Set oSproc = CreateObject("ADODB.Command")
With oSproc
.NamedParameters = True
.ActiveConnection = oConn
.CommandText = "bfsp_insert_NEW_EXTERNAL_IDENTITY"
.CommandType = adCmdStoredProc
With .Parameters
.Append oSproc.CreateParameter("#IN_mode", adChar, adParamInput, 1, "I")
.Append oSproc.CreateParameter("#IN_external_identity_id", adInteger, adParamInput, 0, -1) '-1 because field isn't needed on new insert
.Append oSproc.CreateParameter("#IN_provider_name", adVarChar, adParamInput, 45, "FACEBOOK")
.Append oSproc.CreateParameter("#IN_login_id", adVarChar, adParamInput, 255, getInput("fbNewUserName"))
.Append oSproc.CreateParameter("#IN_email", adVarChar, adParamInput, 255, oFbUser.email)
.Append oSproc.CreateParameter("#IN_ip", adVarChar, adParamInput, 46, getUserIp())
.Append oSproc.CreateParameter("#IN_first_name", adVarChar, adParamInput, 255, oFbUser.first_name)
.Append oSproc.CreateParameter("#IN_last_name", adVarChar, adParamInput, 255, oFbUser.last_name)
.Append oSproc.CreateParameter("#IN_json_response_raw", adVarChar, adParamInput, 8000, oFbUser.rawJson)
.Append oSproc.CreateParameter("#IN_user_id", adVarChar, adParamInput, 255, oFbUser.id)
.Append oSproc.CreateParameter("#IN_name", adVarChar, adParamInput, 255, oFbUser.name)
End With
.Execute
End With
iNewExternalIdentityId = oSproc.parameters("#OUTPUT_originalId").Value 'grabbing the new EXTERNAL_IDENTITY.ID
Set oSproc = Nothing

Resources