Solved

tsql stored procedure to return a primary key column for datatable

Posted on 2007-11-14
6
1,301 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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
PHP installation issues 11 59
Query Optimization 14 42
How can i use WITH CTE for checking exist value? 3 30
2016 SQL Licensing 7 40
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
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.

815 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

8 Experts available now in Live!

Get 1:1 Help Now