Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I add a row number to a reusable table w/in script?

Posted on 2008-10-12
4
Medium Priority
?
394 Views
Last Modified: 2012-05-05
Hi,

I like to add a row number to a reusable table w/in a script w/ cursor.  So far, I'm using a table variable bc the table holds less than 100 rows.  I like one column to show the rowcount, but if I create the field as an identity field and delete the rows each time, the row number does not reset to one.  any work arounds?  Thx.
0
Comment
Question by:skip1000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Expert Comment

by:Cvijo123
ID: 22700423
in SQL 2000 there is bit harder but you can do liek this


SELECT IDENTITY(int, 1, 1) AS rownumber, fieldName1, fieldName2 INTO #tmp FROM yourTable

SELECT * FROM #tmp
DROP TABLE #tmp


NOTE: keep in mind that when u selecting row from your table u cannot select identity column becouse there cant be 2 identity columns in table wich will be case in your #tmp
0
 
LVL 11

Accepted Solution

by:
Goodangel Matope earned 500 total points
ID: 22700425
Instead of DELETING from the table, you could use TRUNCATE TABLE, it resets the identity to 1.

in order to do the rownumber dynamically, you can check out the following

http://support.microsoft.com/?id=186133
0
 
LVL 5

Expert Comment

by:Cvijo123
ID: 22700427
but u can have workaround for your real identity in select statment i just tested it and it worked

so:

SELECT IDENTITY(int, 1, 1) AS rownumber, cast( yourIDColumn as int), fieldName2 INTO #tmp FROM yourTable

SELECT * FROM #tmp
DROP TABLE #tmp
0
 

Author Comment

by:skip1000
ID: 22700544
Seems like no solution if using table variable.  I don't mind temp tables as long as they don't stuff up and get orphaned, which happens all the time.  truncating temp table seems like next easiest solution tho.  thx.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question