Solved

tsql stored procedure to return a primary key column for datatable

Posted on 2007-11-14
6
1,305 Views
Last Modified: 2008-02-01
I want a stored procedure to return an extra unique column, for example called PrimaryKey, and it would just be an integer column that autoincremented, so its values would be 1, 2, 3,... etc

Thanks
0
Comment
Question by:shmitnols
[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
6 Comments
 
LVL 6

Expert Comment

by:Marcel Hopman
ID: 20284444
I'm not sure why you want to do that, but you could use the ROW_NUMBER() functrion

CREATE PRODEDURE ExtraKey()
AS
SELECT  ROW_NUMBER() OVER (ORDER BY TablePK) AS ExtraPK
,              TablePK
FROM      Table
0
 
LVL 13

Expert Comment

by:Wizilling
ID: 20284604
are you taking about a IDENTITY column.
It auto increments and is a int field.

http://msdn2.microsoft.com/en-us/library/aa933196(SQL.80).aspx
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20284887
so your stored proc returns data as a result set? you mentioned "extra column"
something like this?

create table UniqueKeyHolderForSomeStoredProc ( i int identity (1,1), adummy bit )

create procedure SomeStoredProc (
 @x int
)
as

declare @uniqueid int
insert into UniqueKeyHolderForSomeStoredProc (adummy) values (0)
select @uniqueid = scope_identity()
delete UniqueKeyHolderForSomeStoredProc -- keep the table empty

select @uniqueid, field1, field2, field3
from mytable
where criteria = @x
0
Technology Partners: 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!

 

Author Comment

by:shmitnols
ID: 20285078
the stored procedure returns columns, I just want it to return another column that will be unique and have values 1,2,3, etc....  
0
 
LVL 6

Accepted Solution

by:
Marcel Hopman earned 500 total points
ID: 20285432
That's exactly what this procedure does, but it only works if the table already has a Primary Key..

CREATE PRODEDURE ExtraKey()
AS
SELECT  ROW_NUMBER() OVER (ORDER BY TablePK) AS ExtraPK
,              TablePK
,              FieldA
,              FieldB
,              ....
FROM      Table
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20285526
does creating a holding table and feeding off it's identity column not work for you?
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Retrieve a Registry value using SCCM 2012. 2 44
SQL Query (lookup) 8 61
SQL syntax for max(date) 3 37
SQL State HYT00. Timeout expired proplem 8 42
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

737 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