使用带有XML批量格式文件的.csv文件中的Insert Openrowset创建TEMPORARY表

时间:2017-04-12 02:47:38

标签: sql-server xml database csv bulkinsert

Microsoft的Tech Page表示您可以执行以下操作,使用格式文件从.csv文件导入物理样本表。我有那个工作。

  

使用OPENROWSET批量行集提供程序

     

以下示例使用INSERT ... SELECT * FROM OPENROWSET(BULK ...)批量导入数据   从myTestFormatFiles-c.Dat数据文件到   AdventureWorks示例中的HumanResources.myTestFormatFiles表   数据库。此示例使用XML格式文件MyTestFormatFiles.Xml。   该示例在导入数据之前删除任何现有的表行   文件。在SQL Server Management Studio查询编辑器中,执行:

$array = -split 'a tale of two cities' -replace '\bof\b', 'bob'

但是,我想要做到这一点,但导入到尚未存在的临时 #table(类似于使用select into语句创建新表的方式)。是可能的,如果没有,为什么不呢?

附属问题:您可以使用select into创建临时表吗? 我知道您可以使用select into to create a table

现在我可以将导入批量OPENROWSET与尚未存在的临时表一起使用来创建表并将其导入到其中:

USE AdventureWorks2012;
DELETE myTestFormatFiles;
GO
INSERT INTO myTestFormatFiles
    SELECT *
      FROM  OPENROWSET(BULK  'C:\myTestFormatFiles-c.Dat',
      FORMATFILE='C:\myTestFormatFiles.Xml'     
      ) as t1 ;
GO
SELECT * FROM myTestFormatFiles;
GO
-- When you finish using the sample table, you can drop it using the following statement:
DROP TABLE myTestFormatFiles

测试告诉我没有: INSERT INTO #TestFormatFiles SELECT * FROM OPENROWSET(BULK 'C:\myTestFormatFiles-c.Dat', FORMATFILE='C:\myTestFormatFiles.Xml' ) as t1 ; GO

但是有没有办法从现有的表创建临时表?

1 个答案:

答案 0 :(得分:1)

是:改为使用SELECT * INTO #TestFormatFiles FROM OPENROWSET...

(你几乎拥有它:你不能INSERT INTO一个不存在的表。)

相关问题