Problem creating a GetNextKey Stored Procedure

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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cseanAuthor Commented:
Edited text of question
cseanAuthor Commented:
Adjusted points to 200
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.)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cseanAuthor Commented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.