USE AdventureWorks2008R2; GO -- Define the CTE expression name and column list. WITH Sales_CTE (SalesPersonID, SalesOrderID, SalesYear) AS -- Define the CTE query. ( SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear INTO #temp1 FROM Sales.SalesOrderHeader WHERE SalesPersonID IS NOT NULL ) -- Define the outer query referencing the CTE name. SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear INTO #temp2 FROM Sales_CTE GROUP BY SalesYear, SalesPersonID ORDER BY SalesPersonID, SalesYear; GO
--启用Ad Hoc Distributed Queries EXEC SP_CONFIGURE 'show advanced options',1 RECONFIGURE EXEC SP_CONFIGURE 'Ad Hoc Distributed Queries',1 RECONFIGURE --使用OpenDataSource导入数据 INSERT INTO IMP_DATA.dbo.t_goods SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.12.0', 'Data Source="E:/Report1.txt";User ID=Admin;Password=;Extended properties=Excel 12.0')...[Sheet1$] --使用完毕后,切记关闭它,因为这是一个安全隐患 EXEC SP_CONFIGURE 'Ad Hoc Distributed Queries',0 RECONFIGURE EXEC SP_CONFIGURE 'show advanced options',0 RECONFIGURE
BULK INSERT允许用户以其指定的格式将数据文件导入到数据库表或视图中。更多请点击:http://msdn.microsoft.com/zh-cn/library/ms188365.aspx
--定义导入目的和导入源 BULK INSERT IMP_DATA.dbo.t_goods FROM 'E:/Report1.txt' WITH ( --列分隔符 FIELDTERMINATOR = ',', --行分隔符 ROWTERMINATOR = '\n' )
--打开高级选项 EXEC SP_CONFIGURE 'show advanced options', 1; RECONFIGURE; --启用执行CMD命令 EXEC SP_CONFIGURE 'xp_cmdshell', 1; RECONFIGURE; --指定导入目的和导入源 EXEC master..xp_cmdshell 'BCP IMP_DATA.dbo.t_goods in E:\report.txt -c -T'
最后,贴张前段时间做的图,导入数据总结:
Good Luck!