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.
EXEC sp_executesql @query?