Solved

DTS ActiveX Script Update within recordset

Posted on 2004-10-11
7
1,437 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

717 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