SQLServer SQL query with a row counter

后端 未结 4 1526
花落未央
花落未央 2021-02-01 05:09

I have a SQL query, that returns a set of rows:

SELECT id, name FROM users where group = 2

I need to also include a column that has an incremen

4条回答
  •  庸人自扰
    2021-02-01 06:06

    Heres a different approach. If you have several tables of data that are not joinable, or you for some reason dont want to count all the rows at the same time but you still want them to be part off the same rowcount, you can create a table that does the job for you.

    Example:

    create table #test (
            rowcounter int identity,
            invoicenumber varchar(30)
            )
    
    insert into #test(invoicenumber) select [column] from [Table1]
    
    insert into #test(invoicenumber) select [column] from [Table2]
    
    insert into #test(invoicenumber) select [column] from [Table3]
    
    select * from #test
    
    drop table #test
    

提交回复
热议问题