473,883 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looping through a recordser in a stored procedure

I have a table [myOrders] with three columns. One of the columns
contains text output data [myText] nvarchar(500), one of them contains
a filename [myFileName] nvarchar(50), one of the columns is a bit to
record if it has been output yet[isOutput] bit default value = 0.

I am creating a SQL Agent job that needs to look at a recordset of
[myOrders] where [isOutput] = 0 and create a seperate text file for
each row using [myFileName] as the filename.
Then I need to mark [isOutput] of each record in [myOrders] as 1.

Ok, so that's the task...

What I'm thinking is I construct a stored procedure that starts with a
select statement:

Create PROCEDURE JustDoIt
AS
set nocount on

SELECT
myText, myFileName
FROM
myOrders
WHERE
(isOutput = 0)

THEN I USE BCP to create the file looping through the recordset above.
THIS IS THE PART I AM CLUELESS ABOUT!

/* NEED TO LOOP HERE */

DECLARE @ReturnCode int
DECLARE @ExportCommand varchar(255)
DECLARE @FileName nvarchar(50)
DECLARE @FileText nvarchar (500)

SELECT @FileName = myFileName

/*THIS SYNTAX IS PROBABLY TOTALLY OUTA WHACK:)

SET @ExportCommand =
'BCP @FileText queryout "c:\' +
@FileName +
'" -T -c -S ' + @@SERVERNAME
EXEC @ReturnCode = master..xp_cmds hell @ExportCommand

/* NEED TO EXIT LOOP HERE */
Then I update all records in [myOrders] to 1

BEGIN TRANSACTION

UPDATE
myOrders
SET isOutput = 1
WHERE
(isOutput = 0)

/* err checking here */

COMMIT TRANSACTION

I'm hoping someone can help me construct this.
Thanks,
lq
Jul 20 '05 #1
7 14698
Lauren Quantrell (la************ *@hotmail.com) writes:
I have a table [myOrders] with three columns. One of the columns
contains text output data [myText] nvarchar(500), one of them contains
a filename [myFileName] nvarchar(50), one of the columns is a bit to
record if it has been output yet[isOutput] bit default value = 0.

I am creating a SQL Agent job that needs to look at a recordset of
[myOrders] where [isOutput] = 0 and create a seperate text file for
each row using [myFileName] as the filename.
I'm glad to see that you are exploring Agent!

Did you ever consider of making it an Active-X job step? You could then
use VBscript for the task, and it may be easier to write files from
VBscript. (Then again, I have never used VB-script myself.) You could
also write a command-line program in whatever language you fancy, and
run the step as as CmdExec.

You could do this in T-SQL, by setting up a cursor, but since you
would have to fork out with xp_cmdshell for BCP for each file, there
may be a performance cost. VBscript (or whatever language) would be
more effective.

The cusror solution is fairly straightforward :

DECLARE your_cur INSENSITIVE CURSOR FOR SELECT
myText, myFileName , OrderID FROM
myOrders
WHERE
(isOutput = 0)
OPEN your_cur

WHILE 1 = 1
BEGIN
FETCH your_cur INTO @FileText, @myFileName, @orderID
IF @@fetch_status <> 0
BREAK
SET @ExportCommand =
'BCP @FileText queryout "c:\' +
@FileName +
'" -T -c -S ' + @@SERVERNAME
EXEC @ReturnCode = master..xp_cmds hell @ExportCommand


IF @ReturnCode = 0
UPDATE myOrders SET isOutput = 1 WHERE orderID= @orderID

END

DEALLOCATE your_cur

Now, as it written above, it assumes that @FileText is a query, but
from your narrative, I believe it is just a file. You could make it
a query by

SELECT @filetext = 'SELECT ''' + replace(@filete xt, '''', '''''') + ''''

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Erland,
Thanks very much for this code. Hopefully I can construct this from
this foundation.
Question though, can I avoid using cursors by taking advantage of BCP
parameters firstrow, lastrow and batchsize parameters so that I output
one row at a time of a variable row recordset??? This would be my
first option.
Thanks,
LQ
Erland Sommarskog <es****@sommars kog.se> wrote in message news:<Xn******* **************@ 127.0.0.1>...
Lauren Quantrell (la************ *@hotmail.com) writes:
I have a table [myOrders] with three columns. One of the columns
contains text output data [myText] nvarchar(500), one of them contains
a filename [myFileName] nvarchar(50), one of the columns is a bit to
record if it has been output yet[isOutput] bit default value = 0.

I am creating a SQL Agent job that needs to look at a recordset of
[myOrders] where [isOutput] = 0 and create a seperate text file for
each row using [myFileName] as the filename.


I'm glad to see that you are exploring Agent!

Did you ever consider of making it an Active-X job step? You could then
use VBscript for the task, and it may be easier to write files from
VBscript. (Then again, I have never used VB-script myself.) You could
also write a command-line program in whatever language you fancy, and
run the step as as CmdExec.

You could do this in T-SQL, by setting up a cursor, but since you
would have to fork out with xp_cmdshell for BCP for each file, there
may be a performance cost. VBscript (or whatever language) would be
more effective.

The cusror solution is fairly straightforward :

DECLARE your_cur INSENSITIVE CURSOR FOR
SELECT
myText, myFileName

, OrderID
FROM
myOrders
WHERE
(isOutput = 0)


OPEN your_cur

WHILE 1 = 1
BEGIN
FETCH your_cur INTO @FileText, @myFileName, @orderID
IF @@fetch_status <> 0
BREAK
SET @ExportCommand =
'BCP @FileText queryout "c:\' +
@FileName +
'" -T -c -S ' + @@SERVERNAME
EXEC @ReturnCode = master..xp_cmds hell @ExportCommand


IF @ReturnCode = 0
UPDATE myOrders SET isOutput = 1 WHERE orderID= @orderID

END

DEALLOCATE your_cur

Now, as it written above, it assumes that @FileText is a query, but
from your narrative, I believe it is just a file. You could make it
a query by

SELECT @filetext = 'SELECT ''' + replace(@filete xt, '''', '''''') + ''''

Jul 20 '05 #3
Erland, Thanks again for your time.
I want to do this without using a cursor, instead use select top 1 of
the recordset and loop through BCP until there are no more records.

Looks something like

SELECT top1 OrderID, myText, myFileName from tblOrders where isOutput
= 0
WHatI need here is to figure out how to extract the value of
myFileName and myText and pass it to the BCP Utility<<

Do until there's no more records in select statement above:

SET @ExportCommand = 'BCP myText queryout "c:\' + myFileName+ '" -T -c
-S ' + @@SERVERNAME
EXEC @ReturnCode = master..xp_cmds hell @ExportCommand

UPDATE myOrders SET isOutput = 1 WHERE orderID= @orderID

Loop
Sorry for being so dumb about this. I have never used this sort of
construction before.


Erland Sommarskog <es****@sommars kog.se> wrote in message news:<Xn******* **************@ 127.0.0.1>... Lauren Quantrell (la************ *@hotmail.com) writes:
I have a table [myOrders] with three columns. One of the columns
contains text output data [myText] nvarchar(500), one of them contains
a filename [myFileName] nvarchar(50), one of the columns is a bit to
record if it has been output yet[isOutput] bit default value = 0.

I am creating a SQL Agent job that needs to look at a recordset of
[myOrders] where [isOutput] = 0 and create a seperate text file for
each row using [myFileName] as the filename.


I'm glad to see that you are exploring Agent!

Did you ever consider of making it an Active-X job step? You could then
use VBscript for the task, and it may be easier to write files from
VBscript. (Then again, I have never used VB-script myself.) You could
also write a command-line program in whatever language you fancy, and
run the step as as CmdExec.

You could do this in T-SQL, by setting up a cursor, but since you
would have to fork out with xp_cmdshell for BCP for each file, there
may be a performance cost. VBscript (or whatever language) would be
more effective.

The cusror solution is fairly straightforward :

DECLARE your_cur INSENSITIVE CURSOR FOR
SELECT
myText, myFileName

, OrderID
FROM
myOrders
WHERE
(isOutput = 0)


OPEN your_cur

WHILE 1 = 1
BEGIN
FETCH your_cur INTO @FileText, @myFileName, @orderID
IF @@fetch_status <> 0
BREAK
SET @ExportCommand =
'BCP @FileText queryout "c:\' +
@FileName +
'" -T -c -S ' + @@SERVERNAME
EXEC @ReturnCode = master..xp_cmds hell @ExportCommand


IF @ReturnCode = 0
UPDATE myOrders SET isOutput = 1 WHERE orderID= @orderID

END

DEALLOCATE your_cur

Now, as it written above, it assumes that @FileText is a query, but
from your narrative, I believe it is just a file. You could make it
a query by

SELECT @filetext = 'SELECT ''' + replace(@filete xt, '''', '''''') + ''''

Jul 20 '05 #4
I realize there is a cost to using cursors, but since you're going
to launch the command shell for BCP on every record, the cost of the
cursor is probably insignificant. But if you insist....
Erland, Thanks again for your time.
I want to do this without using a cursor, instead use select top 1 of
the recordset and loop through BCP until there are no more records.

Looks something like
WHILE 1=1 BEGIN SELECT TOP 1 @OrderID=OrderI D,@MyText=myTex t,@MyFileName=m yFileName
from tblOrders where isOutput = 0

IF @@ROWCOUNT = 0 BREAK
SET @ExportCommand = 'BCP '+@myText+' queryout "c:\' + @myFileName+ '" -T -c
-S ' + @@SERVERNAME
EXEC @ReturnCode = master..xp_cmds hell @ExportCommand

UPDATE myOrders SET isOutput = 1 WHERE orderID= @orderID
END -- end of loop

Jul 20 '05 #5
[I'm answering to Jim's post, since Lauren's has not made it here yet. My
ISP reconfigured the news server and it took them two days to realize
that it was no longer working.]

Jim Geissman (ji**********@c ountrywide.com) writes:
I realize there is a cost to using cursors, but since you're going
to launch the command shell for BCP on every record, the cost of the
cursor is probably insignificant. But if you insist....
Why Laruen does not want to use a cursor I don't know, but since he
has to iterate anyway, cursor is the best solution for iteration anyway.

Say that you instead do:
SELECT TOP 1 @OrderID=OrderI D,@MyText=myTex t,@MyFileName=m yFileName
from tblOrders where isOutput = 0


If there is no index on isOutput (and one would not expect that),
and the table is huge, this can be very expensive. I have no benchmarks,
but I would suggest that for an iteration a cursor is the best way to
go, although it depends on the cursor type. FAST_FORWARD may be the
fastest, but I always use INSENSITIVE.

Anyway, Laruen should not do this in T-SQL at all, he should use VBscript
or similar as I suggested in my previous post. It will be easier to
program and execute faster.

Also, it occurred to me that if tblOrders.myTex t is the text that is
to be written to the file, the QueryOut thing will not work, since the
newlines in myText causes problem. Then again if the query for queryout is

'SELECT myText FROM tblOrders = ' + str(@orderid)

that will work.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #6
Erland (and Jim)
You guys have gone above and beyond the call of duty in your response
to my problem and because of your repsonses I have been able to roll
this out. Thanks a million, and I have only one correction for
Erland's post - substitute "she" for "he" and it's 100% correct!
Thanks,
lq
Erland Sommarskog <es****@sommars kog.se> wrote in message news:<Xn******* *************** @127.0.0.1>...
[I'm answering to Jim's post, since Lauren's has not made it here yet. My
ISP reconfigured the news server and it took them two days to realize
that it was no longer working.]

Jim Geissman (ji**********@c ountrywide.com) writes:
I realize there is a cost to using cursors, but since you're going
to launch the command shell for BCP on every record, the cost of the
cursor is probably insignificant. But if you insist....


Why Laruen does not want to use a cursor I don't know, but since he
has to iterate anyway, cursor is the best solution for iteration anyway.

Say that you instead do:
SELECT TOP 1 @OrderID=OrderI D,@MyText=myTex t,@MyFileName=m yFileName
from tblOrders where isOutput = 0


If there is no index on isOutput (and one would not expect that),
and the table is huge, this can be very expensive. I have no benchmarks,
but I would suggest that for an iteration a cursor is the best way to
go, although it depends on the cursor type. FAST_FORWARD may be the
fastest, but I always use INSENSITIVE.

Anyway, Laruen should not do this in T-SQL at all, he should use VBscript
or similar as I suggested in my previous post. It will be easier to
program and execute faster.

Also, it occurred to me that if tblOrders.myTex t is the text that is
to be written to the file, the QueryOut thing will not work, since the
newlines in myText causes problem. Then again if the query for queryout is

'SELECT myText FROM tblOrders = ' + str(@orderid)

that will work.

Jul 20 '05 #7
Lauren Quantrell (la************ *@hotmail.com) writes:
You guys have gone above and beyond the call of duty in your response
to my problem and because of your repsonses I have been able to roll
this out. Thanks a million, and I have only one correction for
Erland's post - substitute "she" for "he" and it's 100% correct!


Glad to hear that you got it working! And my cheeks blossom in embarrassment
for calling you a man. I remember it to next time.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4090
by: Roy Adams | last post by:
Hi posting again because no answer to previous.. tring to loop through a recordset and update a record, thing is it only updates the first record in the table rather than searching through the entire table or records returned, and updating a record if certain criteria is met. shouldn't the while loop do this? I know my syntax must be wrong, but difficult to work out how or where table = String(Request.Cookies("table"));
1
3727
by: Beau | last post by:
Hi all, thanks in advance. Ok, heres the story. What is happening...... -------------------------------- I've got an ASP page that loops. It loops in order to get data in different, sequential date ranges. I.E. from 9/1/2000 - 10/1/2000 then 10/1/2000 - 11/1/2000 etc etc etc. It calls SPs using the 2 dates and an integer used for companyid reference.
11
2634
by: Lyle Fairfield | last post by:
The stored procedure script below is an example of how looping, case statements and output parameters can be used in MS-SQL stored procedures to accomplish things for which we may have had to use VBA code, or automation, in the JET world. Some who have not yet worked with MS-SQL may be interested. The script is indented in reality but I have aligned it all left to make reading easier, (I hope). The Sproc takes an Account ID, an...
5
2070
by: Brian Conway | last post by:
I desperately need some help in coding how to have a completely ediable datagrid be able to loop through each row and insert into a database table. I am using a stored procedure in the database to do the insert, so I think I just need to figure out how to get it to loop through all the rows on the grid, and update from there.
2
5470
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
13
5034
by: JayCallas | last post by:
I know this question has been asked. And the usual answer is don't use cursors or any other looping method. Instead, try to find a solution that uses set-based queries. But this brings up several questions / senarios: * I created several stored procedures that take parameters and inserts the data into the appropriate tables. This was done for easy access/use from client side apps (i.e. web-based).
5
1499
by: Jeff | last post by:
Hey gang. I have a script that gets stats from a mssql db, and then inserts those stats into a temp table. where i can work with them as i wish. the problem is it isn't looping through all the records, and i am not sure why. i know the script isn't perfect, as it is open to sql injection and stuff like that, but once the site is complete, i will be putting in checks and
1
2961
by: Alex | last post by:
Hello, I have a stored procedure that processes an individual file from a directory and archives it in a subdirectory.Now, the problem is, when i execute it , it will only process one file. What i want to do is to check to see if there are any files in the folder, and if there are , process them all, and once done, go to the next part in a DTS package, if there are no files, simply go to the next part in the DTS package. I tried an...
5
7545
by: g | last post by:
Hi Guys.. i know this might sound really really simple, but I'm kinda stuck..I have this form..which has a table (created from stored procedure values)..once the table is populated..i have some radio buttons (for each row of the table) and a main submit button. On clicking submit..i want to loop through the table..pick up the first <TDbeing the user ID and the value of the radio button clicked (currently I have my radio button ID set...
0
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11109
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10728
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7959
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3227
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.