[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1443
  • Last Modified:

DTS ActiveX Script Update within recordset

Hi experts,

I'm running a dts activex script that inserts each line of a query into a csv file and then emails the file.... i need to make the process update my database as it writes each line... i've been trying to use the following code - which aint working - can anyone suggest a way to do this?

CURRENT CODE (this is just the bit that deals with this part of the process)

          Set rs = cn.Execute("SELECT  a,b,c FROM customers WHERE a,b,c")

          if rs.bof or rs.eof then noOrders = true
          if not noOrders then

          strLine="" 'Initialize the variable for storing the filednames

         'Retrieve the values from the database and write into the database
          Do while Not rs.EOF
          strLine=""
         for each x in rs.Fields
         strLine= strLine & chr(34) & x.value & chr(34) & chr(44)
          next
          MyFile.writeline strLine

        if x.name = "b" then
          wN= x.value

'set the email flag in the orders database
set rsConnU3 = CreateObject("ADODB.Connection")
set rs_ordersU3 = CreateObject("ADODB.Recordset")

rsConnU3.Open = "Provider=SQLOLEDB.1;Data Source=x; Initial Catalog=y;uid=xxx;password=xxx"

rsSQLCmdTextU3 = "UPDATE customers SET d= CONVERT(CHAR(8), GETDATE(), 112) WHERE (b= wN)"
rs_ordersU3.Open rsSQLCmdTextU3, rsConnU3

          rs.MoveNext

      Loop
0
pjordanna
Asked:
pjordanna
  • 3
  • 2
  • 2
2 Solutions
 
arbertCommented:
Don't open a recordset, just execute against the connection object like you're doing when you open your first recordset. (this assumes the connections are the same):


rsSQLCmdTextU3 = "UPDATE customers SET d= CONVERT(CHAR(8), GETDATE(), 112) WHERE (b= wN)"
cn.execte (rsSQLCmdTextU3)

Also, instead of opening a recordset and looping, why don't you use OSQL, or BCP to create a CSV text file?  It looks like you can come up with a query that will perform the same logic.  You can also exeucte the UPDATE statement on all records that match the criteria instead of a record at a time...

Brett
0
 
pjordannaAuthor Commented:
hi Brett - many thanks for your answer - i've not come across osql or bcp, the connections are the same though - i'd like to use the setup I've got but i get a 'loop without do' error message when i try to execute the new code (in fact i think i got that before) - can you help at all?
0
 
Brendt HessSenior DBACommented:
Since you are using a recordset row-by-row, this may be one of the cases where performing the update through the recordset would be your most efficient answer.  However, you can modify your current code as well.

((Modify current))

You have the 'if x.name' line in the wrong position.  In addition, you should (as recommended) just execute your Update against the Connection object.  This should be:

      Set rs = cn.Execute("SELECT  a,b,c FROM customers WHERE a,b,c")

      if rs.bof or rs.eof then noOrders = true
      if not noOrders then

      Do while Not rs.EOF
          strLine=""
          wN = ""  ' or zero, or whatever is correct for not found.
         for each x in rs.Fields
             strLine= strLine & chr(34) & x.value & chr(34) & chr(44)
             if x.name = "b" then wN= x.value   ' <<<<<----- Within the loop!!
          next
          MyFile.writeline strLine

          'set the email flag in the orders database

          rsSQLCmdTextU3 = "UPDATE customers SET d= CONVERT(CHAR(8), GETDATE(), 112) WHERE (b= " & wN & ")" ' <<---- Append the variable's **value**
          cn.Execute rsSQLCmdTextU3

          rs.MoveNext
       Loop

This should do what you need.

A DO Without LOOP error usually indicates that you have an unclosed IF ... THEN ... ENDIF, or some similar structure.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
Brendt HessSenior DBACommented:
((Recordset))

To modify your current code to update through the recordset, you would add the 'd' field to your Recordset.open, and ensure that it opened in a read-write mode.  Then, when going through the recordset, this would be the simplest update:

      Set rs = New ADODB.Recordset
      rs.Open "SELECT  a,b,c, d  FROM customers  WHERE a,b,c", cn, adOpenDynamic, adLockOptimistic

      if rs.bof or rs.eof then
          noOrders = true
      Else
          Do while Not rs.EOF
              strLine=""
              for each x in rs.Fields
                  If x.Name <> "d" Then
                      strLine= strLine & chr(34) & x.value & chr(34) & chr(44)
                  End If
              next
              MyFile.writeline strLine

              'set the email flag in the orders database
              rs.Fields("d").Value = Format$(Date, "yyyymmdd")
              rs.Update

              rs.MoveNext
          Loop
     End If

Don't forget to close the RS before setting it to nothing.

BTW, the IF ... THEN ... ELSE statement that I mentioned above is probably the line:

       if x.name = "b" then
          wN= x.value

Since this is on two lines, it needs an End If
0
 
pjordannaAuthor Commented:
that's a real, real help - thank you so much

this is the code i now have - it's failing with an incorrect syntax error on the update line - i've tried single quotes, double quotes, no quotes, single & but still no joy...

        Do while Not rs.EOF
          strLine=""
          WN=""
         for each x in rs.Fields
         strLine= strLine & chr(34) & x.value & chr(34) & chr(44)
         if x.name="writeNo" then WN=x.value end if  
          next
          MyFile.writeline strLine


'set the email flag in the orders database


rsSQLCmdTextU3 = "UPDATE adjustments SET Mailed = CONVERT(CHAR(8), GETDATE(), 112) WHERE (sp_adjustments.writeNo = & wN & )"
cn.execute (rsSQLCmdTextU3)

          rs.MoveNext
0
 
arbertCommented:
Is writeno a number?  If so, this should work:

rsSQLCmdTextU3 = "UPDATE adjustments SET Mailed = CONVERT(CHAR(8), GETDATE(), 112) WHERE (sp_adjustments.writeNo ="  & wN & " )"

However, it looks like your WHERE clause is calling a stored proc?  If so, this isn't possible, you would have to use a function, or come up with another method--is sp_adjustments a proc?
0
 
pjordannaAuthor Commented:
thanks guys - a real help
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now