Page 1 of 1

How to loop through the results from "show tables"

Posted: 03 Mar 2009 08:42
by leetee
Hi,

I am in the middle of writing a DOS batch file that will be used to loop through all tables in a database to truncate them. I have never used DOS before and have searched all over the internet but am struggling on how I would loop through the results from the "Show tables" results. I have read that DOS does not support arrays and could only loop through a list.

Can anyone help with how I would exactly do this?

Many thanks in advance

Post

Posted: 24 Mar 2009 11:34
by cc2
Just use:

Code: Select all

:start
some codes and stuff here
goto start
That will loop the script, if that's what you wanted :?:

Posted: 25 Mar 2009 09:59
by avery_larry
If you're generating a list somehow, you'll probably want to use the FOR command to do something to each item in the list. Like this:

If your list is in a file:

Code: Select all

for /f "delims=" %%a in (file.txt) do (
   echo The current line in the txt file is %%a.
   do whatever else you want using %%a as the line from the text file.
)


If your list is generated by another command, then you can either redirect that list to a file:

Code: Select all

sqlcmd (something that generates output) > file.txt


and then work with the for statement, or you can attempt to work directly with the output:

Code: Select all

for /f "usebackq delims=" %%a in (`sqlcmd "something here"`) do (
   echo Same as above %%a
)


Maybe that helps? Or perhaps you could be more clear on what you need.