Thursday 18 July 2013

Increase MAXSEQUENCE in SQL Server

Some times developers will insert records using sql script.

Oracle database : you can get sequenceid by using NEXTVAL.

SQL Server : There is no function called NEXTVAL. Below are the steps you can follow :


SET NOCOUNT ON;

--Declare integer variable
declare @seqid int

--Update MAXSEQUENCE table by increasing MAXRESERVED value+1.
update [maxsequence] set [maxreserved] = [maxreserved]+1 where [tbname] = 'MAXATTRIBUTE' and [name] = 'MAXATTRIBUTEID'
GO

--Get MAXRESERVED from MAXSEQUENCE table and Set to integer variable (in this case @seqid)
SET @seqid = (select maxreserved from maxsequence  where [tbname] = 'MAXATTRIBUTE' and [name] = 'MAXATTRIBUTEID')

--When inserting use @seqid at your sequenceid value. Below case, MAXATTRIBUTEID value replaced with @seqid.
INSERT INTO MAXATTRIBUTE
 (MAXATTRIBUTEID,OBJECTNAME,ATTRIBUTENAME,.................) VALUES
( @seqid,<OBJECTNAME>,<ATTRIBUTENAME>,.........)
GO

For multiple inserts, add 'update step' and 'set sequenceid in variable step' before every insert :


--Update MAXSEQUENCE table by increasing MAXRESERVED value+1.
update [maxsequence] set [maxreserved] = [maxreserved]+1 where [tbname] = '<OBJECTNAME>' and [name] = '<OBJECTSEQUENCEIDNAME>'
GO

--Get MAXRESERVED from MAXSEQUENCE table and Set to integer variable (in this case @seqid)
SET @seqid = (select maxreserved from maxsequence  where [tbname] = '<OBJECTNAME>' and [name] = '<OBJECTSEQUENCEID NAME>')


INSERT INTO <OBJECTNAME>
 (OBJECTSEQID,,.................) VALUES
( @seqid,,.........)
GO

Hope it will be useful. :)


2 comments:

Anonymous said...

Doesnt this require the server to be shutdown while executing the scripts and restart it back once its done?

NJSR said...

Recommended way is, any maximo backend changes need server restart or just turn on admin mode and off to get changes affective.

Maximo SOAPUI error javax.xml.ws.WebServiceException

SOAPUI error while sending payload to below url (in cluster environment) http://localhost:9080/meaweb/services/MXASSET Error : <f...