Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AM
Starting Member
1 Post |
Posted - 2007-04-23 : 13:15:08
|
| Guys,We are trying implement sequence objects in sql server.http://blogs.msdn.com/sqlcat/archive/2006/04/10/572848.aspxfrom the above blog we are using option 2 for simulating sequences. If you look at option 2 we have to create table and stored procedure for each sequence.Is there a way I can create a function and pass the <tablename> and it executes respective stored procedure??CREATE FUNCTION GETNEXTVAL(@TABLENAME VARCHAR(20))RETURN INTBEGINExec GetNewSeqVal_<tablename>ENDThanks |
|
|
cvraghu
Posting Yak Master
187 Posts |
Posted - 2007-04-23 : 18:09:02
|
| Is there any specific reason why you want a function to do this job? Because you cannot call stored procedures from functions. Instead you can write a stored procedure which will take the table name and call the appropriate sp using dynamic sql. CREATE PROC GETNEXTVAL(@TABLENAME VARCHAR(20))@RETURNVAL INT OUTPUTBEGINDECLARE @SQLString nvarchar(500);DECLARE @ParmDefinition nvarchar(500);DECLARE @SeqNoOutside int;SET @SQLString = N'Exec @seqnoinside = GetNewSeqVal_' + @tablename;SET @ParmDefinition = N'@seqnoinside int OUTPUT';EXECUTE sp_executesql @SQLString, @ParmDefinition, @seqnoinside=@SeqNooutside OUTPUT;SELECT @RETURNVAL = @SeqNooutsideENDRefer to http://msdn2.microsoft.com/en-us/library/ms188001.aspx to find out how to use sp_executesql and get an output value. An easier approach would be to use simple IF ELSE blocks and call appropriate sps. |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-04-23 : 19:53:21
|
also, you can avoid all this if you just use IDENTITY columns. www.elsasoft.org |
 |
|
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2007-04-24 : 10:09:04
|
| Thanks for your replyHow do I modify the below stored procedure to spit out the output value. Right now they give 'Command(s) completed successfully.' messageany suggestions/inputs would help.---- STORED PROC 1create procedure GetNewSeqVal_FACTS_CASE_ID_SEQasbegin declare @NewSeqValue int set NOCOUNT ON insert into FACTS_CASE_ID_SEQ (fname) values ('smith') set @NewSeqValue = scope_identity() delete from FACTS_CASE_ID_SEQ WITH (READPAST)return @NewSeqValueend---- STORED PROC 2CREATE PROC GETNEXTVAL(@TABLENAME VARCHAR(20), @RETURNVAL INT OUTPUT)asBEGINDECLARE @SQLString nvarchar(500);DECLARE @ParmDefinition nvarchar(500);DECLARE @SeqNoOutside int;SET @SQLString = N'Exec @seqnoinside = GetNewSeqVal_' + @tablename;SET @ParmDefinition = N'@seqnoinside int OUTPUT';EXECUTE sp_executesql @SQLString, @ParmDefinition, @seqnoinside=@SeqNooutside OUTPUT;SELECT @RETURNVAL = @SeqNooutsideEND |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
|
|
|
|
|