Reputation: 103
I am writing a stored proc that calculates a WHOLE bunch of different things, but I have a bit in it, that is repeated about 9 times.
eg:
if @argA = 1 (true)
select Count(samples) from dbo.X where type = @argAType
if @argB = 1 (true)
select Count(samples) from dbo.X where type = @argBType
if @argC = 1
select Count(samples) from dbo.X where type = @argCType
and so on...
how can I write a function (or something similar) that I can pass in a bit (true or false), and other argument, and only return the result set if true???
Upvotes: 0
Views: 194
Reputation: 13511
Is this what you're looking for? This is the best I can deduce based on the question as it's currently posted.
SELECT COUNT(samples)
FROM dbo.X
WHERE
(type=@argAType AND @argA=1)
OR
(type=@argBType AND @argB=1)
OR
(type=@argCType AND @argC=1)
In function form, I think this is right:
CREATE FUNCTION GetCount(@n AS BIGINT) RETURNS BIGINT
AS
BEGIN
DECLARE @count BIGINT
SELECT @count = COUNT(samples)
FROM dbo.X
WHERE
(type=@argAType AND @argA=1)
OR
(type=@argBType AND @argB=1)
OR
(type=@argCType AND @argC=1)
RETURN @count
END
Upvotes: 1