September 6, 2006 at 12:45 pm
Hi everyone,
I have a store procedure and 3 parameters, @year1 char(4), @year2 char(4), and @Rflag char(1)
I want the store procedure pull different set up data based on the @Rflag(the idea is from Andrew's post the other day), when Rflag is 'D', need to pull data for agent number '12345', when Rflag is 'B' then I need to pull data for all other agents' sales except agent '12345', but display agentcode '66666' to represent all those agents. When @Rflag with any other value, I need to pull all sales from all agent (without agentCode condition), year1 and year2 just mean I need to pull those two years' sales etc.
So I test it first using the following statement:
Declare @Rflag char(1)
select @Rflag='D'
select count(product) numPol, product, sum(Cast((order_total/100.00) As Decimal(12,2))) txnSale,
CASE WHEN agentcode = '12345' THEN agentCode ELSE '66666' End AS Agent from tis_vs
where (year(txndatetime)=2004 or year(txndatetime)=2005)
if @Rflag='D'
and agentCode='12345'
if @Rflag='B'
and agentCode<>'12345'
group by product, CASE WHEN agentcode = '12345' THEN agentCode ELSE '66666' End
order by product, CASE WHEN agentcode = '12345' THEN agentCode ELSE '66666' End
but it keeps display the following error message:
Incorrect syntax near the keyword 'group'.
I guess I did the wrong way. Can you help?
Thank you.
Betty
September 6, 2006 at 3:50 pm
Try the below query...
DECLARE @RFLAG CHAR(1)
SET @RFLAG = 'D'
IF @RFLAG = 'D'
BEGIN
SELECT COUNT(PRODUCT) NUMPOL, PRODUCT,
SUM(CAST((ORDER_TOTAL/100.00) AS DECIMAL(12,2))) TXNSALE, AGENTCODE
FROM TIS_VS
WHERE (YEAR(TXNDATETIME)=2004 OR YEAR(TXNDATETIME)=2005)
AND AGENTCODE = '12345'
GROUP BY PRODUCT, NUMPOL, AGENTCODE
ORDER BY PRODUCT
END
ELSE IF @RFLAG = 'B'
BEGIN
SELECT COUNT(PRODUCT) NUMPOL, PRODUCT,
SUM(CAST((ORDER_TOTAL/100.00) AS DECIMAL(12,2))) TXNSALE, '66666' AS AGENTCODE
FROM TIS_VS
WHERE (YEAR(TXNDATETIME)=2004 OR YEAR(TXNDATETIME)=2005)
AND AGENTCODE <> '12345'
GROUP BY PRODUCT, NUMPOL, PRODUCT, AGENTCODE
ORDER BY PRODUCT
END
ELSE
BEGIN
SELECT COUNT(PRODUCT) NUMPOL, PRODUCT,
SUM(CAST((ORDER_TOTAL/100.00) AS DECIMAL(12,2))) TXNSALE, AGENTCODE
FROM TIS_VS
WHERE (YEAR(TXNDATETIME)=2004 OR YEAR(TXNDATETIME)=2005)
GROUP BY PRODUCT, NUMPOL, AGENTCODE
ORDER BY PRODUCT
END
September 6, 2006 at 5:13 pm
Use the following query in a dynamic way
Declare
@Rflag char(1)
select
@Rflag='B'
Declare
@sql nvarchar(4000)
set
@sql=N'select count(product) numPol, product, sum(Cast((order_total/100.00) As Decimal(12,2))) txnSale, ' +
'CASE WHEN agentcode = ''12345'' THEN agentCode ELSE ''66666'' End AS Agent ' +
'from tis_vs ' +
'where (year(txndatetime)=2004 or year(txndatetime)=2005) '+
'and ' + case @Rflag when 'D' then 'AgentCode =''12345''' when 'B' then 'AgentCode <> ''12345''' end +
' group by product, CASE WHEN agentcode = ''12345'' THEN agentCode ELSE ''66666'' End ' +
' order by product, CASE WHEN agentcode = ''12345'' THEN agentCode ELSE ''66666'' End '
exec
sp_executesql @sql
September 7, 2006 at 2:11 pm
N stands for unicode in a literal string.
Kind of like doing CAST('this text to ' AS nVarchar(4000))
September 7, 2006 at 2:17 pm
Prefixing N is necessary in our problem
From BOL
Unicode string constants that appear in code executed on the server, such as in stored procedures and triggers, must be preceded by the capital letter N. This is true even if the column being referenced is already defined as Unicode. Without the N prefix, the string is converted to the default code page of the database
September 7, 2006 at 2:28 pm
Thank you both. No wonder I saw some database setup script with a lot of N.
But I never figured out when we do need it and when we don't need it.
Betty
September 7, 2006 at 2:29 pm
I'm reading it but I don't see how this can be bad. Care to explain some more?
September 7, 2006 at 2:40 pm
It will not compile at all as sp_executesql expects 'N' in Parameter
Please read earlier reply (extract from BOL)
September 7, 2006 at 2:47 pm
Gopi,
got you. I didn't read the sp_executesql definition.
I am very appreciated your help.
Betty
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply