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
 export data to sql statement ?

Author  Topic 

pamyral_279
Posting Yak Master

143 Posts

Posted - 2007-01-02 : 11:15:39
I have table which include many records in it.
Example:
Table A (ID integer,Name nvarchar(30),Age text,Sex nvarchar(10))
ID======Name======Age======Sex
1 ======A=========24=======Female
2 ======B=========35=======Male
3 ......
My problems : I want to export entire data above to sql file which
have following format :
Insert into TableB(ID,Name,Age,Sex) values(1,'A','24','Female')
Insert into TableB(ID,Name,Age,Sex) values(2,'B','35','Male')
.....
Any one help me ?
Thank you very much.

nr
SQLTeam MVY

12543 Posts

Posted - 2007-01-02 : 11:20:53
Do you want to copy the data to another table
Insert into TableB(ID,Name,Age,Sex) select ID, Name, Age, Sex from TableA

or to a file - in which case have a look at bcp

master..xp_cmdshell 'bcp mydb..TableA out c:\TableA.txt -SMyserver -T -c -t,'


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-01-02 : 11:28:44
if the table has many records, I would recommend using bcp to export/import. it's much faster than generating and then executing 10m separate insert statements.

however if you really need the insert script, here are a few methods:

http://vyaskn.tripod.com/code/generate_inserts.txt
http://www.nigelrivett.net/SQLTsql/sp_CreateDataLoadScript.html
http://www.google.com/search?q=generate+insert+script+sql+server





www.elsasoft.org
Go to Top of Page

pamyral_279
Posting Yak Master

143 Posts

Posted - 2007-01-02 : 21:36:55
Thank all !
quote:

http://vyaskn.tripod.com/code/generate_inserts.txt


I perform command : sp_generate_inserts 'mytable'
How do i export result to file ?
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-01-02 : 22:50:22
Make use of OSQL command-line utility.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-02 : 23:09:56
Copy the result in QA to a text file.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

sudhirpayal
Starting Member

1 Post

Posted - 2007-03-23 : 17:44:13
Hi,
I want to insert specific rows.
I tried using
exec generate_inserts D_time,'','', from = (select * from D_time where time_id>20070000) but thats not working.
Has anybody any idea how to use the insert statements using where clause..
Go to Top of Page
   

- Advertisement -