Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying different table based on a parameter

I have a stored procedure that I would like to query either the production or the "work in progress" table, based on the parameter I am passing in. I could write two separate stored procedures, but I thought this was worth a try.

something along the lines of:

create procedure getUserDetails
    @userID int,
    @prod varchar(5)
as 
    begin
        select * from
            if (@prod = 'true')
            Begin
                userprod_table
            else
                userwip_table
            end 
    where something = 'something'
END

Is this at all possible? I wouldn't want to write 2 SP that are almost identical :-/

like image 627
Madam Zu Zu Avatar asked Mar 10 '26 12:03

Madam Zu Zu


1 Answers

Why not use a simple if(@prod = 'true') statement like below:

if (@prod = 'true')
begin
    select * from userprod_table where something = 'something'
end  else
begin
    select * from userwip_table where something = 'something'
end
like image 107
Kaf Avatar answered Mar 12 '26 03:03

Kaf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!