insert into temp table based on if condition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • das

    insert into temp table based on if condition

    hello all,
    this might be simple:

    I populate a temp table based on a condition from another table:

    select @condition = condition from table1 where id=1 [this will give
    me either 0 or 1]

    in my stored procedure I want to do this:

    if @condition = 0
    begin
    select * into #tmp_table
    from products p
    inner join
    sales s on p.p_data = s.p_data
    end
    else
    begin
    select * into #tmp_table
    from products p
    left join
    sales s on p.p_data = s.p_data
    end

    Tha above query would not work since SQL thinks I am trying to use the
    same temp table twice.

    As you can see the major thing that gets effected with the condiction
    being 0/1 is the join (inner or outer). The actual SQL is much bigger
    with other joins but the only thing changing in the 2 sql's is the join
    between products and sales tables.

    any ideas gurus on how to use different sql's into temp table based on
    the condition?

    thanks
    adi

  • Chris Fulstow

    #2
    Re: insert into temp table based on if condition

    This guy had the same problem:
    Find answers to CONDITIONAL IF SATEMENT USING TEMP TABLES from the expert community at Experts Exchange


    Doesn't look like they found much of a solution :(

    Maybe you could try something with table variables and dynamic SQL?

    Comment

    • Serge Rielau

      #3
      Re: insert into temp table based on if condition

      select * into #tmp_table
      from products p
      left join
      sales s on p.p_data = s.p_data
      where s.p_data IS NOT NULL OR
      @condition <> 0
      --
      Serge Rielau
      DB2 Solutions Development
      IBM Toronto Lab

      Comment

      • Teresa Masino

        #4
        Re: insert into temp table based on if condition

        You have to create your temp table outside of your select statements
        and then insert into it. For example,

        CREATE TABLE #tmp_table
        AS SELECT *
        FROM PRODUCTS
        WHERE 1 = 2

        IF @condition = 0
        BEGIN
        INSERT #tmp_table
        SELECT *
        FROM products ....
        END
        ELSE
        ....

        Hope it helps
        Teresa

        das wrote:[color=blue]
        > hello all,
        > this might be simple:
        >
        > I populate a temp table based on a condition from another table:
        >
        > select @condition = condition from table1 where id=1 [this will give
        > me either 0 or 1]
        >
        > in my stored procedure I want to do this:
        >
        > if @condition = 0
        > begin
        > select * into #tmp_table
        > from products p
        > inner join
        > sales s on p.p_data = s.p_data
        > end
        > else
        > begin
        > select * into #tmp_table
        > from products p
        > left join
        > sales s on p.p_data = s.p_data
        > end
        >
        > Tha above query would not work since SQL thinks I am trying to use the
        > same temp table twice.
        >
        > As you can see the major thing that gets effected with the condiction
        > being 0/1 is the join (inner or outer). The actual SQL is much bigger
        > with other joins but the only thing changing in the 2 sql's is the join
        > between products and sales tables.
        >
        > any ideas gurus on how to use different sql's into temp table based on
        > the condition?
        >
        > thanks
        > adi[/color]

        Comment

        • Chris Fulstow

          #5
          Re: insert into temp table based on if condition

          Genius.

          Comment

          • das

            #6
            Re: insert into temp table based on if condition

            Thats a cool idea, I almost am half-bald over this problem.
            but for me your solution works.

            Just curious, how can we create a empty table with this statement?

            CREATE TABLE #tmp_table
            AS SELECT *
            FROM PRODUCTS
            WHERE 1 = 2

            I had to use:

            SLECT * INTO #tmp_table
            FROM PRODUCTS
            WHERE 1 = 2

            and then I used the if condition.

            thanks a lot again!

            Comment

            Working...