0

I would like to have a query like this

SELECT
    param1,
    param2,
    param3,
    getHidden(param1, param3)
FROM xxx

In which my function has to build a dynamic query, execute it and return 0 or 1.

CREATE FUNCTION getHidden (@param1 VARCHAR(50), @param2 INT) RETURNS INT AS
BEGIN
    DECLARE @firstSelect VARCHAR(200);
    DECLARE @query VARCHAR(1000);
    SELECT @firstSelect = restriction FROM xxxx WHERE param1 = @param1 AND param2
    SET @query = 'SELECT (CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END) FROM xxx WHERE ' + @firstSelect;
    -- EXEC @query?
    RETURN 1
END

I'm blocked in the dynamic query execution, is there a workaround to get through this problem?

Thx, Jeremy

EDIT : Details In jasper ireport, I have to make a query (only one) which should be able to return and display a list of questions. In the database, each line has a question and may have a restriction field. This field contains a WHERE constraint (example of content: BEIE_Emp_Nb = 0) In my php program, I use this field for building my query. My final query will be:

SELECT param1, param2 FROM questions WHERE 1 = 1 AND $constraint
which gives me
SELECT param1, param2 FROM questions WHERE 1 = 1 AND BEIE_Emp_Nb = 0

I don't know if this is clear enough. The problem is the fact that I have to query the constraint field for the actual question and THEN build my query and return the result.

6
  • 2
    What problem are you trying to solve here? I'd be surprised if there weren't a better way if you can explain in more detail. Commented Dec 9, 2014 at 9:32
  • I can't find a solution to execute @query and return the value (1 or 0 Commented Dec 9, 2014 at 9:34
  • EXEC sp_executesql @query ? Commented Dec 9, 2014 at 9:36
  • 1
    @user1474188 That's what you're trying to do here, I can see that. I'm suggesting that you take a step back from the code and explain the actual problem you're trying to solve, which might have a better solution than the one you're implementing. Commented Dec 9, 2014 at 9:37
  • Just edited my main topic. Commented Dec 9, 2014 at 9:47

2 Answers 2

1

You cannot call stored procedures, like sp_executesql, in a SQL Server function. This means that you can't define and execute dynamic SQL code inside a function. The reason is that functions are not allowed to have side-effects (like calling themselves and changing data or parameters, as you're doing with your dynamic SQL query, and executing something like INSERT, DELETE or UPDATE).

Only stored procedures can call functions and not the other way around. Check here the limitations and restrictions of functions. You should then create a stored procedure, instead of a function, to solve your problem.

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

2 Comments

Will I be abble to call it the way I want then? (SELECT param1, EXEC proc @param1 = 'xxx' FROM mytable)
@jeremyb Yes, you will be able to execute dynamic SQL code inside a stored procedure.
0

To execute dynamic query use EXEC with output variable.

DECLARE @nResult INT

EXEC sp_executesql @query, N'@nResultNew INTEGER OUTPUT', @nResult OUTPUT

RETURN @nResult 

1 Comment

You cannot call stored procedures in a SQL Server function. See here.

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.