if statements in a proc?
I need to to build a temp table and I was thinking of doing unions and using if statements. I am passing values from a multi dropdown in a report. So I pass that to a proc, I was to do like a "If variable contains, then do the union select * into #tempTable from OffenderAddress if varable x contains '1' then union select * into #tempTable from OffenderAddressSchool if varable x contains '2' then union select * into #tempTable from OffenderAddressEmployment www.helixpoint.com
April 21st, 2011 3:20pm

Why don't you create a function which can pass multi-valued parameter to a stored proc CREATE FUNCTION [dbo].[FN_MULTI_VALUE_PARAM] (@RepParameter nvarchar(4000 ) ,@Delimeter char(1)= ',') RETURNS @Values TABLE (Param nvarchar(4000)) AS BEGIN DECLARE @chrind INT DECLARE @Piece nvarchar(100) SELECT @chrind = 1 WHILE @chrind > 0 BEGIN SELECT @chrind = CHARINDEX(@Delimeter,@RepParameter) IF @chrind > 0 SELECT @Piece = LEFT(@RepParameter,@chrind - 1) ELSE SELECT @Piece = @RepParameter INSERT @Values(Param) VALUES(CAST(@Piece AS VARCHAR)) SELECT @RepParameter = RIGHT(@RepParameter,LEN(@RepParameter) - @chrind) IF LEN(@RepParameter) = 0 BREAK END RETURN END AND THEN USE THE BELOW SQL STATEMENT INSIDE YOUR STORED PROC: column_name IN( SELECT Param FROM dbo.FN_MULTI_VALUE_PARAM(@Parameter_name,',')) Plz mark as Answer if your problem is resolved. Reddy
Free Windows Admin Tool Kit Click here and download it now
April 21st, 2011 3:59pm

Actually you're kind of close. SSRS passes a multi value as a single, comma separated string. So if all 3 choices are selected, the passed values would look like this: @Parameter = '1,2' So to use this in your scenario, the SP sql would look something like this... INSERT INTO #TempTable (ColumnList) SELECT ColumnList FROM OffenderAddress IF @Parameter LIKE '%1%' BEGIN INSERT INTO #TempTable (ColumnList) SELECT ColumnList FROM OffenderAddressSchool END IF @Parameter LIKE '%2%' BEGIN INSERT INTO #TempTable (ColumnList) SELECT ColumnList FROM OffenderAddressEmployment END A UNION isn't really applicable in this situation. Adding rows to a table already "unions" them together. Jason Long
April 22nd, 2011 1:25am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics