Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS Deployment problem 5 67
SQL Dump exec output to table 3 23
Problems "swapping" SQL Server DBs 2 24
Setting variables in a stored procedure 5 24
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

820 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