Solved

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

Posted on 2008-10-12
4
376 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 125 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

751 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