简体   繁体   中英

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. 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. If the DATA field for "B" is blank, then there is also blank IMAGEDATA for that row.

  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. 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.

Restrictions:

    1. "ELEMENTS" are dynamic, so the number of columns for the pivot table is always changing. (ie. It *could* go all the way from A to Z.)
    2. The fields are to be used in an SSRS report, therefore I need to maintain the image data type
    3. I can't use any other developer language (.NET, etc..)

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. 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. 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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