Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

Loop an exec command through all tables of a database

$
0
0

Hi all,

I am pretty new to SQL but i would like to run an exec command through all tables of a specific database.

Here is the command i use for a specifc table for the database which works fine (it just exports a table to a csv file):

exec xp_cmdshell 'bcp "SELECT * FROM dbo.table1" queryout C:\export\table1.csv -t, -c -S . -d database1 -T'

I have 25 tables in this database and i want to execute this command for each of them, i could just copy this command out 25 times and manually change the table names but am sure there is a more efficient way :)

I am thinking that i need a loop and have the table name as a parameter which changes as it cycles through each table and ends when it reaches the final table.

Thanks!


Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>