Solved

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

Posted on 2008-10-12
4
333 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
  • 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now