Direct Data Load from external file to SQL Server Table and Vice Versa

## Using OpenRowSet we can load data from an xl file to a table
insert into dbo.Customers_xl
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
  'Excel 8.0;IMEX=1;HDR=NO;DATABASE=E:\sample_entry.xls', 'Select * from [Sheet1$]');


## Using BCP (bulk copy) we can write the output of a XPATH query to an xml file.
declare @sql varchar(8000)

SELECT @sql = 'bcp "SELECT TOP 5 * FROM AdventureWorks.HumanResources.Department for xml auto,elements" queryout c:\myXML.xml -c -t -T -S "MININT-CM4Q9R0\SQLSERVERDEV2008"'

exec master..xp_cmdshell @sql
before doing this The first thing you have to do is make sure xp_cmdshell is enabled

EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE 



ref:
http://www.mssqltips.com/sqlservertip/1633/simple-way-to-export-sql-server-data-to-text-files/
http://thiagsundar.wordpress.com/export-data-to-text-file/
http://www.simple-talk.com/sql/t-sql-programming/beginning-sql-server-2005-xml-programming/

Comments

Popular posts from this blog

SQL Server: Finding Query using SPID

Restoring Master, Model and MSDB from Netbackup