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 copy table data

Author  Topic 

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-06-08 : 18:57:16
Hi,
I've a table called Admin which contains id, login, password, name columns in SQL Server 2008

Since this is a newly created table, can any one tell me how to copy the table data into other machine during deployment?

Please note that I need to copy only this table's data. How can I do it in SQL Server 2008. Appreciate your help!

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-08 : 19:10:20
Vyas has an excellent script that can generate the INSERT statements for you: http://vyaskn.tripod.com/code/generate_inserts.txt

I typically use bcp.exe to import/export data though. You can also use SSIS or the import/export wizard available in SSMS.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-06-08 : 19:14:15
Can you please provide me syntax of using bcp for exporting table data of table tableA?

Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-08 : 19:34:19
It's a command-line executable (Start..Run..cmd):

bcp db1.dbo.tbl1 out C:\Dir1\File1.csv -c -t, -T -Sserver1\instance1 -r\r\n

-c means use character format (the file will be readable in a text program such as notepad).
-t is the field terminator, and I have specified comma.
-T means use Windows authentication. You can alternatively use -U along with -P instead of -T to use SQL authentication.
-S is the server information for the source server
-r is the row terminator. I specified line feed and carriage return.

To then import that file, you would do this:

bcp db1.dbo.tbl1 in C:\Dir1\File1.csv -c -t, -T -Sserver2\instanceB -r\r\n

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-08 : 19:36:26
By the way, you'll have bcp.exe on the client machine if you've got the SQL client tools installed such as Management Studio.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-06-08 : 19:41:15
Hi tkzier,
Thanks again for the info. However, let me explain my problem. When I export the data using bcp syntax which you gave me, it works fine. The file got created with records as well.

Now, I imported them as well but the data is not good for password field. This field is defined as nvarchar & contains encrypted password which might have byte encoding, etc. Now, how do I export it properly so that import would also be nice with good data??

Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-08 : 19:52:27
I'm not sure actually as I've never dealt with moving encrypted data. Try native format (-n) instead maybe that'll preserve the data.

bcp db1.dbo.tbl1 out C:\Dir1\File1.csv -n -t, -T -Sserver1\instance1 -r\r\n
bcp db1.dbo.tbl1 in C:\Dir1\File1.csv -n -t, -T -Sserver2\instanceB -r\r\n

I haven't used native format in a while though, so we may need to change the commands a little to get it to work. Try the above first and let me know how it goes.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-08 : 19:53:49
We could also use OPENQUERY or a linked server to move the data. I prefer linked server. Do you have one in place on server1 to point to server2 or one in place on server2 to point to server1? If you do, then it's as simple as this

INSERT INTO db1.dbo.tbl1 (...)
SELECT ...
FROM LinkedServerName.db1.dbo.tbl1

LinkedServerName typically is the same as the servername\instancename.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-06-08 : 20:24:56
Yup....Thanks a lot, Tara!!! the native option worked!!!!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-08 : 20:40:34
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -