How to loop through the results from "show tables"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
leetee
Posts: 1
Joined: 03 Mar 2009 08:34

How to loop through the results from "show tables"

#1 Post by leetee » 03 Mar 2009 08:42

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

cc2
Posts: 2
Joined: 24 Mar 2009 11:28

Post

#2 Post by cc2 » 24 Mar 2009 11:34

Just use:

Code: Select all

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#3 Post by avery_larry » 25 Mar 2009 09:59

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.

Post Reply