Article View: comp.database.ms-sqlserver
Article #4Re: Help: Need help creating a GetNextKey Stored Procedure
From: Steve Robinson
Date: Wed, 28 Jan 1998 00:00
Date: Wed, 28 Jan 1998 00:00
80 lines
3113 bytes
3113 bytes
Sean,
The way to do this is as follows. i have made it possible for you to change
the Key Field name :
create table test2 (keyField int)
go
insert into test2 values (10)
go
CREATE PROCEDURE GetNextKey @Tablename Varchar(30),@KeyField Varchar(30)
AS
Declare @NextKey Integer
create table #Curkey (CurKey int)
insert into #Curkey Exec ('select max('+@keyField+') from '+@Tablename)
select Curkey + 1 from #Curkey
go
exec GetNextKey "test2","KeyField"
I hope this was of use to you.
Steve Robinson
steve@Samurai1.demon.co.uk
Sean Chard wrote in message <34CF5378.5F69@recruiter.ca>...
>Hello.
>
>I am trying to create a stored procedure to get the next available
>key from a specified table, so I can use it in my Delphi app.
>
>Here is the stored proc. as I have it 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
>-------------------------------------------
>
>The first problem I encountered was specifying the tablename in the
>from clause of the SQL statement. I got around this by building a string
>containing the SQL statement and executing it with "Exec". However, when
>I do this, I get a "Must declare variable '@CurKey'" error.
>
>I am trying to find out the following:
>a) If there is an easier way to use a variable in a from clause of an
> SQL statement
>b) If there is a way of assigning the value to the @CurKey variable
> in the SQL statement executed with "Exec"
>c) How I can return the variable @NextKey as the result of the stored
> procedure back to my Delphi app.
>d) If someone out there as written a similar stored proc. and can
>show me ways to do it.
>
>Reply via email to: sean@recruiter.ca
>
>Thanks in advance to anyone who replies.
>Sean Chard
>
>
>
>
>--
>---------------------
>Sean S. Chard
>Programmer / Analyst - The Recruiter Inc.
>Home: (416) 244-3100 Work: (905) 773-0570
>Email: sean@recruiter.on.ca WWW:
>http://www.recruiter.on.ca/people/sean
>
Technical Details
Message-ID:
<#UzMpIBL9GA.301@uppssnewspub04.moswest.msn.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<34CF5378.5F69@recruiter.ca>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10f695,d283056a621e56c3 X-Google-Attributes: gid10f695,public X-Google-Thread: 10de66,d283056a621e56c3 X-Google-Attributes: gid10de66,public X-Google-Thread: f8191,d283056a621e56c3 X-Google-Attributes: gidf8191,public X-Google-Thread: 11301e,d283056a621e56c3 X-Google-Attributes: gid11301e,public From: "Steve Robinson" <steve@samurai1.demon.co.uk> Subject: Re: Help: Need help creating a GetNextKey Stored Procedure Date: 1998/01/28 Message-ID: <#UzMpIBL9GA.301@uppssnewspub04.moswest.msn.net> X-Deja-AN: 320013700 References: <34CF5378.5F69@recruiter.ca> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4 Newsgroups: comp.database.ms-sqlserver,microsoft.public.sqlserver.server,microsoft.public.sqlserver.programming,borland.public.delphi.database.sqlservers