Solved

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

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

624 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