Insert into #tableerror

  • Hi Guys,

    I have temp table #temptbl with two columns

    I am inserting into two column by calculating some formula with exec.

    INSERT INTO #TEMPTBL (formula,final_formula) EXEC ('SELECT ' + @FORMULA) , exec('select'+ @FINAL_FORMULA)

    this is giving error

    How to insert two values in to the temporary table?

    Thanks;)

    Ramaa

  • INSERT INTO #TEMPTBL (formula, final_formula) VALUES (@FORMULA, @FINAL_FORMULA)

  • Rama (6/5/2008)


    Hi Guys,

    I have temp table #temptbl with two columns

    I am inserting into two column by calculating some formula with exec.

    INSERT INTO #TEMPTBL (formula,final_formula) EXEC ('SELECT ' + @FORMULA) , exec('select'+ @FINAL_FORMULA)

    this is giving error

    How to insert two values in to the temporary table?

    Thanks;)

    Several issues:

    - You can't do dynamic concatenations within the EXEC statement. build an appropriate complete statement, and then send it to Exec.

    - You can't use multiple execs the way you are. Not sure I can explain it well - but that just won't fire.

    Why not combine the 2 statements into a exec SQL statement, and exec that (just once)

    as in something kind of like this:

    declare @sql varchar(max);

    set @sql='SELECT '+@formula+','+@final_formula

    insert #mytable(formula, final_formula)

    exec(@SQL)

    Of course - @SQL need to contain a valid SQL statement so you might need to massage the two variables a little.

    Otherwise you're looking at using something like sp_executeSQL.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Hi Matt

    Thank You,

    I did the same thing after the posting in the forums. It worked.

    Ramaa

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply