SQL

xiaoxiao2021-03-06  25

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" "" "" "imported" ", '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)) ' 'Convert Alias ​​from OpenDataSource (' Microsoft .Jet.oledb.4.0 ',' data source = "c: /test.xls"; user ID = admin; password =; extended proties = excel 5.0 ') ... xactions / ** Import text files Exec Master .. XP_cmdshell 'BCP "DBNAME..TABLENAME" IN C: /DT.TXT -C -SSERVERNAME -USA-PPASSWORD' / ** Export file exec master..xp_cmdshell 'bcp "dbname..tablename" OUT C: / DT. TXT-CSSERVERNAME-ISA-Password 'or Exec Master "," Select * from dbname..tablename "Queryout C: /dt.txt -c -ssrvername -usa -ppassword' exported to txt text, with comma Separate the exec master..xp_cmdshell 'bcp "library name .. Name" OUT "D: /TT.TXT" -c -t, -u sa -p password' bulk INSERT library 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 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 ( That is, the 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 file name of the FoxPro table where the FoxPro table is specified. / ************* 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 Table 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. <

ORDER OID = "O4" DATE = "1/20/1996" Amount = "10000" /> '- create an internal representation of the xml documentation. 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 / ******************************************************************************************************************************* **************** / Use BCP-implemented stored procedures / * Implement data import / export stored procedures According to different parameters, the import / export can be implemented Database / Single Table Call Example: - Export Call Example --- Export 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 .. 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 ProCedu Re file2table @servername varchar (200) - server name, @ username varchar (200) - User name, if used in NT authentication mode, @ Password varchar (200) - password, @ TbName Varchar 500) - Database. DBO. Table name, if not specified: .dbo. Download all user tables, @ filename varchar (1000) - Import / Export Path / File Name, if the @TBName parameter indicates Is exporting the entire database, this parameter is the file storage path, the file name automatically uses a 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 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, '') =

'' '' 'else' / u ' @ Username end ' / p ' isnull (@password,' ') exec master..xp_cmdshell @SQL 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 '. .soBjects where xtype = 'u' 'EXEC (@m_tbname) open #tb fetch next from # @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 Inull (@username,' ') =' 'TEN' 'ELSE' / u ' @ username end ' / p ' isnull (@password,' ') exec master..xp_cmdshell @sql fetch next from #TB INTO @m_tbname end close #TB DEAALLOCATE #TB End Go / ******************************************************************************************************************************************************************************************************** ********************** / want to use Select * Into OpenDataSource (...) from openDataSource to import an Excel file content into one Text file assumes two columns in Excel, the first list is named, the second list is a very account (16-bit) and the bank account is derived from the text file. Two 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-65087.html

New Post(0)