Author |
Topic |
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2007-08-03 : 09:38:52
|
guys,I have following stored procedure which runs osql cmd to write to a file every next value of a cursor, but the problem is the file is overwritten for each value of the cursor. Is there any way to modify the stored proc to append values to the same outputfile. Basically the outfile contains the output only for last value of the cursor.ALTER PROCEDURE [dbo].[INSERTTEST2]ASBEGIN declare C_mf1 cursor for select tab_name from metaDECLARE @cmd varchar(2000)DECLARE @TAB_NAME VARCHAR(20)DECLARE @FETCHCOUNT INTset @tab_name = ''set @fetchcount = 0 open C_mf1 fetch next from C_mf1 into @tab_nameWHILE @@fetch_status<>-1BEGIN set @cmd = 'sqlcmd -E -dWatauga_Test -SNV-DEVSQL1\DAPHNE -Q"exec SP_GENERATE_INSERTS '+@TAB_NAME+'" -oc:\myTable.sql'select @cmd -- + '...'EXEC master.dbo.xp_cmdShell @cmdset @fetchcount=@fetchcount+1fetch next from C_mf1 into @tab_nameENDENDAny suggestions and inputs would helpThanks |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-08-04 : 21:44:43
|
Why not just use bcp (or osql) with the query rather than a cursor?If you must do this then output to a temp file then use a type command to append.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-08-06 : 07:43:13
|
The idea of using BCP or OSQL instead of a cursor is the proper way to go... that not withstanding...set @cmd = 'sqlcmd -E -dWatauga_Test -SNV-DEVSQL1\DAPHNE -Q"exec SP_GENERATE_INSERTS '+@TAB_NAME+'" >>c:\myTable.sql'--Jeff Moden |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-08-06 : 07:44:49
|
P.S. Hmmm... This looks very familiar... Pretty sure I also answered this on another forum for the same person.--Jeff Moden |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-08-06 : 12:00:10
|
sqlcmd is v2005.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2007-08-06 : 17:13:47
|
IS there any way when my stored proc runs the following command that it creates a new file mytable.sql after deleting the old one. 'sqlcmd -E -dWatauga_Test -SNV-DEVSQL1\DAPHNE -Q"exec SP_GENERATE_INSERTS '+@TAB_NAME+'" >>c:\myTable.sql'Right now it is writing to the same file.Any suggestions and inputs would helpThanks |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-08-06 : 18:01:01
|
>c:\myTable.sql> is new, >> is append.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2007-08-06 : 21:44:09
|
Thank you for your reply tkizer.From the stored proc definition you would see that I pass cursor values into osql commmand. If I use '>' it creates new file, but it loads data only for the last value of the cursor. Whereas '>>' appends the data for all values of the cursor but it works on the same output file with out creating the new file when it runs.Is there any solution ? can I run a windows command to delete the file from the stored procedure before it executes the osql command.Thanks |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-08-06 : 22:25:07
|
[ > ] will erase a file if it exists and then create a new one. [ >> ] will append to a file if it exists and still create a new one if it does not. So... just before you run the cursor with the append output ">>", why don't you just run another dip to the operating system that deletes a file if it exists? ;)ALTER PROCEDURE [dbo].[INSERTTEST2]ASBEGINdeclare C_mf1 cursor forselect tab_name from metaDECLARE @cmd varchar(2000)DECLARE @TAB_NAME VARCHAR(20)DECLARE @FETCHCOUNT INTset @tab_name = ''set @fetchcount = 0set @cmd = 'DEL c:\myTable.sql /q' --/q is the "quiet" or "promptless" mode of the commandEXEC master.dbo.xp_cmdShell @cmdopen C_mf1 fetch next from C_mf1 into @tab_nameWHILE @@fetch_status<>-1BEGINset @cmd = 'sqlcmd -E -dWatauga_Test -SNV-DEVSQL1\DAPHNE -Q"exec SP_GENERATE_INSERTS '+@TAB_NAME+'" >> c:\myTable.sql'select @cmd -- + '...'EXEC master.dbo.xp_cmdShell @cmdset @fetchcount=@fetchcount+1fetch next from C_mf1 into @tab_nameENDEND--Jeff Moden |
 |
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2007-08-07 : 08:10:13
|
Thanks again Jeff that works just fine.I didnt know that we can os commands through osql.Thanks |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-08-07 : 08:17:52
|
You're not really... the DEL is not done using OSQL... It's done using xp_CmdShell just like OSQL. The > and >> are just part of the command line and not really part of OSQL at all.--Jeff Moden |
 |
|
|