SQL statement import export

xiaoxiao2021-03-05  51

/ ******* 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 (Fieldtt) Erminator = ';', 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; 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: / 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 you use NT authentication, it is empty '', @ password varchar (200) - password, @ TBNAME VARCHAR (500) - Database .dbo. Table name, if not specified: .dbo. Download All user tables of the database, @ filename varchar (1000) - Import / Export Path / File Name, if the @TBName parameter is specified to export the entire database, this parameter is the file storage path, the file name is automatically used .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 @ 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, When you export all rows that meet the conditions, all meetings will be given The row is also available to the specified file This stored procedure only uses BCP to implement Zou Jian 2003.08 --------------- * / / * - Call Example - Data Export EXEC P_BINARYIO ' ZJ ',' ',' ',' ACC_ Download Data ..TB ',' IMG ',' C: /ZJ1.DAT '- Data Export EXEC P_BINARYIO' ZJ ',' ',', 'ACC_ Demo data ..tb ',' img ',' c: / zj1.dat ',' ', 0- * 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 @Password varchar (30), - password @tbname varchar (500), - database .. Name @fdname varchar (30), - field name @

FNAME VARCHAR (1000), - Directory File Name, Use / override during processing: @ filename .bak @tj varchar (1000) = '', - handle condition. For data import, if the condition contains @fdname Please specify a 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 file to be processed Size, @ 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 Name VARCHAR (20), size INT, creation date varchar (10), creation time varchar (20), last written operation date varchar (10), last write operation time varchar (20), last visited date varchar (10 ), Last visited 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 No' Return End end - Generate Data Process 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..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 '' '' 'Else' Where ' @ Tj end '" queryout " @

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

New Post(0)