![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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. |
|
SELECT myText, myFileName , OrderID FROM myOrders WHERE (isOutput = 0) |
|
SET @ExportCommand = 'BCP @FileText queryout "c:\' + @FileName + '" -T -c -S ' + @@SERVERNAME EXEC @ReturnCode = master..xp_cmdshell @ExportCommand |
#3
| |||
| |||
|
|
Lauren Quantrell (laurenquantrell (AT) hotmail (DOT) 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_cmdshell @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(@filetext, '''', '''''') + '''' |
#4
| |||
| |||
|
|
WHatI need here is to figure out how to extract the value of myFileName and myText and pass it to the BCP Utility |
|
Lauren Quantrell (laurenquantrell (AT) hotmail (DOT) 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_cmdshell @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(@filetext, '''', '''''') + '''' |
#5
| |||
| |||
|
|
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 TOP 1 @OrderID=OrderID,@MyText=myText,@MyFileName=myFile Name |
|
SET @ExportCommand = 'BCP '+@myText+' queryout "c:\' + @myFileName+ '" -T -c -S ' + @@SERVERNAME EXEC @ReturnCode = master..xp_cmdshell @ExportCommand UPDATE myOrders SET isOutput = 1 WHERE orderID= @orderID END -- end of loop |
#6
| |||
| |||
|
|
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.... |
|
SELECT TOP 1 @OrderID=OrderID,@MyText=myText,@MyFileName=myFile Name from tblOrders where isOutput = 0 |
#7
| |||
| |||
|
|
[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 (jim_geissman (AT) countrywide (DOT) 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=OrderID,@MyText=myText,@MyFileName=myFile Name 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.myText 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. |
#8
| |||
| |||
|
|
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! |
![]() |
| Thread Tools | |
| Display Modes | |
| |