I have a function that receives parameters and returns a result, the following way works fine:
DECLARE @var varchar (10)
EXEC @var = GetLinea '','','', '' , ''
SELECT @var
But when I want to fill the values that I send by parameter with an CASE
error:
DECLARE @var varchar (10)
EXEC @var = GetLinea '', CASE WHEN '11' = '11' THEN 'SI' ELSE 'SI NO' END ,'', '' , ''
SELECT @var
The error that appears is:
Incorrect syntax near the keyword 'CASE'.
As if I was misusing the CASE
.
The code that I pass is in test mode, I must execute the function in a view.
This is the true case, the others are tests I used
SELECT
IdOferta,
Location,
PRO,
Toneladas,
Espesor,
Ancho,
CASE WHEN Location<>''
THEN 'MP_' + location
WHEN CHARINDEX('PREPI', clase_desc)>0 Or CHARINDEX('CINCA', clase_desc)>0
THEN 'REV_CA'
ELSE
GetLinea
parametro1,
CASE WHEN Null = parametro2 THEN '' ELSE parametro2 END,
CASE WHEN Null = parametro3 THEN '' ELSE parametro3 END,
CASE WHEN Null = parametro4 THEN '' ELSE parametro4 END,
parametro5
END
AS Linea_Final
What you can do is use an intermediate variable as a parameter, since exec does not accept expressions as a parameter:
In the case of a view you can use:
Note According to your condition of '11' = '11' it will always enter the case of if
In stored procedure mode:
Note Here it depends on what you really need to handle as parameters you can adjust, add or remove as you need Note2 I don't know if the select comes from a table or from variables from another site but it would be something like that in that form
Cheers
I think you are trying to validate the input parameters of your function
GetLinea
before passing them to the function.In any case, you can do that validation inside the function
GetLinea
:This way from the view you just use the function directly no matter what values you pass to it: