Focus,
Thanks for your reply. Sorry for not posting my SQL. Here it is:
Code:
ALTER PROCEDURE [dbo].[spCreateTemplateClause]
@template_id int,
@where_clause nvarchar(MAX) OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @clause AS nvarchar(MAX)
SET @clause = ''
DECLARE @type AS varchar(50)
DECLARE @value AS varchar(255)
DECLARE @combo_type AS varchar(50)
DECLARE @combo_value AS varchar(255)
DECLARE @current_type AS varchar(50)
SET @current_type = ''
DECLARE @current_combo_type AS varchar(50)
SET @current_combo_type = ''
DECLARE @join AS varchar(5)
SET @join = ''
-- Loop through all the values in the filter values table to create
-- a 'WHERE' clause for which items to include in a query.
DECLARE temp_cursor CURSOR FORWARD_ONLY STATIC FOR
SELECT [TYPE], [VALUE], COMBO_TYPE, COMBO_VALUE
FROM SCENARIO_TEMPLATE_VALUES
WHERE TEMPLATE_ID = @template_id
ORDER BY [TYPE], COMBO_TYPE
OPEN temp_cursor
-- Get the first value from the cursor
FETCH NEXT FROM temp_cursor
INTO @type, @value, @combo_type, @combo_value
-- Need to keep track of when the filter type changes. For the
-- filter, similar types should be OR'd and type changes
-- should be AND'd.
SET @current_type = @type
IF @@FETCH_STATUS = 0
SET @clause = ' WHERE '
WHILE @@FETCH_STATUS = 0
BEGIN
-- eg. "(WAREHOUSE_NAME = 'Warehouse 1')"
SET @clause = @clause + '(' + @type + ' = ''' + @value + ''')'
-- Combo values are dependant criteria... they should be AND'd with
-- the main clause from above.
IF @combo_type IS NOT NULL
SET @clause = @clause + ' AND (' + @combo_type + ' = ''' + @combo_value + ''')'
FETCH NEXT FROM temp_cursor
INTO @type, @value, @combo_type, @combo_value
IF @@FETCH_STATUS <> 0
BREAK
-- Set the clause join, depending on whether the type changes or not.
IF @type = @current_type
SET @join = ' OR '
ELSE
SET @join = ' AND '
SET @clause = @clause + @join
SET @current_type = @type
END
CLOSE temp_cursor
DEALLOCATE temp_cursor
SET @where_clause = @clause
END
The stored procedure takes in an int ID and builds the where clause I am supposed to be using for another select statement.
I am not sure if this would work with hibernate as I read in the docs it needs a resultset as output to work.
Thanks for your help,
Javid