UnKnown Temp table in cursor

  • hi all,

    I must feel data to a temptable but i've no idea what it's structure. So I cannot use create table (col coltype) format.

    And I'm trying to feel data from a user defined function in cursor loop.

    after feeling temptable i select data from it by dinamik sql...

    exec ('select * from #tempo' + whereclause)

    but i can't find any solution for this.

     

  • Hello,

    it seems you have some serious problems...

    How can you work with something if you don't know what's the structure?

    How can you define "whereclause" if you don't know what columns are in the table?

    Why are you using cursor? In 95% of cases, there is a better solution than cursor.

    What does "feel data" mean? If you mean INSERT data, then you can't do that in UDF.. although maybe you meant that UDF returns table and you insert the result - but then you have to know the structure of table returned by UDF...? I'm puzzled... please explain a little more your situation.

    Generally, if you want to be lazy, you can use SELECT INTO to create a table based on the result of a query. But be careful, it is better to use SELECT INTO only to create the structure, without inserting data at the same time - because of locking issues. Like this:

    /*create structure - empty table*/

    SELECT * INTO #temptable

    FROM yourquery

    WHERE 1=2

    /*insert data*/

    INSERT INTO #temptable

    SELECT *

    FROM yourquery

    WHERE your_where_clause

    It isn't recommended to use "SELECT *", you should always know and name the columns explicitly, but if you say you don't know the structure...

  • If you really need dynamic SQL you can use sp_executesql to run it, but I agree with Vladan - you've got some VERY strange requirements there!

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

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