[英]Crosstab/pivot query with Image data type in SQL Server 2008
I need to execute a crosstab/pivot query in SQL Server using columns of all varchar data types and one image data type.我需要使用所有 varchar 数据类型和一种图像数据类型的列在 SQL 服务器中执行交叉表/数据透视查询。 I have created a stored procedure that joins a series of tables in this format.
我创建了一个以这种格式连接一系列表的存储过程。 "B" the only element that can be linked to image data.
“B”是唯一可以链接到图像数据的元素。 If the DATA field for "B" is blank, then there is also blank IMAGEDATA for that row.
如果“B”的 DATA 字段为空白,则该行也有空白 IMAGEDATA。
OBJID CONTAINERID ELEMENT DATA IMAGEDATA
1 11 A a123 NULL
1 12 A aa123 NULL
1 11 B b123 0XFFD8FFE
1 12 B bb123 0XFFD8FFE
1 11 C c123 NULL
Then I use a pivot query.然后我使用 pivot 查询。 Ideally, the table should look like this:
理想情况下,表格应如下所示:
OBJID CONTAINERID A B C IMAGEDATA
1 11 a123 b123 c123 0XFFD8FFE
1 12 aa123 bb123 0XFFD8FFE
Problem is that image data fields cannot be pivoted, grouped, converted, etc...I've tried converting the image to varchar from binary, but it gives me gibberish.问题是图像数据字段无法旋转、分组、转换等...我尝试将图像从二进制转换为 varchar,但它给了我乱码。
Restrictions:限制:
Is there any other possible way to do this?有没有其他可能的方法来做到这一点?
So it seems there is no other way to do this than to manually add/create the IMAGEDATA column as a last step.因此,除了作为最后一步手动添加/创建 IMAGEDATA 列之外,似乎没有其他方法可以做到这一点。 Here was my solution:
这是我的解决方案:
I used two separate tables: one pivot table without the IMAGEDATA column, and another table similar to the first one displayed in the question above.我使用了两个单独的表:一个没有 IMAGEDATA 列的 pivot 表,另一个类似于上述问题中显示的第一个表。 Once I made the queries for the pivot table, I put it into a temp table, added a column with an IMAGE data type, and made the matches to fill in the IMAGEDATA column.
对 pivot 表进行查询后,我将其放入一个临时表中,添加一个具有 IMAGE 数据类型的列,并进行匹配以填充 IMAGEDATA 列。
Seems like a simple solution, but it is hardly maintainable nor efficient.似乎是一个简单的解决方案,但它几乎不可维护且效率不高。 I am not relying on much to match up the IMAGEDATA with the other fields, and what if I decide that more ELEMENTS will have IMAGEDATA?
我不太依赖于将 IMAGEDATA 与其他字段匹配,如果我决定更多的 ELEMENTS 将具有 IMAGEDATA 怎么办?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.