Hello all --
I have the following query in the query analyzer that works perfectly:
select top 1 r.callCenter, count(*) as countTop
from cgResponse2001_3 c, regionCallCenter r
where r.callCenterCode=c.callCenter and q5 between 4 and 5
group by r.callCenter
order by c.countTop DESC
I am trying to make it work in a SPROC by dynamically creating and executing a sql statement, but I'm having real problems getting the ORDER BY clause to function. Here's what I have so far:
SET @sql1='(select top 1 @best = regionCallCenter.callCenter, @thisCount = count(*) as countTop from ' + @responseTable +' , regionCallCenter where (regionCallCenter.callCenterCode='+@responseTable +'.callCenter and '+@responseTable+'.' + @variable + ' BETWEEN ' + @topLBound + ' AND ' + @topUBound + ')' + @regionWhere + @callCenterWhere + @customerExperienceWhere + @usageLevelWhere + @customerSegmentWhere + 'group by regionCallCenter.callCenter order by ' + @responseTable + '.countTop DESC )'
Please pay attention to the bolded portion, as this is the culprit. I just can't seem to get the thing to work.
I know that AS is usually not supported inside a SPROC, but if I don't have it, then what do I order by?
Error msg:
Incorrect syntax near the keyword 'as'.
If anyone could shed some light on how to get this to function, I really would appreciate it.
Thanks!
Paul Prewett
I have the following query in the query analyzer that works perfectly:
select top 1 r.callCenter, count(*) as countTop
from cgResponse2001_3 c, regionCallCenter r
where r.callCenterCode=c.callCenter and q5 between 4 and 5
group by r.callCenter
order by c.countTop DESC
I am trying to make it work in a SPROC by dynamically creating and executing a sql statement, but I'm having real problems getting the ORDER BY clause to function. Here's what I have so far:
SET @sql1='(select top 1 @best = regionCallCenter.callCenter, @thisCount = count(*) as countTop from ' + @responseTable +' , regionCallCenter where (regionCallCenter.callCenterCode='+@responseTable +'.callCenter and '+@responseTable+'.' + @variable + ' BETWEEN ' + @topLBound + ' AND ' + @topUBound + ')' + @regionWhere + @callCenterWhere + @customerExperienceWhere + @usageLevelWhere + @customerSegmentWhere + 'group by regionCallCenter.callCenter order by ' + @responseTable + '.countTop DESC )'
Please pay attention to the bolded portion, as this is the culprit. I just can't seem to get the thing to work.
I know that AS is usually not supported inside a SPROC, but if I don't have it, then what do I order by?
Error msg:
Incorrect syntax near the keyword 'as'.
If anyone could shed some light on how to get this to function, I really would appreciate it.
Thanks!

Paul Prewett

