pamplemousse
Programmer
Hi all,
I need to create a stored procedure that will return a result set based on criterias that are provided by the application we use.
So far what we have is a store procedure that generate an sql statement dynamicaly. But I don't like this approche.
CREATE PROCEDURE sp_myProc ( inClause1 varchar, inClause2 varchar )
AS
BEGIN
DECLARE @sql VARCHAR(2000)
SET @sql = 'SELECT * FROM myTable
WHERE fieldChar IN (@inClause1)
AND fieldInt IN (@inClause2)'
EXEC (@sql)
END
Then I call the sp like so:
EXEC sp_myProc '''Male'',''Female''','1,2,3,4'
It works fine but I'm looking for a way not to recontruct the sql statement each time and pass only the parameters.
The problem is when I tried creating the sp using the parameters I got explicit data conversion error.
Trying to cast '1,2,3,4' as int.
I tried creating a function as StringTokenizer in Java to return a table containing int but got the same problem. My function would return correct info when assigning a local( within the function to test it ) varchar set to '1,2,3,4' but when passed a value '1,2,3,4' it would not return any rows.
Thanks for your time
Sylvain
I need to create a stored procedure that will return a result set based on criterias that are provided by the application we use.
So far what we have is a store procedure that generate an sql statement dynamicaly. But I don't like this approche.
CREATE PROCEDURE sp_myProc ( inClause1 varchar, inClause2 varchar )
AS
BEGIN
DECLARE @sql VARCHAR(2000)
SET @sql = 'SELECT * FROM myTable
WHERE fieldChar IN (@inClause1)
AND fieldInt IN (@inClause2)'
EXEC (@sql)
END
Then I call the sp like so:
EXEC sp_myProc '''Male'',''Female''','1,2,3,4'
It works fine but I'm looking for a way not to recontruct the sql statement each time and pass only the parameters.
The problem is when I tried creating the sp using the parameters I got explicit data conversion error.
Trying to cast '1,2,3,4' as int.
I tried creating a function as StringTokenizer in Java to return a table containing int but got the same problem. My function would return correct info when assigning a local( within the function to test it ) varchar set to '1,2,3,4' but when passed a value '1,2,3,4' it would not return any rows.
Thanks for your time
Sylvain