?
Solved

DTS ActiveX Script Update within recordset

Posted on 2004-10-11
7
Medium Priority
?
1,438 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:
Brendt Hess earned 1000 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 32

Expert Comment

by:Brendt Hess
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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

777 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