Author |
Topic |
mgreen84
Yak Posting Veteran
94 Posts |
Posted - 2014-05-22 : 18:14:15
|
in the below script I have changed function getnextclaimnumber() to be a store procedure in sql server 2012, that uses a sequence generator.My question now is though how can I alter the script to execute that procedure, but also assign the return OUTPUT value to 'claim_id'RESERVE_CLAIM_NUMBER_SQL_TEMPLATE = "SELECT getnextclaimnumber() as claim_id"; * would Exec getnextclaimnumber @NextCMSClaimNumber as Claim_ID work? |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2014-05-22 : 18:54:10
|
Assuming the return value is an INT, something like this:DECLARE @RetVal intEXECUTE @RetVal = [dbo].[getnextclaimnumber] |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2014-05-22 : 20:39:21
|
totally depends on how you "changed the function to be a stored procedure". There are 3 ways to get values from an SP:- return code- result set- output variableyou would consume the output differently for each of the three techniques. Post your SP code and we'll tell you how to call it.Be One with the OptimizerTG |
|
|
mgreen84
Yak Posting Veteran
94 Posts |
Posted - 2014-05-23 : 09:41:49
|
Well the function was some java that was creating a sequence gen. But basically I'm removing all there java code and php code from doing the bulk of the work on a Postgres DB. By moving everything over to SQL Server environment, and well this particular function they had in java is simply done SQL 2012 with Sequences. So I just built out a Sequence gen and called it in this store procedure, but I place the return value in a OUTPUT variable.ALTER PROCEDURE [claims].[getnextclaimnumber] @NextCMSClaimNumber VarChar(20) OUTPUTASBEGIN SET NOCOUNT ON -- Calls next avaliable claim number in sequence -- SELECT @NextCMSClaimNumber = NEXT VALUE FOR claims.ClaimNumber -- Appends '0' to claim number -- Select '0'+ @NextCMSClaimNumber End |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2014-05-23 : 10:09:46
|
this is how you get the value of an output variable:declare @result varchar(20)exec [claims].[getnextclaimnumber] @NextCMSClaimNumber = @result OUTPUTselect @result as [ResultOfGetNextClaimNumber] If you're new to Sql Server then you may not be aware of IDENTITY columns. They are a built in sequence object by table. Not sure why you are converting the sequence to varchar and prepending a '0' but if that is necessary for some reason you could add a computed column based on the identity value. Something like this:create table #test (ClaimNumber_ident int identity(1,1) , ClaimNumber as ('0' + convert(varchar(20), [ClaimNumber_ident])) , otherCols int)insert #test (otherCols) values (10),(11),(12)select * from #testOUTPUT:ClaimNumber_ident ClaimNumber otherCols----------------- --------------------- -----------1 01 102 02 113 03 12 EDIT:you could also leave the value as integer and perform that conversion only when you display the value.One peculiarity of sequence objects is that when sql server service restarts for any reason (like a server reboot) the next value jumps by whatever the cache value is. You can configure the cache size or set it to [no cache] but there will be a performance hit and disk io impact. This behavior doesn't happen with identity columns.Be One with the OptimizerTG |
|
|
mgreen84
Yak Posting Veteran
94 Posts |
Posted - 2014-05-23 : 11:31:32
|
Thanks -Well varchar and prepending 0 was requested. I went with the Seq gen, because the claim# needed to start at 500001. Can't pick where I want the identity value to start.Although how would I prepend a 0 to a int?quote: Originally posted by TG this is how you get the value of an output variable:declare @result varchar(20)exec [claims].[getnextclaimnumber] @NextCMSClaimNumber = @result OUTPUTselect @result as [ResultOfGetNextClaimNumber] If you're new to Sql Server then you may not be aware of IDENTITY columns. They are a built in sequence object by table. Not sure why you are converting the sequence to varchar and prepending a '0' but if that is necessary for some reason you could add a computed column based on the identity value. Something like this:create table #test (ClaimNumber_ident int identity(1,1) , ClaimNumber as ('0' + convert(varchar(20), [ClaimNumber_ident])) , otherCols int)insert #test (otherCols) values (10),(11),(12)select * from #testOUTPUT:ClaimNumber_ident ClaimNumber otherCols----------------- --------------------- -----------1 01 102 02 113 03 12 EDIT:you could also leave the value as integer and perform that conversion only when you display the value.One peculiarity of sequence objects is that when sql server service restarts for any reason (like a server reboot) the next value jumps by whatever the cache value is. You can configure the cache size or set it to [no cache] but there will be a performance hit and disk io impact. This behavior doesn't happen with identity columns.Be One with the OptimizerTG
|
|
|
mgreen84
Yak Posting Veteran
94 Posts |
Posted - 2014-05-23 : 11:37:09
|
Ahhh nvm I see sql 2012 has the CONCAT(), cool! |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2014-05-23 : 13:09:20
|
>> Can't pick where I want the identity value to startyes you can.Either when the identity column is created or via a DBCC CheckIdent RESEED >>Although how would I prepend a 0 to a int?You can't. You must convert the number to a character type data type in order to prepend '0'.This applies to CONCAT() as well. Only works on strings.Be One with the OptimizerTG |
|
|
mgreen84
Yak Posting Veteran
94 Posts |
Posted - 2014-05-23 : 14:56:29
|
Actually Select concat(0,@NextCMSClaimNumber)works fine.quote: Originally posted by TG >> Can't pick where I want the identity value to startyes you can.Either when the identity column is created or via a DBCC CheckIdent RESEED >>Although how would I prepend a 0 to a int?You can't. You must convert the number to a character type data type in order to prepend '0'.This applies to CONCAT() as well. Only works on strings.Be One with the OptimizerTG
|
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2014-05-23 : 15:00:20
|
@NextCMSClaimNumber is varchar. 0 is being implicitly converted by sql server to varchar so that it can concatenate it.EDIT:Concat is a definitely a handy function but you should be aware for efficiency sake that sql has to do a lot of work if you pass many arguments of different data types.So if you use a home grown verion of identity and you have a lot of concurrency you may see a lot more blocking then is necessary.Be One with the OptimizerTG |
|
|
|