dbTalk Databases Forums  

Calling sqlcmd in a loop?

microsoft.public.sqlserver.tools microsoft.public.sqlserver.tools


Discuss Calling sqlcmd in a loop? in the microsoft.public.sqlserver.tools forum.



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

Default Calling sqlcmd in a loop? - 01-19-2009 , 06:07 PM






I am trying to develop a script(s) that can retrieve a list of .sql files
names to be executed and then call sqlcmd for each file name. The filename
retrieval part works fine but I am at a loss for how to loop and call sqlcmd
for the current file. All the examples I see are for a known set of
filenames to be executed. Currenttly I executing a SQLCMD file that does the
foloowing

DECLARE c_Scripts cursor FAST_FORWARD for SELECT ScriptName from @TestTable
OPEN c_Scripts
FETCH NEXT FROM c_Scripts INTO @ScriptName
WHILE @@FETCH_STATUS = 0
Begin

Select @ScriptName = quotename(@ScriptName,'"')
Print @ScriptName
:setvar SetVariablesScript @ScriptName
:r $(SetVariablesScript)

FETCH NEXT From c_Scripts INTO @ScriptName
End
CLOSE c_Scripts
DEALLOCATE c_Scripts

Doesn't work! A While loop doesn't work either.

I could do this in a windows for loop but if one sql file fails I would like
NOT to execute any others.

for %%X in (*.SQL) do SQLCMD -S ServerName -d DatabasesName -U UserName -P
UserPassword -I -i "%%X" >> ResultScript.txt


There must be someway to iterate through a list and execute the .sql file.
While Perl is an option to me I would rather keep this out of Perl. Is there
a way in TSQL/sqlcmd to do this?


Any help in that direction would be greatly appreciated!
RJ


Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Calling sqlcmd in a loop? - 01-20-2009 , 05:01 PM






RJ (RJ (AT) discussions (DOT) microsoft.com) writes:
Quote:
DECLARE c_Scripts cursor FAST_FORWARD for SELECT ScriptName from
@TestTable
OPEN c_Scripts
FETCH NEXT FROM c_Scripts INTO @ScriptName
WHILE @@FETCH_STATUS = 0
Begin

Select @ScriptName = quotename(@ScriptName,'"')
Print @ScriptName
:setvar SetVariablesScript @ScriptName
:r $(SetVariablesScript)

FETCH NEXT From c_Scripts INTO @ScriptName
End
CLOSE c_Scripts
DEALLOCATE c_Scripts

Doesn't work! A While loop doesn't work either.
That's right, it doesn't work, and it can't work. SQLCMD is a client,
and cannot be controlled from SQL Server. The only way do could do
the loop with T-SQL is to kick off the SQLCMD script through xp_cmdshell.

Quote:
I could do this in a windows for loop but if one sql file fails I would
like NOT to execute any others.

for %%X in (*.SQL) do SQLCMD -S ServerName -d DatabasesName -U UserName
-P UserPassword -I -i "%%X" >> ResultScript.txt
I'm not too good at BAT files (for reasons that soon will prevail), but
does it not give you the option to check %ERRORLEVEL% and exit the loop?

Quote:
There must be someway to iterate through a list and execute the .sql file.
While Perl is an option to me I would rather keep this out of Perl.
Hrpmf! Perl is a great tool for this sort of stuff. I would definitely
use Perl. And Perl is definitely a better choice than runing a
T-SQL scripts that calls SQLCMD through xp_cmdshell.

If you want to stick with things that comes in the box, Powershell
could be an option. It may not be in your box, but it ships with SQL 2008,
and I guess with Windows 2008 as well. And as far as I down, it's
available for download.



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx



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.