?
Solved

tsql stored procedure to return a primary key column for datatable

Posted on 2007-11-14
6
Medium Priority
?
1,310 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

800 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