November 7, 2011 at 11:28 pm
I want to backup a table with data in SQL Express 2005. How to generate a script with data?
"Here is a test to find out whether your mission in life is complete. If you're alive, it isn't. "
Richard Bach
November 8, 2011 at 3:02 am
have a look at the procedure here, it will generate the insert script with data for you.
http://vyaskn.tripod.com/code.htm#inserts"> http://vyaskn.tripod.com/code.htm#inserts
November 8, 2011 at 3:16 am
another way is the SSMS addin from http://www.ssmstoolspack.com/ also has the ability to generate insert statements from The SSMS GUI;
Lowell
November 8, 2011 at 3:22 am
1)select * into new_tablename from existing_tablename (this will create a new table with data same as in existing table)
2) insert into new_tablename select * from existing_tablename(this will work only if you have created a backup table of existing table having same structure)
November 8, 2011 at 4:20 am
Interesting.
I don't have time to develop a fully working script, but this will generate SELECT statements of the data from your table.
Note, it's reliant on the size of VARCHAR(MAX), so big tables won't work.
DECLARE @yourTableName VARCHAR(100)
SET @yourTableName = '' --Your table name here
DECLARE @sql AS VARCHAR(MAX)
SELECT @sql = COALESCE(@SQL + '+'',''+', '') + sql_code
FROM (SELECT 'QUOTENAME('+name+','''''''')' AS sql_code
FROM (SELECT name
FROM sys.columns
WHERE object_id = OBJECT_ID(@yourTableName)) a ) b
SELECT @sql = 'SELECT ''SELECT ''+ ' + @sql + ' + '' UNION ALL'' FROM ' + @yourTableName
IF object_id('tempdb..#resultHolder') IS NOT NULL
BEGIN
DROP TABLE #resultHolder
END
CREATE TABLE #resultHolder (ID INT IDENTITY, code VARCHAR(MAX))
INSERT INTO #resultHolder
EXEC (@SQL)
UPDATE #resultHolder
SET code = REPLACE(code,' UNION ALL','')
WHERE ID = (SELECT MAX(ID) FROM #resultHolder)
SET @sql = NULL
SELECT @sql = COALESCE(@SQL + ' ','') + code
FROM (SELECT code
FROM #resultHolder) a
You'd then use the SELECT statements held in @sql to insert into your new table.
Can I just note that there are many better ways of performing back-ups of specific tables.
November 9, 2011 at 7:12 pm
Why don't use old and reliable bcp utility?
You can export your table into file by something like
bcp Table out FileName.dat -S server -d database -n -T
and then import it back changing out to in.
That's all. Simple, isn't it?
Andrei.
November 9, 2011 at 8:43 pm
some good ideas above. The key is do you want the table backed up to move to another database, or stored outside of SQL Server or are you trying to copy the table inside SQL Server in case changes to the table need to be rolled back?
November 10, 2011 at 7:51 am
Along those same lines, I want to back up or copy table and data to restore on a different server in a different database. Any good suggestions here? It is also going from Server 2005 to Server 2008.
November 10, 2011 at 8:01 am
dave.chapman (11/10/2011)
Along those same lines, I want to back up or copy table and data to restore on a different server in a different database. Any good suggestions here? It is also going from Server 2005 to Server 2008.
just the data, or the script for the table, which might include indexes, foreign keys, default constraints, check constraints? that's one more thing to cosnider, aside from the data itself.
Lowell
November 10, 2011 at 8:04 am
Yes anything underneath the table.
November 10, 2011 at 8:16 am
dave.chapman (11/10/2011)
Along those same lines, I want to back up or copy table and data to restore on a different server in a different database. Any good suggestions here? It is also going from Server 2005 to Server 2008.
Import wizard is probably the easiest way.. you can also include contraints and keys in the options
November 10, 2011 at 8:26 am
Dave
This is what I would do to copy table from SQL 2005 box to SQL 2008 box
1) Script out the table in SQL 2005 and create the table on SQL 2008
2) Copy the data using Import/Export Wizard in SQL Server or use bcp
3) Script out the indexes as scripting the table will not script the indexes and create them on SQL 2008 box
November 10, 2011 at 8:30 am
Thanks. Believe that will work fine.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply