SQL statement Import and Export Daquan (collections, favorites) SQL statement Import and Export Daquan, including Excel, access --
/****** Export to excel
Exec master.. xp_mongoshell 'bcp settledb. DBO. shanghu out c: \ temp1.xls-C-Q-s "gnetdata/gnetdata"-U "sa"-P ""'
/*********** Import Excel
Select *
From OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = "C: \ test.xls"; user id = admin; Password =; extended properties = Excel 5.0 ')... xactions
Select cast (subject number as numeric (255) as nvarchar () + ''converted alias
From OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = "C: \ test.xls"; user id = admin; Password =; extended properties = Excel 5.0 ')... xactions
/** Import a text file
Exec master .. xp_mongoshell 'bcp "dbname .. tablename" in C: \ dt.txt-C-sservername-USA-ppassword'
/** Export a text file
Exec master .. xp_mongoshell 'bcp "dbname .. tablename" out c: \ dt.txt-C-sservername-USA-ppassword'
Or
Exec master .. xp_mongoshell 'bcp "select * From dbname .. tablename" queryout c: \ dt.txt-C-sservername-USA-ppassword'
Export to TXT text, separated by commas
Exec master.. xp_mongoshell 'bcp "database name... table name" out "D: \ tt.txt"-c-t,-U sa-P password'
Bulk insert database name... table name
From 'C: \ test.txt'
With (
Fieldterminator = ';',
Rowterminator = '\ N'
)
--/* Dbase iv File
Select * from
OpenRowSet ('Microsoft. Jet. oledb.4.0'
, 'Dbase IV; HDR = no; IMEX = 2; database = c: \ ', 'select * from [customer profile 4.dbf]')
--*/
--/* Dbase iii File
Select * from
OpenRowSet ('Microsoft. Jet. oledb.4.0'
, 'Dbase III; HDR = no; IMEX = 2; database = c: \ ', 'select * from [customer profile 3.dbf]')
--*/
--/* FoxPro Database
Select * From OpenRowSet ('msdasql ',
'Driver = Microsoft Visual FoxPro driver; sourcetype = DBF; sourcedb = c :\',
'Select * from [AA. DBF] ')
--*/
/************** Import the DBF File ****************/
Select * From OpenRowSet ('msdasql ',
'Driver = Microsoft Visual FoxPro driver;
Sourcedb = E: \ vfp98 \ data;
Sourcetype = DBF ',
'Select * from customer where country! = "USA" order by country ')
Go
/**************** Export to DBF ***************/
If you want to export data to the generated structure (that is, the existing table) FoxPro table, you can directly use the following SQL statement
Insert into OpenRowSet ('msdasql ',
'Driver = Microsoft Visual FoxPro driver; sourcetype = DBF; sourcedb = c :\',
'Select * from [AA. DBF] ')
Select * from table
Note:
Sourcedb = c: \ specifies the folder where the Foxpro table is located
AA. DBF specifies the name of The FoxPro table.
********************/
Insert into OpenRowSet ('Microsoft. Jet. oledb.4.0 ',
'X: \ A. mdb '; 'admin'; '', table A) Select * from database name... Table B
/************* Import access ********************/
Insert into B Table selet * From OpenRowSet ('Microsoft. Jet. oledb.4.0 ',
'X: \ A. mdb '; 'admin'; '', Table)
* ********************* Import XML files
Declare @ IDOC int
Declare @ Doc varchar (1000)
-- Sample XML document
Set @ Doc ='
<Root>
<Customer cid = "C1" name = "Janine" city = "issaquah">
<Order OID = "O1" date = "1/20/1996" amount = "3.5"/>
<Order OID = "O2" date = "4/30/1997" amount = "13.4"> customer was very satisfied
</Order>
</Customer>
<Customer cid = "C2" name = "Ursula" city = "oelde">
<Order OID = "O3" date = "7/14/1999" amount = "100" note = "wrap it blue
White red ">
<Urgency> important </urgency>
Happy customer.
</Order>
<Order OID = "O4" date = "1/20/1996" amount = "10000"/>
</Customer>
</Root>
'
-- Create an internal representation of the XML document.
Exec sp_xml_preparedocument @ IDOC output, @ Doc
-- Execute a SELECT statement using openxml rowset provider.
Select *
From openxml (@ IDOC, '/root/customer/order', 1)
With (OID char (5 ),
Amount float,
Comment ntext 'text ()')
Exec sp_xml_removedocument @ IDOC
/******************* Guide the entire database **************** *****************************/
Stored procedure implemented with BCP
/*
Stored Procedure for Data Import/Export
You can import/export the entire database or a single table based on different parameters.
Call example:
-- Export call example
---- Export a single table
Exec file2table 'zj', '','', 'xzkh _ sa .. region information', 'c: \ zj.txt ', 1
---- Export the entire database
Exec file2table 'zj', '','', 'xzkh _ Sa', 'c: \ docman ', 1
-- Import call example
---- Import a single table
Exec file2table 'zj', '','', 'xzkh _ sa .. region information', 'c: \ zj.txt ', 0
---- Import the entire database
Exec file2table 'zj', '','', 'xzkh _ Sa', 'c: \ docman ', 0
*/
If exists (select 1 from sysobjects where name = 'file2table' and objectproperty (ID, 'isprocedure ') = 1)
drop procedure file2table
go
Create procedure file2table
@ servername varchar (200) -- server name
, @ username varchar (200) -- user name. If you use the NT authentication method, it is null ''
, @ password varchar (200) -- password
, @ tbname varchar (500) -- database. DBO. table name, if not specified :. DBO. table Name, all user tables of the database are exported
, @ filename varchar (1000) -- import/export path/export
, @ isout bit -- 1 is exported, 0: Import
as
declare @ SQL varchar (8000)
If @ tbname like '%. %. % '-- if the table name is specified, export a single table
begin
set @ SQL = 'bcp' + @ tbname
+ case when @ isout = 1 then 'out' else 'in' end
+ '"' + @ filename + '"/W'
+'/s' + @ servername
+ case when isnull (@ username, '') = ''then'' 'else'/U' + @ username end
+ '/P' + isnull (@ password ,'')
exec master .. xp_cmdshell @ SQL
end
else
begin -- export the entire database, define the cursor, and retrieve all user tables.
declare @ m_tbname varchar (250)
If right (@ filename, 1) <> '\' set @ filename = @ filename + '\'
Set @ m_tbname = 'Clare # TB cursor for select name from '+ @ tbname +' .. sysobjects where xtype = 'u '''
Exec (@ m_tbname)
Open # TB
Fetch next from # TB into @ m_tbname
While @ fetch_status = 0
Begin
Set @ SQL = 'bcp' + @ tbname + '...' + @ m_tbname
+ Case when @ isout = 1 then 'out' else' in 'end
+ '"'{@Filename}@m_tbname}'.txt"/W'
+ '/S' + @ servername
+ Case when isnull (@ username, '') = ''then'' 'else'/U' + @ username end
+ '/P' + isnull (@ password ,'')
Exec master .. xp_mongoshell @ SQL
Fetch next from # TB into @ m_tbname
End
Close # TB
Deallocate # TB
End
Go
/********************** Export to TXT ************* ***************************/
Intended Use
Select * into OpenDataSource (...) from OpenDataSource (...)
Import an Excel file to a text file
Suppose there are two columns in Excel, the first column is name, and the second column is a very row account (16 digits)
The exported bank accounts are divided into two parts: the first eight digits and the last eight digits.
If you want to insert the statement above, the text file must exist and have one line: name, bank account 1, bank account 2
Then you can use the following statement to insert
Note that the file name and directory are modified according to your actual situation.
Insert
OpenDataSource ('Microsoft. Jet. oledb.4.0'
, 'Text; HDR = yes; database = c :\'
)... [Aa # TXT]
--, Aa # txt)
--*/
Select name, bank account 1 = left (bank account, 8), bank account 2 = right (bank account, 8)
From
OpenDataSource ('Microsoft. Jet. oledb.4.0'
, 'Excel 5.0; HDR = yes; IMEX = 2; database = c: \ a.xls'
--, Sheet1 $)
)... [Sheet1 $]
If you want to directly insert and generate a text file, you need to use BCP
Declare @ SQL varchar (8000), @ tbname varchar (50)
-- First import the Excel table content to a global temporary table
Select @ tbname = '[# temp' + Cast (newid () as varchar (40) + ']'
, @ SQL = 'select name, bank account 1 = left (bank account, 8), bank account 2 = right (bank account, 8)
Into '+ @ tbname +' from
OpenDataSource (''microsoft. Jet. oledb.4.0''
, ''Excel 5.0; HDR = yes; IMEX = 2; database = c: \ a.xls''
)... [Sheet1 $]'
Exec (@ SQL)
-- Use bcp to export data from a global temporary table to a text file
Set @ SQL = 'bcp "'+ @ tbname +'" out "C: \ aa.txt"/s "(local)"/P ""/C'
Exec master .. xp_mongoshell @ SQL
-- Delete a temporary table
Exec ('drop table' + @ tbname)
The stored procedure of using bcp to import and export files to the database:
/* -- BCP-Import and Export of binary files
Supports import/export of image, text, and ntext Fields
Image is suitable for binary files; text and ntext are suitable for text data files.
Note: during import, all rows meeting the conditions will be overwritten.
During export, all rows that meet the conditions will also be exported to the specified file.
This stored procedure is only implemented using BCP
--2003.08 -----------------*/
/* -- Call example
-- Data export
Exec p_binaryio 'zj', '','', 'Acc _ demo data .. tb', 'img ', 'c: \ zj1.dat'
-- Data export
Exec p_binaryio 'zj', '','', 'Acc _ demo data .. tb', 'img ', 'c: \ zj1.dat', '', 0
--*/
If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [p_binaryio] ') and objectproperty (ID, n' isprocedure') = 1)
Drop procedure [DBO]. [p_binaryio]
Go
Create proc p_binaryio
@ servename varchar (30), -- server name
@ username varchar (30 ), -- username
@ password varchar (30), -- password
@ tbname varchar (500), -- database .. table name
@ fdname varchar (30), -- field name
@ fname varchar (1000), -- directory + file name, used/overwritten during processing: @ filename +. bak
@ TJ varchar (1000) = '', -- processing condition. for data import, if the condition contains @ fdname, specify the table name prefix
@ isout bit = 1 -- 1 (default ), 0 import
as
declare @ fname_in varchar (1000) -- BCP processes the response file name
, @ fsize varchar (20) -- file size to be processed
, @ m_tbname varchar (50) -- temporary table name
, @ SQL varchar (8000)
-- Obtains the size of the imported file.
If @ isout = 1
Set @ fsize = '0'
Else
Begin
Create Table # Tb (varchar (20), int size
, Created on varchar (10), created on varchar (20)
The last write operation date is varchar (10), and the last write operation time is varchar (20)
, Last access date varchar (10), last access time varchar (20), feature INT)
Insert into # TB
Exec master .. xp_getfiledetails @ fname
Select @ fsize = size from # TB
Drop table # TB
If @ fsize is null
Begin
Print 'file not found'
Return
End
End
-- Generate a data processing response File
Set @ m_tbname = '[# temp' + Cast (newid () as varchar (40) + ']'
Set @ SQL = 'select * into '+ @ m_tbname +' from (
Select null as type
Union all select 0 as prefix
Union all select '+ @ fsize +' as length
Union all select null as ended
Union all select null as format
)'
Exec (@ SQL)
Select @ fname_in = @ fname + '_ temp'
, @ SQL = 'bcp "'+ @ m_tbname +'" out "'+ @ fname_in
+ '"/S"' + @ servename
+ Case when isnull (@ username, '') = ''then''
Else '"/u"' + @ username end
+ '"/P"' + isnull (@ password, '') + '"/C'
Exec master .. xp_mongoshell @ SQL
-- Delete a temporary table
Set @ SQL = 'drop table' + @ m_tbname
Exec (@ SQL)
If @ isout = 1
begin
set @ SQL = 'bcp "select top 1' + @ fdname + 'from'
+ @ tbname + case isnull (@ TJ, '') when ''then''
else 'where' + @ TJ end
+ '"queryout"' + @ fname
+ '"/s"' + @ servename
+ case when isnull (@ username, '') = ''then''
else '"/u "' + @ username end
+ '"/P "' + isnull (@ password, '')
+ '"/I "' + @ fname_in + '"'
exec master .. xp_cmdshell @ SQL
end
else
begin
-- prepare a temporary table for data import
set @ SQL = 'select Top 0' + @ fdname + 'input'
+ @ m_tbname + 'from' + @ tbname
exec (@ SQL)
-- import data to a temporary table
set @ SQL = 'bcp "'+ @ m_tbname +'" in "'+ @ fname
+ '"/ s "'+ @ servename
+ case when isnull (@ username, '') = ''then''
else '"/u "' + @ username end
+ '"/P "' + isnull (@ password, '')
+ '"/I "' + @ fname_in + '"'
exec master .. xp_mongoshell @ SQL
-- import data to the official table
set @ SQL = 'update' + @ tbname
+ 'set' + @ fdname + '= B. '+ @ fdname
+ 'from' + @ tbname + 'A,'
+ @ m_tbname + 'B'
+ case isnull (@ TJ, '') when''then''
else 'where' + @ TJ end
exec (@ SQL)
-- Delete a temporary data processing table
Set @ SQL = 'drop table' + @ m_tbname
End
-- Delete the data processing response File
Set @ SQL = 'del '+ @ fname_in
Exec master .. xp_mongoshell @ SQL
Go
/** Import a text file
Exec master .. xp_mongoshell 'bcp "dbname .. tablename" in C: \ dt.txt-C-sservername-USA-ppassword'
As follows, no quotation marks are required.
Exec master .. xp_mongoshell 'bcp dbname .. tablename in C: \ dt.txt-C-sservername-USA-ppassword'
/** Export a text file
Exec master .. xp_mongoshell 'bcp "dbname .. tablename" out c: \ dt.txt-C-sservername-USA-ppassword'
Quotation marks are required for this sentence.