2

I am trying to get a complex stored procedure to work, but I can't figure out the relationship between stored procedures, parameters and IF/THEN statements.

I have stripped my code down to the attached chunk of code, that shows where I am going wrong. When I execute it, I get an error

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'and'

Basically, I have 3 variables - @Test1, @Test2 and @Test3. These are prompts that appear when you execute the stored procedure. If the user enters '1', '2', '3' respectively, it should show the text 'Show 1 and 2 and 3 Plus Pivot Section'. If the only enter '1','2' it should show the text 'Show 1 and 2 Plus Pivot Section'.

Below is my code, am I doing something wrong??

ALTER PROCEDURE [dbo].[sp_Data_Extract_Test]
   @Test1 [NVARCHAR] (400),
   @Test2 [NVARCHAR] (400),
   @Test3 [NVARCHAR] (400)
WITH EXECUTE AS CALLER
AS
BEGIN
   SET NOCOUNT ON;

   DECLARE @Output NVARCHAR(400) ;
   Declare @param NVARCHAR(900) ;

   Set @param = '@Test1 nvarchar(400), @Test2 nvarchar(400), @Test3 nvarchar(400)'

   if (@Test1 = '1')
   BEGIN
      SET @Output = 'Show 1';
   END

   if (@Test2 = '2')
   BEGIN
      SET @Output = @Output + ' and 2';
   END

   if (@Test3 = '3')
   BEGIN
      SET @Output = @Output + ' and 3';
   END

   BEGIN
      SET @Output = @Output + ' Plus Pivot Section'
      PRINT @Output
   END

   EXECUTE SP_EXECUTESQL @Output, @param, @Test1, @Test2, @Test3;
END

Edit: My example above isn't a good example. I was trying to narrow it down to identify the problem, but it seems as though the Execute statement at the bottom expects a SQL statement, not a line of text.

What I am trying to do, is effectively the following. I want to execute a stored procedure that pivot's a data set. The data is built up by 2 appends (unions) that are based on prompts. I want it so that the user can potentially run it for 1 type of Quantity, for 2, or for 3. If they want to see all 3 Quantities, it needs to do 2 appends. The code is as below. I've added XXX's where the SQL statement is because I know those parts are working fine, it's only when I try to do this if statement based on variables so I can potentially filter off the unions.

--- Above: Alter Stored Procedure, with variable declarations
declare @V1 NVARCHAR(4000) ;
declare @param NVARCHAR(900) ;

set @param='XXXXXX'

if (@Value1 = 'Qty_Ordered')
BEGIN
set @V1='SELECT XXXXXX’
END

if (@Value1 = 'Qty_Ordered' and @Value2 = 'Qty_Supplied')
BEGIN set @V1= @V1 + '
UNION ALL
SELECT XXXXX'
END

if ((@Value1 = 'Qty_Ordered' or @Value2 = 'Qty_Supplied') and @Value3 = 'Qty_Forecast')
BEGIN set @V1= @V1 + 
'UNION ALL
SELECT XXXXXX’
END 

BEGIN 
set @V1 = @V1 + ' 
PIVOT
(
SUM([Value])
FOR p.forecast_Week_No IN ('+@cols1+','+@cols2+','+@cols3+')) AS pvt'
END

EXECUTE SP_EXECUTESQL @V1, @param, XXXXXXXX;

END
2
  • Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all! Commented Sep 26, 2014 at 4:58
  • I think the problem is that you aren't executing a sql statement. @output ends up being Show 1 and 2 and 3 Plus Pivot Section if I pass a 1,2,3. So your final execute reads execute sp_executesql 'Show 1 and 2 and 3 Plus Pivot Section...' Commented Sep 26, 2014 at 5:40

1 Answer 1

1

The frist paramenter for sp_executesql should be a TSQL statement, but @Output does not represent that. If your intention was only to print the message then comment EXECUTE SP_EXECUTESQL @Output, @param, @Test1, @Test2, @Test3;


Now regarding Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'and'

sqlserver basically checked for syntax error in the TSQL stmt passed through @Output,
what it got was "Show 1 and 2 and 3 Plus Pivot Section"
As SHOW is not a SQL stmt, it considered it as a stored proc,
next it looked at 1, and 1 it is allowed as a paramenter
next it got "and", it is not accepted to used words like "and" while executing a SP
so gave a syntax error Msg 156, Level 15, State 1...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.