SQL statement import export

xiaoxiao2021-03-06  39

/ ******* 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) 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 file EXEC MASTER..XP_CMDSHELL' BCP "DBNAME .. Tablename "in c: /dt.txt -c -ssword '/ ** Export text file EXEC MASTER..TXP_CMDSHELL' BCP" DBNAME..TABLENAME "OUT C: /DT.TXT -C -SSERVERNAME -Usa -ppassword 'or exec master..xp_cmdshell' bcp "Select * from dbname..tablename" Queryout c: /dt.txt -c -sservername -usa -ppassword 'exported to TXT text, separate EXEC MASTER with comma .. XP_cmdshell 'bcp "library name .. Name" OUT "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 Data 3.dbf] ") - * / - / * FoxPro Database Select * from OpenRowSet ('MSDasql', 'Driver = Microsoft Visual FoxPro Driver; source; ************ / 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: / Specify FoxPro Table The folder AA.DBF specifies the file name of the FoxPro table. / ************ Export to Access ****************** * / INSERT INTO OPENROWSET ('Microsoft.jet.OleDb.4.0', 'x: /a.mdb'; 'admin'; '', table) Select * from database name ..b table / ***** ******** Import Access ****************************** / INSERT INTO B SELET * from OpenRowSet ('Microsoft.jet.OleDb.4.0', 'x: /a.mdb'; 'admin'; '', table A ********************* Import XML file Declare @idoc int declare @doc VARCHAR (1000) --SAMPLE XML Document Set @doc = ' Customer Was Very Satisfied 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 / ****************************** *********************************** / with BCP implementation / * Implementing data import / export stored procedures can implement import / exporting the entire database / single table call example: - Export 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) - Server Name, @ Username Varchar (200) - User name, 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 that the entire database is exported, this parameter is the file storage path, the file name automatic table name .txt, @ isout bit --1 is exported, 0 is imported as Declare @SQL VARCHAR (8000) if @tbname like '%.%.%' - If you specify a table name, directly export a single table begin set @ SQL = 'bcp' @ TBNAME CASE WHEN @ ISOUT = 1 TEN 'OUT' ELSE 'IN' END '"' @ 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_cmdshell @SQL fetch next from #TB INTO @m_tbname endclose #tb deallocate #TB end go / *************************************** *********************************************** / want to use Select * Into OpenDataSource (...) From OpenDataSource (...) Imports an Excel file content into a text file to assume two columns in Excel, the first list is named, the second list is a very account (16-bit) and the bank account is exported to the text file. Different parts, the first 8 bits and the post-8 bits are separated.

If you want to insert it with the statement above, the text file must exist, and there is a line: name, bank account 1, bank account 2 can then use the following statement to make a note file name and directory to modify according to your actual situation. Insert INTO OpenDataSource ('microsoft.jet.oledb.4.0', 'text; hdr = yes; data = 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 from the 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) uses BCP Document Import Export to Database: / * - BCP-Binary Import Export Support IMAGE, TEXT, NTEXT field Import / Export Image is suitable for binary files; text, ntext is suitable for text data file Note: When imported, Export all rows that cover the conditions When all the rows of satisfying conditions are also available to the specified file, this stored procedure will only use BCP to implement 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_ 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), - 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) - get the size of the import file if @ isout = 1 set @ fsize = ' 0 'Else Begin Create Table #TB (optional VARCHAR (20), Size INT, Create Date VARCHAR (10), Create Time VARCHAR (20), Last Date VARCHAR (10), last Write operation time varchar (20) Fsize is null begin print 'File Did not find' return end end - Generate Data Process Appel 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 Format) 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..xp_cmdshell @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' @

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

New Post(0)