Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

VBscript - Parametrized SQL Insert Statement (CM11874)

$
0
0

Good day all,

I need to create a VBscript to perform the following:

  1. Gather the Supplier Name information from the Company DB, using a Stored Procedure
  2. Insert/Update the results into a temporary SQL table
  3. Repeat step 1 & 2 for 7 other Company DB’s

So far, I have been able to create a script which basically meets the requirements, but I don’t feel that I am using best practices and it is also limited to one Company DB. My current problems are:

  1. Information from the Company DB contains special characters which break the code when performing the SQL insert. I am currently concatenating a SQL insert script, which is not ideal. I believe that I need to create a parameterized insert statement for the dynamic information in the DB. Could you please assist me with the code that performs the parameterized insert statement?
  2. I currently first delete all records in the temporary table and then insert the results from the SP. I am not too sure what the best practice would be to rather update the temporary table instead of deleting all records and then inserting all results. There are 7 Company DB’s to connect to to gather the info from a SP and then insert the information into the temporary table. Would it be best to update this table or to delete all records and insert all the info from the Company DB’s? If updating is the best option, how should I structure this?
  3. I currently cannot execute the SP for more than 1 Company DB, as I get an error that states that parameters are already used (not the exact error message), so as a temporary measure, I have created a separate script for each Company DB. Could you please assist me with the code to execute the SP for each separate Company DB and to insert the results into the temporary table?

My code is as follows:

'This script is used to popluate the Supplier Info Temp Table, by performing the following:
'1) Delete all record in the TEMP DB
'2) Gather the Supplier name from the Holding Company DB
'3) Insert data into the Temp DB
'Ver. 1.2b
'Changes:
'Ver. 1.2b
'1) Added additional Companies
'Ver. 1.2
'1) Added seperate Company Details
'2) First Delete the table and then populate the values
'Ver. 1.1
'1) Change ContSring to Barden
Option Explicit
dim objConn, conStr, par1, par2, par3, par4, rs, result, command, fields, value, name, f, SupplierName, SupplierID, MyConn, SQL_query, SQL_query_Delete, KOEDOESPOORTconStr, TEMPDBConStr
set objConn = CreateObject("ADODB.command")
Const adParamInput = 1
Const adVarChar = 200
'1) Delete all record in the TEMP DB
Set MyConn = CreateObject("ADODB.Connection")
'Itec Test Connection String
'MyConn.Open ".....
'Barden Connection String
TEMPDBConStr = "....
MyConn.Open TEMPDBConStr
SQL_query_Delete = "Delete from Suppliers"
MyConn.Execute(SQL_query_Delete) 
'2) Gather the Supplier name from the Holding Company DB
'Itec Dev Connection String
'conStr = ".....
'Barden Connection String - LIVE - Connecting to HOLDINGS DB - ID = 1
conStr = "....
'Barden Connection String - LIVE - Connecting to KOEDOESPOORT DB - ID = 2
'conStr = "....
set par1 = CreateObject("ADODB.Parameter")
set par2 = CreateObject("ADODB.Parameter")
par1.Direction=adParamInput
par1.name="@Option"
par1.Size=50
par1.Type=adVarChar
par1.Value="0"
par2.Direction=adParamInput
par2.name="@CompanyID"
par2.Size=50
par2.Type=adVarChar
par2.Value="1"
With objConn
 .activeconnection = conStr
 .commandtype = 4
 .commandtext = "sp_AO_Select_GRV_Lups"
 .Parameters.Append par1 
 .Parameters.Append par2
 .Execute
End with
'3) Insert data into the Temp DB
'This section results with multiple records from the stored procedure
result="" 
set rs = CreateObject("ADODB.Recordset")
rs.open objConn
do until rs.eof
    suppliername = rs.Fields(1).Value
SQL_query = "Insert into Suppliers ([Supplier Name], [Company ID], [Company Name]) Values ('" & suppliername & "', '1', 'Holdings')"
MyConn.Execute(SQL_query) 
    rs.MoveNext
loop
RS.Close 
set rs=nothing

Thank you,

Any assistance will be appreciated.

Regards,


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>