Solved

DTS ActiveX Script Update within recordset

Posted on 2004-10-11
7
1,423 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now