Solved

DTS ActiveX Script Update within recordset

Posted on 2004-10-11
7
1,435 Views
Last Modified: 2008-01-09
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
Comment
Question by:pjordanna
  • 3
  • 2
  • 2
7 Comments
 
LVL 34

Expert Comment

by:arbert
ID: 12279198
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
 

Author Comment

by:pjordanna
ID: 12280175
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
 
LVL 32

Accepted Solution

by:
bhess1 earned 250 total points
ID: 12280500
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 32

Expert Comment

by:bhess1
ID: 12280605
((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
 

Author Comment

by:pjordanna
ID: 12280827
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
 
LVL 34

Assisted Solution

by:arbert
arbert earned 250 total points
ID: 12281173
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
 

Author Comment

by:pjordanna
ID: 12285843
thanks guys - a real help
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question