Why is an integer variable not accepted as a value for START WITH in sequence












8















I am trying to create dynamic start number for sequence but it is not accepting variable viz. @START_SEQ for START WITH. Please consider following code : -



CREATE PROCEDURE  [dbo].[SP_RESET_SEQ]
AS
DECLARE @START_SEQ INT =0;

BEGIN
SET @START_SEQ = (SELECT MAX(USER_ID)+1 FROM MASTER_USER);
IF OBJECT_ID('SEQ_USER_ID') IS NOT NULL
DROP SEQUENCE [dbo].[SEQ_USER_ID]

CREATE SEQUENCE [dbo].[SEQ_USER_ID]
AS [bigint]
START WITH @START_SEQ
INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999999
CACHE
END









share|improve this question

























  • Looks like the FROM keyword is missing in your inner query

    – Raj
    Nov 18 '13 at 7:05











  • I did correction in my question. @Raj

    – RAKESH HOLKAR
    Nov 18 '13 at 7:12











  • What happens if a new user is added to MASTER_USER between reading the MAX(USER_ID) value and creating the sequence?

    – ta.speot.is
    Nov 18 '13 at 7:33











  • Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

    – marc_s
    Oct 25 '17 at 12:05
















8















I am trying to create dynamic start number for sequence but it is not accepting variable viz. @START_SEQ for START WITH. Please consider following code : -



CREATE PROCEDURE  [dbo].[SP_RESET_SEQ]
AS
DECLARE @START_SEQ INT =0;

BEGIN
SET @START_SEQ = (SELECT MAX(USER_ID)+1 FROM MASTER_USER);
IF OBJECT_ID('SEQ_USER_ID') IS NOT NULL
DROP SEQUENCE [dbo].[SEQ_USER_ID]

CREATE SEQUENCE [dbo].[SEQ_USER_ID]
AS [bigint]
START WITH @START_SEQ
INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999999
CACHE
END









share|improve this question

























  • Looks like the FROM keyword is missing in your inner query

    – Raj
    Nov 18 '13 at 7:05











  • I did correction in my question. @Raj

    – RAKESH HOLKAR
    Nov 18 '13 at 7:12











  • What happens if a new user is added to MASTER_USER between reading the MAX(USER_ID) value and creating the sequence?

    – ta.speot.is
    Nov 18 '13 at 7:33











  • Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

    – marc_s
    Oct 25 '17 at 12:05














8












8








8








I am trying to create dynamic start number for sequence but it is not accepting variable viz. @START_SEQ for START WITH. Please consider following code : -



CREATE PROCEDURE  [dbo].[SP_RESET_SEQ]
AS
DECLARE @START_SEQ INT =0;

BEGIN
SET @START_SEQ = (SELECT MAX(USER_ID)+1 FROM MASTER_USER);
IF OBJECT_ID('SEQ_USER_ID') IS NOT NULL
DROP SEQUENCE [dbo].[SEQ_USER_ID]

CREATE SEQUENCE [dbo].[SEQ_USER_ID]
AS [bigint]
START WITH @START_SEQ
INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999999
CACHE
END









share|improve this question
















I am trying to create dynamic start number for sequence but it is not accepting variable viz. @START_SEQ for START WITH. Please consider following code : -



CREATE PROCEDURE  [dbo].[SP_RESET_SEQ]
AS
DECLARE @START_SEQ INT =0;

BEGIN
SET @START_SEQ = (SELECT MAX(USER_ID)+1 FROM MASTER_USER);
IF OBJECT_ID('SEQ_USER_ID') IS NOT NULL
DROP SEQUENCE [dbo].[SEQ_USER_ID]

CREATE SEQUENCE [dbo].[SEQ_USER_ID]
AS [bigint]
START WITH @START_SEQ
INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999999
CACHE
END






sql-server tsql database-sequence






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 10:47









a_horse_with_no_name

302k46463559




302k46463559










asked Nov 18 '13 at 7:02









RAKESH HOLKARRAKESH HOLKAR

1,12751635




1,12751635













  • Looks like the FROM keyword is missing in your inner query

    – Raj
    Nov 18 '13 at 7:05











  • I did correction in my question. @Raj

    – RAKESH HOLKAR
    Nov 18 '13 at 7:12











  • What happens if a new user is added to MASTER_USER between reading the MAX(USER_ID) value and creating the sequence?

    – ta.speot.is
    Nov 18 '13 at 7:33











  • Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

    – marc_s
    Oct 25 '17 at 12:05



















  • Looks like the FROM keyword is missing in your inner query

    – Raj
    Nov 18 '13 at 7:05











  • I did correction in my question. @Raj

    – RAKESH HOLKAR
    Nov 18 '13 at 7:12











  • What happens if a new user is added to MASTER_USER between reading the MAX(USER_ID) value and creating the sequence?

    – ta.speot.is
    Nov 18 '13 at 7:33











  • Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

    – marc_s
    Oct 25 '17 at 12:05

















Looks like the FROM keyword is missing in your inner query

– Raj
Nov 18 '13 at 7:05





Looks like the FROM keyword is missing in your inner query

– Raj
Nov 18 '13 at 7:05













I did correction in my question. @Raj

– RAKESH HOLKAR
Nov 18 '13 at 7:12





I did correction in my question. @Raj

– RAKESH HOLKAR
Nov 18 '13 at 7:12













What happens if a new user is added to MASTER_USER between reading the MAX(USER_ID) value and creating the sequence?

– ta.speot.is
Nov 18 '13 at 7:33





What happens if a new user is added to MASTER_USER between reading the MAX(USER_ID) value and creating the sequence?

– ta.speot.is
Nov 18 '13 at 7:33













Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

– marc_s
Oct 25 '17 at 12:05





Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all!

– marc_s
Oct 25 '17 at 12:05












2 Answers
2






active

oldest

votes


















6














You can do the same with dynamic SQL:



CREATE PROCEDURE  [dbo].[SP_RESET_SEQ]
AS
DECLARE @START_SEQ INT =0;

BEGIN
SET @START_SEQ = (SELECT MAX(USER_ID)+1 FROM MASTER_USER);
IF OBJECT_ID('SEQ_USER_ID') IS NOT NULL
DROP SEQUENCE [dbo].[SEQ_USER_ID]

DECLARE @sql NVARCHAR(MAX)

SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID]
AS [bigint]
START WITH ' + @START_SEQ
+ 'INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999999
CACHE'

EXEC(@sql)
END


As noted by ta.speot.is below (thanks!), the syntax for CREATE SEQUENCE takes a constant (see MSDN).






share|improve this answer





















  • 4





    I believe this is the only way to do it: the syntax for START WITH takes a <constant>.

    – ta.speot.is
    Nov 18 '13 at 7:34











  • Works well except variable @sql. I had to convert @START_SQL into nvarchar SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID] AS [bigint] START WITH ' + CONVERT(NVARCHAR(10), @START_SEQ) + 'INCREMENT BY 1 MINVALUE 1 MAXVALUE 99999999 CACHE

    – Camille
    Oct 30 '18 at 12:09





















1














Another example as the above one not working with me



declare @maxBookingId as int 
select @maxBookingId = max(bookingid) from booking
declare @s nvarchar(4000);
set @s = N'
CREATE SEQUENCE Invoice_Seq AS INTEGER
START WITH ' + cast(@maxBookingId as nvarchar) + '
INCREMENT BY 1
NO CYCLE;'

EXEC (@s);





share|improve this answer
























  • @maxBookingId + 1 ?

    – LukStorms
    Jan 2 at 10:52











  • No sequence itself make the incremental operation :)

    – Ahmed Salem
    Jan 2 at 11:21











  • Oh right, normally the code that uses the sequence would use a NEXT VALUE. So it's fine if that sequence starts with the current maximum id.

    – LukStorms
    Jan 2 at 11:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20041946%2fwhy-is-an-integer-variable-not-accepted-as-a-value-for-start-with-in-sequence%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














You can do the same with dynamic SQL:



CREATE PROCEDURE  [dbo].[SP_RESET_SEQ]
AS
DECLARE @START_SEQ INT =0;

BEGIN
SET @START_SEQ = (SELECT MAX(USER_ID)+1 FROM MASTER_USER);
IF OBJECT_ID('SEQ_USER_ID') IS NOT NULL
DROP SEQUENCE [dbo].[SEQ_USER_ID]

DECLARE @sql NVARCHAR(MAX)

SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID]
AS [bigint]
START WITH ' + @START_SEQ
+ 'INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999999
CACHE'

EXEC(@sql)
END


As noted by ta.speot.is below (thanks!), the syntax for CREATE SEQUENCE takes a constant (see MSDN).






share|improve this answer





















  • 4





    I believe this is the only way to do it: the syntax for START WITH takes a <constant>.

    – ta.speot.is
    Nov 18 '13 at 7:34











  • Works well except variable @sql. I had to convert @START_SQL into nvarchar SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID] AS [bigint] START WITH ' + CONVERT(NVARCHAR(10), @START_SEQ) + 'INCREMENT BY 1 MINVALUE 1 MAXVALUE 99999999 CACHE

    – Camille
    Oct 30 '18 at 12:09


















6














You can do the same with dynamic SQL:



CREATE PROCEDURE  [dbo].[SP_RESET_SEQ]
AS
DECLARE @START_SEQ INT =0;

BEGIN
SET @START_SEQ = (SELECT MAX(USER_ID)+1 FROM MASTER_USER);
IF OBJECT_ID('SEQ_USER_ID') IS NOT NULL
DROP SEQUENCE [dbo].[SEQ_USER_ID]

DECLARE @sql NVARCHAR(MAX)

SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID]
AS [bigint]
START WITH ' + @START_SEQ
+ 'INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999999
CACHE'

EXEC(@sql)
END


As noted by ta.speot.is below (thanks!), the syntax for CREATE SEQUENCE takes a constant (see MSDN).






share|improve this answer





















  • 4





    I believe this is the only way to do it: the syntax for START WITH takes a <constant>.

    – ta.speot.is
    Nov 18 '13 at 7:34











  • Works well except variable @sql. I had to convert @START_SQL into nvarchar SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID] AS [bigint] START WITH ' + CONVERT(NVARCHAR(10), @START_SEQ) + 'INCREMENT BY 1 MINVALUE 1 MAXVALUE 99999999 CACHE

    – Camille
    Oct 30 '18 at 12:09
















6












6








6







You can do the same with dynamic SQL:



CREATE PROCEDURE  [dbo].[SP_RESET_SEQ]
AS
DECLARE @START_SEQ INT =0;

BEGIN
SET @START_SEQ = (SELECT MAX(USER_ID)+1 FROM MASTER_USER);
IF OBJECT_ID('SEQ_USER_ID') IS NOT NULL
DROP SEQUENCE [dbo].[SEQ_USER_ID]

DECLARE @sql NVARCHAR(MAX)

SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID]
AS [bigint]
START WITH ' + @START_SEQ
+ 'INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999999
CACHE'

EXEC(@sql)
END


As noted by ta.speot.is below (thanks!), the syntax for CREATE SEQUENCE takes a constant (see MSDN).






share|improve this answer















You can do the same with dynamic SQL:



CREATE PROCEDURE  [dbo].[SP_RESET_SEQ]
AS
DECLARE @START_SEQ INT =0;

BEGIN
SET @START_SEQ = (SELECT MAX(USER_ID)+1 FROM MASTER_USER);
IF OBJECT_ID('SEQ_USER_ID') IS NOT NULL
DROP SEQUENCE [dbo].[SEQ_USER_ID]

DECLARE @sql NVARCHAR(MAX)

SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID]
AS [bigint]
START WITH ' + @START_SEQ
+ 'INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999999
CACHE'

EXEC(@sql)
END


As noted by ta.speot.is below (thanks!), the syntax for CREATE SEQUENCE takes a constant (see MSDN).







share|improve this answer














share|improve this answer



share|improve this answer








edited May 23 '17 at 12:02









Community

11




11










answered Nov 18 '13 at 7:31









SzymonSzymon

38.7k137898




38.7k137898








  • 4





    I believe this is the only way to do it: the syntax for START WITH takes a <constant>.

    – ta.speot.is
    Nov 18 '13 at 7:34











  • Works well except variable @sql. I had to convert @START_SQL into nvarchar SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID] AS [bigint] START WITH ' + CONVERT(NVARCHAR(10), @START_SEQ) + 'INCREMENT BY 1 MINVALUE 1 MAXVALUE 99999999 CACHE

    – Camille
    Oct 30 '18 at 12:09
















  • 4





    I believe this is the only way to do it: the syntax for START WITH takes a <constant>.

    – ta.speot.is
    Nov 18 '13 at 7:34











  • Works well except variable @sql. I had to convert @START_SQL into nvarchar SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID] AS [bigint] START WITH ' + CONVERT(NVARCHAR(10), @START_SEQ) + 'INCREMENT BY 1 MINVALUE 1 MAXVALUE 99999999 CACHE

    – Camille
    Oct 30 '18 at 12:09










4




4





I believe this is the only way to do it: the syntax for START WITH takes a <constant>.

– ta.speot.is
Nov 18 '13 at 7:34





I believe this is the only way to do it: the syntax for START WITH takes a <constant>.

– ta.speot.is
Nov 18 '13 at 7:34













Works well except variable @sql. I had to convert @START_SQL into nvarchar SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID] AS [bigint] START WITH ' + CONVERT(NVARCHAR(10), @START_SEQ) + 'INCREMENT BY 1 MINVALUE 1 MAXVALUE 99999999 CACHE

– Camille
Oct 30 '18 at 12:09







Works well except variable @sql. I had to convert @START_SQL into nvarchar SET @sql = 'CREATE SEQUENCE [dbo].[SEQ_USER_ID] AS [bigint] START WITH ' + CONVERT(NVARCHAR(10), @START_SEQ) + 'INCREMENT BY 1 MINVALUE 1 MAXVALUE 99999999 CACHE

– Camille
Oct 30 '18 at 12:09















1














Another example as the above one not working with me



declare @maxBookingId as int 
select @maxBookingId = max(bookingid) from booking
declare @s nvarchar(4000);
set @s = N'
CREATE SEQUENCE Invoice_Seq AS INTEGER
START WITH ' + cast(@maxBookingId as nvarchar) + '
INCREMENT BY 1
NO CYCLE;'

EXEC (@s);





share|improve this answer
























  • @maxBookingId + 1 ?

    – LukStorms
    Jan 2 at 10:52











  • No sequence itself make the incremental operation :)

    – Ahmed Salem
    Jan 2 at 11:21











  • Oh right, normally the code that uses the sequence would use a NEXT VALUE. So it's fine if that sequence starts with the current maximum id.

    – LukStorms
    Jan 2 at 11:29
















1














Another example as the above one not working with me



declare @maxBookingId as int 
select @maxBookingId = max(bookingid) from booking
declare @s nvarchar(4000);
set @s = N'
CREATE SEQUENCE Invoice_Seq AS INTEGER
START WITH ' + cast(@maxBookingId as nvarchar) + '
INCREMENT BY 1
NO CYCLE;'

EXEC (@s);





share|improve this answer
























  • @maxBookingId + 1 ?

    – LukStorms
    Jan 2 at 10:52











  • No sequence itself make the incremental operation :)

    – Ahmed Salem
    Jan 2 at 11:21











  • Oh right, normally the code that uses the sequence would use a NEXT VALUE. So it's fine if that sequence starts with the current maximum id.

    – LukStorms
    Jan 2 at 11:29














1












1








1







Another example as the above one not working with me



declare @maxBookingId as int 
select @maxBookingId = max(bookingid) from booking
declare @s nvarchar(4000);
set @s = N'
CREATE SEQUENCE Invoice_Seq AS INTEGER
START WITH ' + cast(@maxBookingId as nvarchar) + '
INCREMENT BY 1
NO CYCLE;'

EXEC (@s);





share|improve this answer













Another example as the above one not working with me



declare @maxBookingId as int 
select @maxBookingId = max(bookingid) from booking
declare @s nvarchar(4000);
set @s = N'
CREATE SEQUENCE Invoice_Seq AS INTEGER
START WITH ' + cast(@maxBookingId as nvarchar) + '
INCREMENT BY 1
NO CYCLE;'

EXEC (@s);






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 10:22









Ahmed SalemAhmed Salem

1,0491525




1,0491525













  • @maxBookingId + 1 ?

    – LukStorms
    Jan 2 at 10:52











  • No sequence itself make the incremental operation :)

    – Ahmed Salem
    Jan 2 at 11:21











  • Oh right, normally the code that uses the sequence would use a NEXT VALUE. So it's fine if that sequence starts with the current maximum id.

    – LukStorms
    Jan 2 at 11:29



















  • @maxBookingId + 1 ?

    – LukStorms
    Jan 2 at 10:52











  • No sequence itself make the incremental operation :)

    – Ahmed Salem
    Jan 2 at 11:21











  • Oh right, normally the code that uses the sequence would use a NEXT VALUE. So it's fine if that sequence starts with the current maximum id.

    – LukStorms
    Jan 2 at 11:29

















@maxBookingId + 1 ?

– LukStorms
Jan 2 at 10:52





@maxBookingId + 1 ?

– LukStorms
Jan 2 at 10:52













No sequence itself make the incremental operation :)

– Ahmed Salem
Jan 2 at 11:21





No sequence itself make the incremental operation :)

– Ahmed Salem
Jan 2 at 11:21













Oh right, normally the code that uses the sequence would use a NEXT VALUE. So it's fine if that sequence starts with the current maximum id.

– LukStorms
Jan 2 at 11:29





Oh right, normally the code that uses the sequence would use a NEXT VALUE. So it's fine if that sequence starts with the current maximum id.

– LukStorms
Jan 2 at 11:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20041946%2fwhy-is-an-integer-variable-not-accepted-as-a-value-for-start-with-in-sequence%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas