Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rmelnyck
Starting Member
4 Posts |
Posted - 2005-10-18 : 16:45:20
|
| I have a stored procedure that contains a parameter that may have multiple values or may be null. If I pass in one or more values for the parameter it works fine but if I do not pass in a parameter it gives me only the records that have a 'null' value instead of all the records.The code is below as well as the function which handles the multiple parameters.Any help would be much appreciated. The IN clause is what is messing me up, if it was '=' instead of IN it would be much easier.---------------------------------------------------------SELECT *FROM ContentINNER JOIN LinksON Content .ContentID = Links.ContentIDWHERE Content.ContentType IN ( Select value from dbo.listToTable(@vcContentType, ','))ALTER FUNCTION listToTable(@list as varchar(8000), @delim as varchar(10))RETURNS @listTable table( Position int, Value varchar(8000) )ASBEGIN declare @myPos int set @myPos = 1 while charindex(@delim, @list) > 0 begin insert into @listTable(Position, Value) values(@myPos, left(@list, charindex(@delim, @list) - 1)) set @myPos = @myPos + 1 if charindex(@delim, @list) = len(@list) insert into @listTable(Position, Value) values(@myPos, '') set @list = right(@list, len(@list) - charindex(@delim,@list)) end if len(@list) > 0 insert into @listTable(Position, Value) values(@myPos, @list) RETURNEND |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-19 : 01:35:44
|
| >>but if I do not pass in a parameter it gives me only the records that have a 'null' value instead of all the records.Then you should pass parameter to the FunctionWill it work correctly if you pass Valid data to the Function parameter?MadhivananFailing to plan is Planning to fail |
 |
|
|
rmelnyck
Starting Member
4 Posts |
Posted - 2005-10-19 : 08:41:56
|
| Thanks for the response, this is how i did it:SELECT *FROM ContentINNER JOIN LinksON Content .ContentID = Links.ContentIDWHERE @vcContentType iS NULL OR Content.ContentType IN ( Select value from dbo.listToTable(@vcContentType, ',')) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-19 : 08:50:22
|
| Is it working for you?MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|