tags:

views:

358

answers:

2

I'm familiar with this sort of syntax in SQL Server, to concatenate strings in my result set:

SELECT 'foo' + bar AS SomeCol
FROM SomeTable

I would like to do something similar inside a ColdFusion Query of Queries:

<cfquery name="qOptimize" dbtype="query">
    select 
     image_id AS imageId,
     '#variables.img_root#' + image_id + '.' + image_ext AS fullImage,
     '#variables.img_root#' + image_id + 't.' + image_ext AS thumbnailImage,
    from qLookup
    order by imageId asc
</cfquery>

This is part of a service consumed by a Flex application, so I'm optimizing the result of a stored procedure used elsewhere in the application before returning to the client -- stripping out unused columns, and compiling image URLs from some dynamic path information.

I could write a new stored procedure that takes the image root as a parameter and does all of this, and I probably will for performance reasons, but the question is still nagging me. I haven't found a syntax that works yet, so I wonder if it's possible.

When I try the above, I get the following error:

Query Of Queries syntax error.
Encountered "from. Incorrect Select List, Incorrect select column,

Has anyone done this? Is it possible, perhaps with another syntax?

+5  A: 

Yes this is possible. I think the problem is that image_id is most likely a numeric value. If you cast it as an varchar then it should be fine.

<cfquery name="qOptimize" dbtype="query">
    select 
        image_id AS imageId,
        '#variables.img_root#' + cast(image_id as varchar) + '.' + image_ext AS fullImage,
        '#variables.img_root#' + cast(image_id as varchar) + 't.' + image_ext AS thumbnailImage
    from qLookup
    order by imageId asc
</cfquery>
Jason
I ran into this when I converted it to a stored procedure, but thanks.
Adam Tuttle
+4  A: 

I think the error you mention is due to the comma at the last concatenation, at the end of thumbnailImage.

Just my $0.002

Agree 100%. If the sample code was the actual code used, the last comma was the deal breaker. Good spot.
jyoseph