Solved

tsql stored procedure to return a primary key column for datatable

Posted on 2007-11-14
6
1,296 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
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 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

9 Experts available now in Live!

Get 1:1 Help Now