Stored procedures to script table data in SQL Server 2005
0
Q:
Stored procedures to script table data in SQL Server 2005
Are there any stored procedures or command line utilities that I can use to script out the data from a particular table in SQL Server 2005?
ASKED: Jan 5 2009  4:54 PM GMT
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
0
1850 pts.
0
A:
 RATE THIS ANSWER
+1
Click to Vote:
  •   1
  •  0
  • AddThis Social Bookmark Button
(kccrosser) I think the original answer was correct, assuming that the question term "to script out the data" meant "generate a complete set of Insert statements to reproduce the contents of a table". There isn't any native function that does this. The "sqlcmd" example below simply dumps the data, but a "script" should contain a fully qualified "Insert into <table> ( <column_list> ) values ( <value_list> );" statement for each row of data.






I disagree with the below answer, there is a native utility that can be used to script data in a Microsoft SQL Server (2005 and above) called "SQLCMD". In previous versions of SQL, this command was 'osql'. This can be used in interactive mode from a command prompt as well as used in scripting. See the below example, also, from a command prompt on your SQL server type 'sqlcmd -?' for a full list of the command parameters.

@echo off
sqlcmd -H "HOSTNAME of Server" -S ServerName\InstanceName -d Employees -U Userid -P Password -Q "SELECT * From Personal_Info" -o F:\Data\SQLTest.txt

Breaking this simple batch file down, it initiates sqlcmd, passes the hostname of the database server, defines the database server\sql instance (if an instance is defined, otherwise just the database server name), passes a simple SELECT * from Employees.Personal_Info table and redirects the output to a text file. If you need the output in unicode format, pass the -u flag in addition to the others. You can also use a previously saved sql query by using the -i flag instead of -Q.







There are some third party utilities which can do this for you. I believe that Red-Gate and Quest Software both have one.

There are no native tools that can be used to script out the data. You can export the data using the BCP command line utility or SSIS.
Last Answered: Feb 12 2009  6:41 PM GMT by Kccrosser   1850 pts.
Latest Contributors: Johro10   75 pts., Mrdenny   46795 pts.
0
0
Discuss This Answer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _



_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

mrdenny   46795 pts.  |   Feb 12 2009  8:09PM GMT

SQLCMD shouldn’t be used to export data as there is no guarantee that the data will be dumped in a format which is reimportable. This is because it is not a data export tool. It is a query tool. The data export tools would be the ones which I listed BCP and SSIS.

 

Msi77   800 pts.  |   Feb 12 2009  9:09PM GMT

I wrote sp which generate inserts for own usage :-)
It does not work with all data types, blobs for example.

 
0