如何在OPENROW集合内添加动态URL

我的Documents表中有一组数据行,该表中的每一行都有一个惟一的列AboluteUri(托管在Azure Container中)。为了实现存储响应(VARBINARY(MAX))的目标,我想做的是在OPENROWSET的大部分中添加一个动态url。当我尝试将一个静电单查询放入批量中时,它可以工作,但是当我试图通过添加来自TDM.AbsoluteUri的数据使其动态化时,它抛出一些错误,这是无效的语法。下面是我尝试过的内容。

SELECT TR.Id AS TransactionID, TDM.Id AS DocumentID,
(SELECT *
FROM OPENROWSET
(
BULK TDM.AbsoluteUri,
DATA_SOURCE = 'DocumentsUri',
SINGLE_BLOB
) AS blob),
FROM [dbo].[Transaction] AS TR
LEFT JOIN [dbo].[Documents] AS TDM ON TR.Id = TDM.TransactionId

预期结果应从DocumentsUri转换为VARBINARY(MAX)数据。

sql

文件名必须是字符串,表示动态推荐答案和游标。例如

declare @images table(name varchar(200), filename varchar(200))
insert into @images (name, filename) values ('Hammer','public/Hammer.jpg')
insert into @images (name, filename) values ('Screwdriver','public/screwdriver.jpg')

declare @localImages table(name varchar(200), image varbinary(max))

declare @name varchar(200), @filename varchar(200)

declare c cursor local for 
    select * from @images
open c

fetch next from c into @name, @filename

while @@FETCH_STATUS = 0
begin
  
  declare @sql nvarchar(max) = concat(N'
    SELECT ''',@name,''' name,  BulkColumn Image 
    FROM OPENROWSET
    (
       BULK ''',@filename, ''',
       DATA_SOURCE = ''BlobStore'',
       SINGLE_BLOB
    ) AS blob;  
     ');

     print @sql

    insert into @localImages(name,image)
    exec (@sql)

  fetch next from c into @name, @filename
end

select * 
from @localImages 

相关文章