"But the string is being passed in for both stored procs, so it wouldn't matter which way you went"Sorry Tara, I probably wasn't clear.I think an Sproc that does something like:CREATE PROCEDURE MySProc @strParam1 varchar(100), @intParam2 int, ... @strorderByColName varchar(100)ASSELECT Col1, Col2, ...FROM MyTableWHERE (@strParam1 IS NULL OR MyCol1 = @strParam1) OR (@intParam2 IS NULL OR MyCol2 = @intParam2) ...ORDER BY CASE WHEN @strorderByColName = 'MyCol1' THEN MyCol1 ELSE NULL END, CASE WHEN @strorderByColName = 'MyCol2' THEN MyCol2 ELSE NULL END, ...
is likely [more particularly on large tables / lots of JOINs involved / etc.] to perform better using:CREATE PROCEDURE MySProc @strParam1 varchar(100), @intParam2 int, ... @strorderByColName varchar(100)ASDECLARE @strSQL nvarchar(4000)SELECT @strSQL = 'SELECT Col1, Col2, ...FROM MyTableWHERE 1=1 ' + CASE WHEN @strParam1 IS NULL THEN '' ELSE ' AND MyCol1 = @strParam1' END + CASE WHEN @intParam2 IS NULL THEN '' ELSE ' AND MyCol2 = @intParam2' END ... + ' ORDER BY ' + @strorderByColNameEXEC sp_ExecuteSQL @strSQL, N'@strParam1 varchar(100), @intParam2 int', @strParam1, @intParam2
notwithstanding the fact that the second, Dynamic SQL, solution requires giving the user permissions on the underlying table(s)Kristen