Dood
Dood

Reputation: 23

how to count the rows in a table using t-sql?

S.NO    MATEL     PRODUCT
1       STEEL     TANDEM
5       IRON      COMMUTER
7       FIBER     TOURING

I Need to count the rows in the above table and insert in next rows without changing names of matel and product.

8        STEEL     TANDEM
12       IRON      COMMUTER
14       FIBER     TOURING

Can you guys help me in this query

Upvotes: 2

Views: 90

Answers (1)

Quassnoi
Quassnoi

Reputation: 425291

INSERT
INTO    mytable
SELECT  no + maxno, matel, product
FROM    mytable
CROSS JOIN
        (
        SELECT  MAX(no) maxno
        FROM    mytable
        ) q

If no is an identity, you'd need to issue

SET IDENTITY_INSERT mytable ON

prior to running the query and set if back to off afterwards.

Upvotes: 1

Related Questions