Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to dump data to .sql text file?

Author  Topic 

gannina
Starting Member

5 Posts

Posted - 2008-03-07 : 15:51:30
Hi, I just started using SQL Server 2005 and I'm trying to find out how to do a sql dump on a table, but this is proving more challenging then it should be.

I usually use mysql with a program called navicat, and all you do is right click on the table and select dump... Inserting the data back in is just as simple. I have also used sql server 2000 awhile back and I know there was a dumping utility for it.

Can someone point me in the right direction on how to dump data to a .sql file and reinsert that data? Thanks!

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-07 : 16:05:15
you mean generate the insert statements?


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

gannina
Starting Member

5 Posts

Posted - 2008-03-07 : 16:47:30
Generate the table structure and the insert statements
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-07 : 17:18:38
well management studio doesn't have insert script generation built in.
it does have scripting of db schema though.

for generating insert statements:

you can use this if you want a T-SQL way
http://vyaskn.tripod.com/code.htm#inserts

or you can use SSMS Tools Pack add-in for Management Studio 2005
you can download it from the link below.


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-03-07 : 17:50:04
You could use this script that I think I stole from Peso (I dont remember for sure):
USE AdventureWorks2000

declare @TABLE_NAME sysname
declare @TABLE_SCHEMA sysname
declare @sql nvarchar(4000)
declare @table_alias sysname

set @TABLE_NAME = 'Address'
set @TABLE_SCHEMA = 'dbo'
set @table_alias = 'a'
set @sql = ''

select
@sql = @sql+
case
when a.ORDINAL_POSITION = 1
then 'select'+char(13)+Char(10)
else ''
end+
char(9)+
@table_alias+'.'+
quotename(a.COLUMN_NAME)+
case
when a.ORDINAL_POSITION = b.LAST_ORDINAL_POSITION
then ''
else ',' end+
char(13)+Char(10)+
case
when a.ORDINAL_POSITION = b.LAST_ORDINAL_POSITION
then 'into'+char(13)+Char(10)+char(9)+'#temp'+char(13)+Char(10)+
'from'+char(13)+Char(10)+char(9)+
quotename(@TABLE_SCHEMA)+'.'+quotename(@TABLE_NAME)+' '+@table_alias
else ''
end
from
INFORMATION_SCHEMA.COLUMNS a
cross join
(
select
LAST_ORDINAL_POSITION = max(bb.ORDINAL_POSITION)
from
INFORMATION_SCHEMA.COLUMNS bb
where
bb.TABLE_SCHEMA = @TABLE_SCHEMA and
bb.TABLE_NAME = @TABLE_NAME
) b
where
a.TABLE_SCHEMA = @TABLE_SCHEMA and
a.TABLE_NAME = @TABLE_NAME
order by
a.ORDINAL_POSITION

--exec (@sql)
select [@sql] = @sql
Go to Top of Page

gannina
Starting Member

5 Posts

Posted - 2008-03-07 : 18:34:41
Thanks a lot for the help! I ended up installing the SMSS Tools and it works great. I'm just shocked that something like that isn't included in SQL Server by default, seems like an essential feature to me...
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-07 : 18:39:00
note that the ssms tools pack only scripts binary data up to 5 Mb.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page
   

- Advertisement -