Solved

Problem creating a GetNextKey Stored Procedure

Posted on 1998-01-28
4
362 Views
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)
AS

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
'@CurKey'".

I am trying to find out the following:

a) If I can use a variable in the from clause of an SQL
     statement.
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
    procedure.
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.
0
Comment
Question by:csean
  • 3
4 Comments
 

Author Comment

by:csean
ID: 1090083
Edited text of question
0
 

Author Comment

by:csean
ID: 1090084
Adjusted points to 200
0
 
LVL 2

Accepted Solution

by:
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
GO
create table TestFreddy (prkey int identity(2,3), SomeThing varchar(15))
GO
set nocount on
Declare @i int
select @i=1
WHILE @i<200
    BEGIN
      insert into TestFreddy VALUES("Something"+convert(varchar, @i))
      select @i=@i+1
    END
GO
select * from TestFreddy
GO
---------------- SUGGESTION FOR YOUR CODE----------------
drop proc GetNextKey
GO
CREATE PROCEDURE GetNextKey @Tablename Varchar(30), @PrmyKeyFieldName varchar(30)
AS
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)
GO
---------------- TEST THE RESULT ----------------
Declare @NextKey int
EXEC @NextKey = GetNextKey "TestFreddy", 'prkey'
select @NextKey '@NextKey'
GO
/*
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.)
Jim
*/
0
 

Author Comment

by:csean
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.

Sean
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

777 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