Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否将var二进制文件更新到现有SQL列(添加到现有映像)?_Sql_Sql Server_Sql Update - Fatal编程技术网

是否将var二进制文件更新到现有SQL列(添加到现有映像)?

是否将var二进制文件更新到现有SQL列(添加到现有映像)?,sql,sql-server,sql-update,Sql,Sql Server,Sql Update,我有一张桌子City;我的Pic列数据类型是varbinary(max): 我用此代码更新我的pic列并进行工作(对于1个pic): 但如何更新Pic列中的3张或更多图像?(添加到现有图像) 这意味着我存储在表中,其中id=4只有一个图像二进制文件。如何在一列中保存多个图像?通过利用xml(及其结构完整性),可以在一列中以“半结构化”的方式存储多个图像。您可以将“pic”列的数据类型更改为xml或将其保持为varbinary(后者需要一些类型转换) 下面的示例假定“图像”列的varbinary

我有一张桌子
City
;我的
Pic
列数据类型是
varbinary(max)

我用此代码更新我的pic列并进行工作(对于1个pic):

但如何更新
Pic
列中的3张或更多图像?(添加到现有图像)


这意味着我存储在表
中,其中id=4
只有一个图像二进制文件。如何在一列中保存多个图像?

通过利用xml(及其结构完整性),可以在一列中以“半结构化”的方式存储多个图像。您可以将“pic”列的数据类型更改为xml或将其保持为varbinary(后者需要一些类型转换)

下面的示例假定“图像”列的varbinary存储和一个“c:\testnew”文件夹(在sql实例上),其中包含3个图像(图像[1..3].png)

首先,为cityid=1的“图像”列加载两个图像,然后使用update.write()将第三个图像附加到二进制数据(前两个图像)中。 使用xml.modify()方法可以在blob中的特定位置删除图像或插入图像

所有这些,如果您确实需要/必须在一行和一列中存储多个图像

create table dbo.TestImages
(
    id int identity(1,1) constraint pkidTestImages primary key clustered(id),
    CityId int,
    ImagesBlobXml varbinary(max) --or xml
)
go


--insert two images from folder c:\testnew
insert into dbo.TestImages
(
    CityId, ImagesBlobXml
)

values (
1, 
cast((
select TheImage
from 
(
select *
from openrowset(bulk N'C:\testnew\image1.png', single_blob) as i(TheImage)
union all
select *
from openrowset(bulk N'C:\testnew\image2.png', single_blob) as i(TheImage)
--union all
--select *
--from openrowset(bulk N'C:\testnew\stackoverflow.png', single_blob) as i(TheImage)
) as images
for xml path(''), type
) as varbinary(max))
);

select 'table content', *
from  dbo.TestImages;


--retrieve images (2)
select 'images in blob, initial insert', t.id, t.CityId, i.bin.value('.', 'varbinary(max)') as TheImage
from
(
select *, cast(ImagesBlobXml as xml) as ImagesBlobXmlXML
from dbo.TestImages 
) as t
cross apply t.ImagesBlobXmlXML.nodes('TheImage') as i(bin);


--append new image
update t
set ImagesBlobXml .WRITE( --note:write cannot be used on NULL values
cast((
select TheImage
from 
(
select *
from openrowset(bulk N'C:\testnew\image3.png', single_blob) as i(TheImage)
) as images
for xml path(''), type
) as varbinary(max)) , 
null, 0 --write() append
)
from dbo.TestImages as t
where CityId = 1;


--retrieve the images (3)
select 'images in blob, after update_append', t.id, t.CityId, i.bin.value('.', 'varbinary(max)') as TheImage
from
(
select *, cast(ImagesBlobXml as xml) as ImagesBlobXmlXML
from dbo.TestImages 
) as t
cross apply t.ImagesBlobXmlXML.nodes('TheImage') as i(bin);

--check for any diff
select i.bin.value('.', 'varbinary(max)') as TheImage
from
(
select *, cast(ImagesBlobXml as xml) as ImagesBlobXmlXML
from dbo.TestImages 
) as t
cross apply t.ImagesBlobXmlXML.nodes('TheImage') as i(bin)
except
select TheImage
from 
(
select *
from openrowset(bulk N'C:\testnew\image1.png', single_blob) as i(TheImage)
union all
select *
from openrowset(bulk N'C:\testnew\image2.png', single_blob) as i(TheImage)
union all
select *
from openrowset(bulk N'C:\testnew\image3.png', single_blob) as i(TheImage)
) as images;
go

--cleanup
drop table dbo.TestImages;
go

在一列中存储多个图像似乎是明智之举,但不要这样做!你永远不应该将多个项目存储到一列中——这只会带来长期的麻烦和维护问题。如果我不在一列中存储多个图像,有些行有40个图像,有些行有3个,有些行有10个。所以我必须有40列来存储图像,我如何知道每行要显示多少图像?你似乎有一个设计问题。行的一列应包含且仅包含由表表示的“thing”的一个属性。我们不知道存储一个城市的图片(或多张图片)的目的是什么,但为一个城市存储多张图片的正确设计是创建一个子表,该子表具有城市表的外键。tsql中没有“数组”类型,我认为您试图在这里不恰当地应用其他语言的概念。@smor我有5000个城市,如果我为任何城市创建一个子表,该城市将大量负载加载到服务器,供用户上载或下载pic。您应该使用适当的,关系模型:1个城市可以在一个单独的表中链接到n张图片-每行一张图片。这是正确的方法
create table dbo.TestImages
(
    id int identity(1,1) constraint pkidTestImages primary key clustered(id),
    CityId int,
    ImagesBlobXml varbinary(max) --or xml
)
go


--insert two images from folder c:\testnew
insert into dbo.TestImages
(
    CityId, ImagesBlobXml
)

values (
1, 
cast((
select TheImage
from 
(
select *
from openrowset(bulk N'C:\testnew\image1.png', single_blob) as i(TheImage)
union all
select *
from openrowset(bulk N'C:\testnew\image2.png', single_blob) as i(TheImage)
--union all
--select *
--from openrowset(bulk N'C:\testnew\stackoverflow.png', single_blob) as i(TheImage)
) as images
for xml path(''), type
) as varbinary(max))
);

select 'table content', *
from  dbo.TestImages;


--retrieve images (2)
select 'images in blob, initial insert', t.id, t.CityId, i.bin.value('.', 'varbinary(max)') as TheImage
from
(
select *, cast(ImagesBlobXml as xml) as ImagesBlobXmlXML
from dbo.TestImages 
) as t
cross apply t.ImagesBlobXmlXML.nodes('TheImage') as i(bin);


--append new image
update t
set ImagesBlobXml .WRITE( --note:write cannot be used on NULL values
cast((
select TheImage
from 
(
select *
from openrowset(bulk N'C:\testnew\image3.png', single_blob) as i(TheImage)
) as images
for xml path(''), type
) as varbinary(max)) , 
null, 0 --write() append
)
from dbo.TestImages as t
where CityId = 1;


--retrieve the images (3)
select 'images in blob, after update_append', t.id, t.CityId, i.bin.value('.', 'varbinary(max)') as TheImage
from
(
select *, cast(ImagesBlobXml as xml) as ImagesBlobXmlXML
from dbo.TestImages 
) as t
cross apply t.ImagesBlobXmlXML.nodes('TheImage') as i(bin);

--check for any diff
select i.bin.value('.', 'varbinary(max)') as TheImage
from
(
select *, cast(ImagesBlobXml as xml) as ImagesBlobXmlXML
from dbo.TestImages 
) as t
cross apply t.ImagesBlobXmlXML.nodes('TheImage') as i(bin)
except
select TheImage
from 
(
select *
from openrowset(bulk N'C:\testnew\image1.png', single_blob) as i(TheImage)
union all
select *
from openrowset(bulk N'C:\testnew\image2.png', single_blob) as i(TheImage)
union all
select *
from openrowset(bulk N'C:\testnew\image3.png', single_blob) as i(TheImage)
) as images;
go

--cleanup
drop table dbo.TestImages;
go