Search code examples
sqlssms

How do I create a stored procedure that select from one table based on condition in another table


For Example, I have 2 tables.

Table A has Setting/isActive

Table B has Name/Ages/City

So I want to retrieve records from B only when isActive = 1 for Setting ='GetThis' in Table A

something like

if 'GetThis' = 1 from table A
then get records from table B

Solution

  • I ended up doing this

    CREATE PROCEDURE GetAllRecords
    @isActive BIT
    SET @isActive = (Select isActive FROM TableA WHERE Setting = 'GetThis')
    
    AS
    
    IF @isActive = 1
    BEGIN
    
    SELECT * FROM TableB
    
    END
    GO;