简体   繁体   English

特定行上的 Openrowset 批量插入

[英]Openrowset bulk insert on a specific row

I am new to SQL, but am trying to learn its logic, I am assuming bulk insert will insert on all rows in this case a blob.我是 SQL 的新手,但我正在尝试学习它的逻辑,我假设批量插入将在这种情况下在所有行上插入一个 blob。 (pdf file) below is my code but what I am trying to accomplish is, inserting a pdf file that I have put on SQL server in a row that has a matching Primary Key that I specify. (pdf 文件) 下面是我的代码,但我想要完成的是,将我放在 SQL 服务器上的 pdf 文件插入一行,该文件具有我指定的匹配主键。 So far I am missing the where clause to specify the PK到目前为止,我缺少 where 子句来指定 PK

   Declare @sql varchar(max)
   Declare @filePath varchar(max)
   Set @filePath = 'C:\iphone.pdf'
   Set @sql='INSERT INTO HDData.dbo.PurchasedCellPhoneInfo(Receipt) SELECT * FROM OPENROWSET(BULK '''+ @filePath+''', SINGLE_BLOB) AS BLOB'
   exec(@sql)

May I use an update t-SQL query instead of insert?我可以使用更新 t-SQL 查询而不是插入吗? and how would I drop the where to specify a specific row I want to insert this blob in?以及如何删除指定要插入此 blob 的特定行的位置? Any help would be appreciated.任何帮助,将不胜感激。

I also have tried this, following @misterPositive's suggestion for update query:我也尝试过这个,遵循@misterPositive 的更新查询建议:

 Declare @criteria varchar(50)
 SET @criteria ='352014075399147'
 UPDATE HDData.dbo.PurchasedCellPhoneInfo SET Receipt =
 (SELECT Receipt FROM OPENROWSET (BULK 'C:\352014075399147.pdf, SINGLE_BLOB') a)
 WHERE(IMEI = @criteria)

i do recieve this message: Either a format file or one of the three options SINGLE_BLOB, SINGLE_CLOB, or SINGLE_NCLOB must be specified.我确实收到此消息:必须指定格式文件或三个选项 SINGLE_BLOB、SINGLE_CLOB 或 SINGLE_NCLOB 之一。 i like this update query as it seems to fit what im trying to do.我喜欢这个更新查询,因为它似乎适合我想要做的事情。

You can do this to UPDATE:你可以这样做来更新:

UPDATE MyTable 
SET blobField = 
   (SELECT BulkColumn FROM OPENROWSET (BULK 'C:\Test\Test1.pdf', SINGLE_BLOB) a) 
WHERE (CriteriaField = @criteria)

Here is another way for PK这是PK的另一种方式

CREATE VIEW [dbo].[VWWorkDataLoad]
AS
SELECT RecordLine
FROM [dbo].[WorkDataLoad];

Now BULK INSERT should then look like:现在 BULK INSERT 应该如下所示:

BULK INSERT [dbo].[VWWorkDataLoad] FROM 'D:\NPfiles\TS082114.trn' 
WITH (FIRSTROW = 2,FIELDTERMINATOR = ',' , ROWTERMINATOR = '\n');

If you want to insert new records then you could have an identity column for your PK and not have to worry about it.如果您想插入新记录,那么您可以为您的 PK 设置一个标识列,而不必担心。 I have also seen functions used when a table is designed without identity on PK.我还看到了在设计表时在 PK 上没有标识时使用的函数。 Something like GetTableNamePK() in the select list.类似于选择列表中的 GetTableNamePK() 。

If you want to update an existing record then you will want a where clause as you mentioned.如果您想更新现有记录,那么您将需要一个您提到的 where 子句。 This worked for me in testing:这在测试中对我有用:

Update TestBlob Set BinaryData = (SELECT * FROM OPENROWSET(BULK 'c:\temp\test.pdf', SINGLE_BLOB) AS BLOB)
    Where ID = 2

If you do not want to use the Identity or Function this worked where ID is a primary key and I want to insert the BLOB with PK of 3:如果您不想使用身份或函数,这在 ID 是主键的情况下有效,我想插入 PK 为 3 的 BLOB:

INSERT INTO TestBlob2 (ID, BinaryData) SELECT 3, * FROM OPENROWSET(BULK 'c:\temp\test.pdf', SINGLE_BLOB) AS BLOB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM