SQL statement import export

xiaoxiao2021-04-01  219

SQL statement import export

/ ******* Export to Excel Exec master..xp_cmdshell '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 (CAST (Subject No. As Numeric (10, 2)) AS NVARCHAR (255)) '' 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 text files EXEC MASTER..XP_CMDSHELL 'BCP "DBNAME..TABLENAME" IN C: /DT.TXT -C -SSERVERNAME -USA-PPASSWORD'

/ ** Export text file EXEC MASTER..XP_CMDSHELL 'BCP "DBNAME..TABLENAME" OUT C: /DT.TXT -C -SSERVERNAME -USA-PPASSWORD' or EXEC MASTER HAINP_CMDSHELL 'BCP "SELECT * from DBNAME .. Tablename "Queryout C: /Dt.txt -c -sservername -usa -ppassword"

Export to TXT text, separate the exec master..xp_cmdshell 'bcp "with comma" library name .. Table name "D: /tt.txt" -c -t, -u sa -p password'

Bulk INSERT library name .. 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 Data 4. DBF] ') - * /

- / * DBASE III file Select * from OpenRowSet ('microsoft.jet.oledb.4.0', 'DBASE III; HDR = NO; IMEX = 2; Database = C: /', 'SELECT * FROM [Customer Information 3. DBF] ') - * /

- / * FoxPro Database Select * from OpenRowSet ('msdasql', 'driver = Microsoft Visual FoxPro Driver; SourceType = DBF; SourceDb = C: /', 'SELECT * from [aa.dbf]') - * /

/ ************* Import 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 a structure (ie, existing) 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

Description: Sourcedb = C: / Specifies the folder AA.DBF of the FoxPro table to specify the file name of the FoxPro table.

/ ************* Export to Access ******************* / INSERT INTO OPENROWSET ('Microsoft.jet.OleDb.4.0 ',' X: /a.mdb ';' admin ';' ', a table) SELECT * FROM database name ..b table

/ ************* Import Access ******************************* / INSERT INTO B SELET * from OpenRowSet ('Microsoft.jet .Oledb.4.0 ',' x: /a.mdb ';' admin ';' ', a table)

******************* Import XML file

Declare @idoc int declare @doc varchar (1000) --Sample XML Document Set @doc = ' Important Happy customer. '- 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

/ ****************************************************************************************************** ****************** /

Stored procedure implemented with BCP

/ * Implementing data import / export stored procedures can implement import / export of the entire database / single table call example: - Export Call Example ---- Export Call Example ---- Export Call Example - Export Single Table EXEC File2Table 'ZJ', '', ' ',' xzkh_sa .. area, '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 .. Area,' 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) - the server name, @ username varchar (200 - Username, if used in NT authentication, @ Password varchar (200) - password, @ TBNAME VARCHAR (500) - Database .dbo. Table name, if not specified: .dbo. Table name, export all user tables of the database, @ filename varchar (1000) - Import / Export Path / File Name, if the @TBName parameter indicates to export the entire database, this parameter is the file storage path, file name automatic table Name .txt, @ isout bit --1 is exported, 0 is imported as Declare @SQL VARCHAR (8000) if @tbname like '%.%.%' - If the table name is specified, then derive a single table Begin Set directly @ SQL = 'bcp' @ TBNAME CASE WHEN @ ISOUT = 1 TEN 'OUT' ELSE 'IND ' "' @ filename '" / w ' ' / s' @ Servername Case When ISNULL ( @Username, '') = '' TEN 'ELSE' / u ' @ username end ' / p ' isnull (@password,' ') exec master..xp_cmdshell @SQL END ELSE BEGIN - Export the entire database , Define a cursor, remove all user table declare @m_tbname varchar (250) if right (@ filename, 1) <> '/' set @ filename = @ filename '/'

set @ m_tbname = 'declare #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 TEN 'OUT' ELSE 'IN' END '"' @ filename @ m_tbname '. Txt" / W ' ' / s ' @ ServerName Case When ISNULL (@username,') = '' TEN '' ELSE '/ U' @ UserName End '/ P' Isnull (@password, '') EXEC MASTER..XP_CMDSHEC MASTER..XP_CMDSHEC MASTER..XP_CMDSHELL @SQL FETCH NEXT from #TB INTO @m_tbname End Close #tb deallocate #TB end go / ******************** Excel guide to TXT **************************************** / Want to use Select * Into OpenDataSource. ..) from openDataSource (...) implements importing an Excel file content into a text file

Suppose there is two columns in Excel, the first list is named, the second list is a queue (16-bit) and the bank account is exported to the text file, and the first 8 bits and the post-8 bits are separated.

If you want to insert the statement above, the text file must exist, and there is a row: Name, Bank Account 1, Bank Account 2 You can use the following statement to make an important note file name and directory to modify according to your actual situation.

INSERT INTO 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 insert and generate a text file directly, use BCP

Declare @SQL VARCHAR (8000), @ TbName Varchar (50)

- First import the Excel table content into 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) - then use BCP to export from global temporary table to text file set @ SQL = 'bcp " @ TBNAME '"OUT" C: /AA.TXT "/ S" (local) "/ p" "/ c' exec master..xp_cmdshell @sql

- Delete Temporary Table EXEC ('Drop Table' @ TBNAME)

Import files into the stored procedure exported to the database with BCP:

/ * - Import export of BCP-binary files

Support Image, Text, NTEXT field import / export image is suitable for binary files; text, ntext is suitable for text data files

Note: When importing all rows that cover the condition, all the rows that meet the conditions are also available to the specified file.

This stored procedure uses only BCP implementation Zou Jian 2003.08 ----------------- * /

/ * - Call example - Data Export EXEC P_BINARYIO 'ZJ', '', '', 'ACC_ Data ..TB', 'IMG', 'C: /ZJ1.DAT'

- Data Export EXEC P_BINARYIO 'ZJ', '', '', 'ACC_ 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), - User name @password varchar (30), - password @tbname varchar (500), - database .. Name @ FDNAME VARCHAR (30), - Field Name @Fname Varchar (1000), - Directory File Name, Use / Coverage during Processing: @ filename .bak @tj varchar (1000) = ', - Processing Conditions For data imports, if you contain @FDName, specify the table name prefix @isout bit = 1 --1 Export ((default), 0 Import As Declare @fname_in varchar (1000) --bcp processing answer file name, @ Fsize varchar (20) - The size of the file to be processed, @ m_tbname varchar (50) - temporary table name, @ SQL VARCHAR (8000)

- Obtain the size of the import file if @ iSout = 1 set @ fsize = '0' else begin create table #TB (optional VARCHAR (20), size INT, creation date varchar (10), create time varchar (20 ), Last written operation date varchar (10), last written operation time varchar (20), last visited date varchar (10), last access time varchar (20), Fe) INSERT INTO #TB EXEC MASTER .XP_GETFILEDETAILS @fname select @ fsize = size from #tb drop table #tb if @FSIZE is null begin print 'File No' Return Endend

- Generate data processing answer 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 End Union All Select Null AS Formats) A 'Exec (@SQL) SELECT @ fname_in = @ fname ' _ Temp ', @ SQL = 'bcp "' @ m_tbname '" out "' @ fname_in '" / s "' @ servename case when isnull (@username, '') = '' TEN '' ELSE '" / u "' @ username end '"/ p"' isnull (@password, '') '"/ c' exec master" @ SQL - Delete 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 '' Ten 'Else' Where ' @ tj end '"queryout" @ fname ' "/ s" ' @ servename case when ISNULL (@usename,' ') =' 'TEN' 'ELSE' "/ u" ' @ username end ' " / P " isnull (@password, '') '" / i "' @ fname_in '"' exec master..xp_cmdshell @SQL END ELSE BEGIN - Prepare Temporary table set @ SQL = 'SELECT TOP 0 ' @ fdname ' INTO ' @ m_tbname ' from ' @ TBNAME EXEC (@SQL) - Import Data into Temporary Table Set @ SQL =' BCP "' @ m_tbname '" in "' @ fname '"/ S" @ servename case when isnull (@username,' ') =' '' '' Else '"/ u"' @ username end '"/ p"' isnull (@password , ') ' "/ I" ' @ fname_in ' "'exec master..xp_cmdshell @SQL - Import data into the official table set @ SQL =' update ' @ TBNAME ' set ' @ fdname '=b.' @fdname 'from' @ TBNAME 'A,' @ m_tbname 'b' case isnull (@tj, '') when '' '' Else 'Where' @ TJ End EXEC (@SQL)

- Delete data processing temporary table set @ SQL = 'drop table' @ m_tbname end

- Delete data processing answer file set @ SQL = 'del' @ fname_in exec master..xp_cmdshell @SQL

Go

/ ** Import text files EXEC MASTER..XP_CMDSHELL 'BCP "DBNAME..TABLENAME" IN C: /DT.TXT -C -SSERVERNAME -USA-PPASSWORD'

Changed to the following, no quotient exec master..xp_cmdshell 'bcp dbname..tablename in c: /dt.txt -c -ssrvername -usa -ppassword'

转载请注明原文地址:https://www.9cbs.com/read-131220.html

New Post(0)