Access the answers to your technology questions today.
Subscribe Now
30-day free trial. Register in 60 seconds.
What Makes Experts Exchange Unique?
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.
Try it out and discover for yourself.
Subscribe Now
30-day free trial. Register in 60 seconds.
Join the Community
Give a Little. Get a Lot.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Join the Community
by: GoodangelPosted on 2007-04-30 at 09:05:41ID: 19002381
I have written a procedure for you which you can create in each of your databases. This procedure will enable all tables in a database to be exported using BCP by running a single command against the relevant database. For this procedure to work in your databases, just put the correct sa password in the procedure, as well as the output path, and run it in your database.
----------
CREATE PROCEDURE spBCPAllTables
@DatabaseName VARCHAR(255)
AS
DECLARE @TableName VARCHAR(255)
DECLARE @CopyString VARCHAR(255)
DECLARE @CopyStringCmd SYSNAME
DECLARE TableList CURSOR FOR
select Name from sysobjects where xtype = 'U'
OPEN TableList
FETCH NEXT FROM TableList INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @CopyString = 'bcp "' + @DatabaseName + '.dbo.' + @TableName + '" out "C:\bcpout\'+ @DatabaseName + '_' +@TableName+'.txt" -c -q -U"sa" -P"sapwd"'
SET @CopyStringCmd = CAST(@CopyString AS SYSNAME)
exec master..xp_cmdshell @CopyStringCmd
FETCH NEXT FROM TableList INTO @TableName
END;
CLOSE TableList
DEALLOCATE TableList
GO
--------
To execute the procedure, just use the following command
exec dbname.dbo.spBCPAllTables 'dbname'
the procedure will then export all the tables in database dbname to text files in the format dbname_tablename.txt
you can play around with this script to tweak if for your needs