Solved

tsql stored procedure to return a primary key column for datatable

Posted on 2007-11-14
6
1,307 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

707 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