dbTalk Databases Forums  

Re: DTS - loop through ADO recordset HELP!!!

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Re: DTS - loop through ADO recordset HELP!!! in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: DTS - loop through ADO recordset HELP!!! - 09-03-2003 , 07:05 AM






OK Let's take a very simple example

I have a table in SQL Server that will hold my settings

CREATE TABLE GVPopVals
(
FileName varchar(200)
)

Ths will hold a set of filenames that i want to export to. I will be
exporting Authors.

INSERT GVPopVals VALUES('d:\MyFirstFile.txt')
INSERT GVPopVals VALUES('d:\MySecondFile.txt')


I want to populate each file with records from authors and I'm going to do
this by looping through a recordset.

Task 1 --Setup the recordset - ExecuteSQL task populates a Rowset GV (or use
an ADO connection)
Task 2 -- Active Script task. - Load the recordset into an array and set up
Global Variables
Task 3 -- Loop through the array keeping count of where we are by using a
GV. Populate the DtaSource property of the text file.
Task 4 -- DataPump (Betwen Authors and some text file which won't be used of
course)
Task 5 -- Loop around


Task 2

Function Main()

dim pkg
dim rs
dim BigString
dim arr
dim cn



set pkg = DTSGlobalVariables.Parent

set cn = CREATEOBJECT("ADODB.Connection")
set rs = CREATEOBJECT("ADODB.Recordset")
'set rs = DTSGlobalVariables("rValue").Value
cn.ConnectionString="Provider=SQLOLEDB;Data Source=.;Initial
Catalog=Pubs;Integrated Security=SSPI"
cn.Open

set rs = cn.Execute("SELECT filename from GVPopVals")

arr = rs.GetRows


DTSGlobalVariables("arrValues").Value = arr
DTSGlobalVariables("Counter").Value = 0
DTSGlobalVariables("MaxValueOfArray").Value = UBOUND(arr) + 1

rs.Close

Main = DTSTaskExecResult_Success
End Function

Task 3

Function Main()

dim stpPump
dim tskPump
dim pkg



set pkg = DTSGlobalVariables.Parent
set tFile = pkg.Connections("Dest")

set stpPump = pkg.Steps("DTSStep_DTSDataPumpTask_1")
set tskPump = pkg.Tasks("DTSTask_DTSDataPumpTask_1")


If ShouldIloop = True then
tFile.DataSource = DTSGlobalVariables("fName").Value
stpPump.DisableStep = False
Else
stpPump.DisableStep = True
End if

Main = DTSTaskExecResult_Success
End Function

Function ShouldILoop

dim arr
arr = DTSGlobalVariables("arrValues").Value

if DTSGlobalVariables("Counter").Value <=
Cint(DTSGlobalVariables("MaxValueOfArray").Value) THEN

DTSGlobalVariables("fName").Value =
arr(0,DTSGlobalVariables("Counter").Value)
DTSGlobalVariables("Counter").Value =
Cint(DTSGlobalVariables("Counter").Value) + 1
ShouldILoop = CBool(True)
Else
ShouldILoop = CBool(False)
End if
End Function


Task 5

Function Main()

dim pkg
dim stp

SET pkg = DTSGlobalVariables.Parent
SET stp = pkg.Steps("DTSStep_DTSActiveScriptTask_1")

stp.executionStatus = DTSStepExecStat_Waiting

Main = DTSTaskExecResult_Success
End Function


Whilst this may not be exactly what you are looking for it should give you a
starting point.





--

----------------------------
Allan Mitchell (Microsoft SQL Server MVP)
MCSE,MCDBA
www.SQLDTS.com
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org



"roxindigo" <member37650 (AT) dbforums (DOT) com> wrote

Quote:
I am in dire need of assistance and have spent days going through
articles and forum posts.



I have a DTS package that exports data to one or more text files based
on a table query. The package starts with an SQL Execute task that
returns a rowset as a global variable. The next task, that executes on
successful completion of the SQL execute task, is an ActiveX script task
that extracts global variable values that are used as input parameters
to the query that the Data Transformation task uses next. In addition,
each text file that is exported has a unique file name that is created
at runtime using the global variable information. The first ActiveX
script generates/assigns the first file name and the input parameters
for the data transformation task. The first text file exported is
perfect. The task that bombs on me is the second ActiveX script that
follows the Data Transformation step. This second ActiveX script is
supposed to generate the remaining text files by placing the Data
transformation task in a waiting state. All of the remaining text files
are named and generated, but are empty. I know that there is valid data
to export for these files because I tested the data transform for each
set of inputs. Following is a copy of my 2nd ActiveX script. Please help
me to understand why this isn't working. I'm new to both vbscript and
DTS, so forgive my obvious ignorance:



' Visual Basic ActiveX Script

'************************************************* *********************-
**

Option Explicit



Dim ado_mdsql_Connect

Dim strConnect_mdsql

Dim objresults



Function Main()



Dim oPKG

Dim osrcConnection

Dim odestConnection

Dim fsObject, sourcefile, destinationfile, fsTextFile

Dim cntr



set ado_mdsql_Connect = CreateObject("ADODB.Connection")

strConnect_mdsql = "Provider=SQLOLEDB;" & "Driver={SQL Server};" &
"Server=SERVER-
MD1;" & "Database=MDSQL;" & "Trusted_Connection=yes"



ado_mdsql_Connect.Open strConnect_mdsql



set objresults = CreateObject("ADODB.recordset")



Set objresults = DTSGlobalVariables("glbv_cust_invoice_nbrs").Value







Do until objresults.EOF





Set oPKG = DTSGlobalVariables.Parent

Set osrcConnection = oPKG.Connections("MDSQL Source")



objresults.MoveNext



If objresults.EOF then



DTSGlobalVariables("glbv_customer_nbr").Value = ""

DTSGlobalVariables("glbv_invoice_date").Value = ""

DTSGlobalVariables("glbv_invoice_nbr").Value =""

Main = DTSStepExecStat_Completed

else



DTSGlobalVariables("glbv_customer_nbr").Value =
objresults("Cust_No").Value

DTSGlobalVariables("glbv_invoice_nbr").Value =
objresults("Invoice_#").Value

DTSGlobalVariables("glbv_invoice_date").Value =
objresults("Inv_Date").Value





Set fsObject = CreateObject("Scripting.FileSystemObject")



destinationfile = "\\server-
md1\Raid5\Inetpub\ftproot\StoreEDI\"&objresults("C ust_No").Value&"i"&Ri-
ght(objresults("Invoice_#").Value,3)&".txt"

Set fsTextFile = fsObject.CreateTextFile(destinationfile,True)

fsTextFile.Close

Set fsObject = nothing





Set odestConnection = oPKG.Connections("Destination")

odestConnection.DataSource = destinationfile



DTSGlobalVariables("glbv_txtfile_path_and_name").V alue = destinationfile





oPKG.Steps("DTSStep_DTSDataPumpTask_1").ExecutionS tatus=
DTSStepExecStat_Waiting





end if



loop



Main = DTSTaskExecResult_Success

End Function





Also,

Many thanks to Darren Green for his articles on SQLDTS. They were
very helpful.


--
Posted via http://dbforums.com



Reply With Quote
  #2  
Old   
roxindigo
 
Posts: n/a

Default Re: DTS - loop through ADO recordset HELP!!! - 09-03-2003 , 09:11 AM







I'm overwhelmed by the level of assistance you have provided. It never
occurred to me to use an array. I will give this a try. Thanks!!!



Karen


--
Posted via http://dbforums.com

Reply With Quote
  #3  
Old   
Darren Green
 
Posts: n/a

Default Re: DTS - loop through ADO recordset HELP!!! - 09-03-2003 , 03:52 PM



In article <3324249.1062598318 (AT) dbforums (DOT) com>, roxindigo
<member37650 (AT) dbforums (DOT) com> writes
Quote:
I'm overwhelmed by the level of assistance you have provided. It never
occurred to me to use an array. I will give this a try. Thanks!!!

I think an array is making hard work of the job, especially as an array
can be a pain itself due to the strange type it uses and the fact that
the package cannot be saved in this state.

You can use the recordset object directly to drive a loop, and to save
messing around with ADO I would use the Execute SQL Task to get the RS
object into a global variable.

The script confused me, since you appeared to do
objresults.MoveNext
before reading any values, which means you will
always miss the first row. You also set a step to waiting, but then
continued with the loop, so it wouldn't get a chance to execute. I may
have misread this however.

If you want a nice sample of driving a loop of tasks from a SQL
recordset query then let me know and I'll mail it direct. I never got
round to writing it up for the site, but I will.

If however you are happy with your arrays then just ignore this and
stick with what works for now.


--
Darren Green (SQL Server MVP)
DTS - http://www.sqldts.com




Reply With Quote
  #4  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: DTS - loop through ADO recordset HELP!!! - 09-04-2003 , 01:21 AM



As Darren has said his example is a dream, he never actually said that I
did, and works a dream. I would also mail Darren and ask to look at his
package and than you may choose to go with his idea and use mine for
something else.

--

----------------------------
Allan Mitchell (Microsoft SQL Server MVP)
MCSE,MCDBA
www.SQLDTS.com
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org



"Darren Green" <darren.green (AT) reply-to-newsgroup-only (DOT) uk.com> wrote in
message news:q6u+HhUbSlV$Ew7u (AT) sqldts (DOT) com...
Quote:
In article <3324249.1062598318 (AT) dbforums (DOT) com>, roxindigo
member37650 (AT) dbforums (DOT) com> writes

I'm overwhelmed by the level of assistance you have provided. It never
occurred to me to use an array. I will give this a try. Thanks!!!


I think an array is making hard work of the job, especially as an array
can be a pain itself due to the strange type it uses and the fact that
the package cannot be saved in this state.

You can use the recordset object directly to drive a loop, and to save
messing around with ADO I would use the Execute SQL Task to get the RS
object into a global variable.

The script confused me, since you appeared to do
objresults.MoveNext
before reading any values, which means you will
always miss the first row. You also set a step to waiting, but then
continued with the loop, so it wouldn't get a chance to execute. I may
have misread this however.

If you want a nice sample of driving a loop of tasks from a SQL
recordset query then let me know and I'll mail it direct. I never got
round to writing it up for the site, but I will.

If however you are happy with your arrays then just ignore this and
stick with what works for now.


--
Darren Green (SQL Server MVP)
DTS - http://www.sqldts.com





Reply With Quote
  #5  
Old   
Darren Green
 
Posts: n/a

Default Re: DTS - loop through ADO recordset HELP!!! - 09-08-2003 , 01:57 PM





Here is the article-

How to loop through a global variable Rowset
(http://www.sqldts.com/default.aspx?298)

--
Darren Green (SQL Server MVP)
DTS - http://www.sqldts.com



Reply With Quote
  #6  
Old   
LilBill
 
Posts: n/a

Default Re: DTS - loop through ADO recordset HELP!!! - 12-17-2003 , 05:01 PM




Hello,
I read and looked at Darren's article "How to loop through a global
variable Rowset". I have a little different need. I need to get a
subset of data from a production server to a development server. The
data is relative based on a colum 'CFcKey'. I gathered 20,000 records
out of the primary table and want to use the pkey to gather the related
(n) records from a secondary table on the production server then send
the result into the sister table on the development server. I can think
of sending the result sets to files then uploading the file into the
development server. Can anybody think of a way to skip the 'sending
results to a file' step and get the data from Prod to Dev?
Any assistance is greatly appreciated, I am bout whipped!
Thanks
Bill


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Reply With Quote
  #7  
Old   
Casualinfoguy
 
Posts: n/a

Default Re: DTS - loop through ADO recordset HELP!!! - 12-17-2003 , 09:21 PM



why dont you create a linked server to the data you need, and perform a SELECT INTO from the linked server to the server you want to insert into. You have to run the query on the server where you are inserting the data, and you have to have execute permissions on the server you are querying the data from. HTH

Cheers,

Casualinfoguy

----- LilBill wrote: -----


Hello,
I read and looked at Darren's article "How to loop through a global
variable Rowset". I have a little different need. I need to get a
subset of data from a production server to a development server. The
data is relative based on a colum 'CFcKey'. I gathered 20,000 records
out of the primary table and want to use the pkey to gather the related
(n) records from a secondary table on the production server then send
the result into the sister table on the development server. I can think
of sending the result sets to files then uploading the file into the
development server. Can anybody think of a way to skip the 'sending
results to a file' step and get the data from Prod to Dev?
Any assistance is greatly appreciated, I am bout whipped!
Thanks
Bill


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Reply With Quote
  #8  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: DTS - loop through ADO recordset HELP!!! - 12-18-2003 , 02:48 AM



Are both tables on the Production Server? It sounds from your description
like they are. Can you not issue a standard Query ? If I have
misunderstood could you explain more (How many servers are involved etc)



--

----------------------------
Allan Mitchell (Microsoft SQL Server MVP)
MCSE,MCDBA
www.SQLDTS.com
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org



"LilBill" <LBill (AT) NoSpam (DOT) com> wrote

Quote:
Hello,
I read and looked at Darren's article "How to loop through a global
variable Rowset". I have a little different need. I need to get a
subset of data from a production server to a development server. The
data is relative based on a colum 'CFcKey'. I gathered 20,000 records
out of the primary table and want to use the pkey to gather the related
(n) records from a secondary table on the production server then send
the result into the sister table on the development server. I can think
of sending the result sets to files then uploading the file into the
development server. Can anybody think of a way to skip the 'sending
results to a file' step and get the data from Prod to Dev?
Any assistance is greatly appreciated, I am bout whipped!
Thanks
Bill


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Reply With Quote
  #9  
Old   
LilBill
 
Posts: n/a

Default Re: DTS - loop through ADO recordset HELP!!! - 12-18-2003 , 09:11 AM



There are two servers, A (Production) B (Development).
Server A's primary table has well over 5 million records, with multiple
related records in each of the other 9 tables. I want to get about
20,000 of the primary tables records, then only the related records from
the other 9 tables from server A to B. I was attempting to use a DTS
package to faciliate the data moving processes.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Reply With Quote
  #10  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: DTS - loop through ADO recordset HELP!!! - 12-19-2003 , 02:07 AM



Then if all the 9 tables are in one place (server) then create a view of the
data. You then use the view(s) as the SourceObjectName for each of your
datapump(s).

--

----------------------------
Allan Mitchell (Microsoft SQL Server MVP)
MCSE,MCDBA
www.SQLDTS.com
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org



"LilBill" <LBill (AT) NoSpam (DOT) com> wrote

Quote:
There are two servers, A (Production) B (Development).
Server A's primary table has well over 5 million records, with multiple
related records in each of the other 9 tables. I want to get about
20,000 of the primary tables records, then only the related records from
the other 9 tables from server A to B. I was attempting to use a DTS
package to faciliate the data moving processes.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.