Problem creating a GetNextKey Stored Procedure

Posted on 1998-01-28
Last Modified: 2008-02-01
I am trying to create a GetNextKey stored procedure given
a tablename passed a parameter, but have had problems creating it.

Here is what I have so far:
CREATE PROCEDURE GetNextKey @Tablename Varchar(10)

Declare @sSQLStatement Varchar(255)
        @CurKey Integer,
        @NextKey Integer
select @sSQLStatement = 'select @CurKey=max(prkey) from ' +@Tablename
Exec (@sSQLStatement)

select @NextKey = @CurKey + 1

At first I had a problem using a variable in the from
clause of an SQL statement. I got around this by building
the sql statement in a variable and executing it using
"Exec". When I did this and executed the stored proc. however, I got the following error: "Must declare variable

I am trying to find out the following:

a) If I can use a variable in the from clause of an SQL
b) How I can assign the value to @CurKey in the SQL     statement executed with "Exec".
c) How I can return @NextKey as the result of the stored
d) If someone has created a similar procedure and can show
    me ways to do it.

Thanks in advance to anyone who reads/responds to my question.
Question by:csean
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
  • 3

Author Comment

ID: 1090083
Edited text of question

Author Comment

ID: 1090084
Adjusted points to 200

Accepted Solution

Jim_SQL earned 200 total points
ID: 1090085
Hi, here is some sample code that answers your questions (I hope).

---------------- CREATE & POPULATE A TABLE ----------------
drop table TestFreddy
create table TestFreddy (prkey int identity(2,3), SomeThing varchar(15))
set nocount on
Declare @i int
select @i=1
WHILE @i<200
      insert into TestFreddy VALUES("Something"+convert(varchar, @i))
      select @i=@i+1
select * from TestFreddy
---------------- SUGGESTION FOR YOUR CODE----------------
drop proc GetNextKey
CREATE PROCEDURE GetNextKey @Tablename Varchar(30), @PrmyKeyFieldName varchar(30)
set nocount on
Declare @NextKey Integer

create table #ReturnValue(R int)

insert into #ReturnValue
EXECUTE("select max(" + @PrmyKeyFieldName + ") from " + @Tablename)

select @NextKey = R+1 from #ReturnValue

return (@NextKey)
---------------- TEST THE RESULT ----------------
Declare @NextKey int
EXEC @NextKey = GetNextKey "TestFreddy", 'prkey'
select @NextKey '@NextKey'
Using the Identity property might be of use for what you are doing (I don't know), but if it is then take a look at the code I used to populate the table... you can set the start & step values (2,3) to (1,1) if that would be better for you.

(Hope this helped.)

Author Comment

ID: 1090086
Thanks Jim_SQL,

The bottom section of your response was very helpful. I never looked into temporary tables enough. That was the key to solving this problem.


Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

717 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