Adding A "Counter" Column ?

  • Hello-

    Anyone know a way (without cursors) to add a "Counter" column to a ResultSet ??

    Select ?? As Counter, Name From People

    Counter Name

    ------- ------

    1 Jane

    2 Joe

    3 Jack

    I guess it would have to be efficient as well 🙁

    😉

  • Try:

    Declare @Select Varchar(500)

    Set @Select='Select c1, c2, c3 from t1'

    Create Table #temp

    (id int identity (1,1) not null,

    c1,

    c2,

    c3)

    Insert #temp

    EXEC (@Select)

    Select * from #Temp

  • Hi,

    I don't know if you can do this with T-SQL code but you may want to create a temp table with an identity field. When you fill the table with the results of the query, the identiy field will automatically increment.

    Mike

  • Try this:

    SELECT IDENTITY(int, 1,1) AS [ID], blah0, blah1, blah2 INTO xTable

    FROM blah_table

    This will create the table xTable with your result set and an identity column. You can then select from this table and then drop it when done.

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

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