How to make sequence jump from 100 to 150 - sql

How to fire sequence to jump from 100 to 150 and followed by 152,153 and so on . .

If your sequence seq1 is now at 100 and you want it to go to 150 you can:
Change its increment to 50.
Use it once.
Change its increment back to 1.
In SQL terms:
alter sequence seq1 increment by 50;
select seq1.nextval from dual;
alter sequence seq1 increment by 1;

Related

Oracle sql format max number 99999999999

Is the number 9999999999999999999999999999 can be written better?
I have multiple queries with that value.
CREATE SEQUENCE "INVOICES_SEQ" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 21 CACHE 20 NOORDER NOCYCLE ;
I don't believe you need to write out the maxvalue. Use NOMAXVALUE, which caps at the 28-digit number.
You could also replace MINVALUE 1 with NOMINVALUE.
ORACLE DOCS:
NOMAXVALUE Specify NOMAXVALUE to indicate a maximum value of 10 27th power
for an ascending sequence or -1 for a descending sequence. This is the
default.
NOMINVALUE Specify NOMINVALUE to indicate a minimum value of 1 for
an ascending sequence or -10 26th power for a descending sequence. This is the
default.
CREATE SEQUENCE "invoices_seq" NOMINVALUE NOMAXVALUE INCREMENT BY 1 START WITH 21 CACHE 20 NOORDER NOCYCLE;
It is good practice to specify only non-default settings.
All of those values except the starting value are the defaults and therefore don't need to be stated (and in my view shouldn't be), so the same sequence can be created more concisely using
create sequence invoices_seq start with 21;
For the more general case in your question title, 9999999999999999999999999999 is 1e28 - 1.

SQL Server 2012 : update a row with unique number

I have a table with 50k records. Now I want to update one column of the table with a random number. The number should be 7 digits.
I don't want to do that with procedure or loop.
PinDetailId PinNo
--------------------
783 2722692
784 9888648
785 6215578
786 7917727
I have tried this code but not able to succeed. I need 7 digit number.
SELECT
FLOOR(ABS(CHECKSUM(NEWID())) / 2147483647.0 * 3 + 1) rn,
(FLOOR(2000 + RAND() * (3000 - 2000) )) AS rn2
FROM
[GeneratePinDetail]
Random
For a random number, you can use ABS(CHECKSUM(NewId())) % range + lowerbound:
(source: How do I generate random number for each row in a TSQL Select?)
INSERT INTO ResultsTable (PinDetailId, PinNo)
SELECT PinDetailId,
(ABS(CHECKSUM(NewId())) % 1000000 + 1000000) AS `PinNo`
FROM GeneratePinDetail
ORDER BY PinDetailId ASC;
Likely Not Unique
I cannot guarantee these will be unique; but it should be evenly distributed (equal chance of any 7 digit number). If you want to check for duplicates you can run this:
SELECT PinDetailId, PinNo
FROM ResultsTable result
INNER JOIN (
SELECT PinNo
FROM ResultsTable
GROUP BY PinNo
HAVING Count(1) > 1
) test
ON result.PinNo = test.PinNo;
You can create a sequence object and update your fields - it should automatically increment per update.
https://learn.microsoft.com/en-us/sql/t-sql/functions/next-value-for-transact-sql
Updated based on comment:
After retrieving the 'next value for' in the sequence, you can do operations on it to randomize. The sequence can basically be used then to create a unique seed for your randomization function.
If you don't want to create a function yourself, SQL Server has the RAND function build in already.
https://learn.microsoft.com/en-us/sql/t-sql/functions/rand-transact-sql

Bulk increment sequence within MS SQL Server

Postgres and Oracle include generate_series/connect by command to allow incrementing a sequence by more than 1. I have a need to increment a sequence by a variable amount before row insertion. For example in Postgres this would look like the query below:
select nextval('mytable_seq') from generate_series(1,3);
What would be the recommended way to accomplish this in Microsoft SQL Server?
There is a stored procedure call you can use. Alternatively, you could set up some sort of while loop that calls next value for multiple times and caches them for use later.
Old question - new answer:
Assuming you've defined your sequence as:
create sequence dbo.IdSequence
as bigint
start with 1
...you can just include the phrase next value for dbo.IdSequence as a column in a select statement. When I have a sequence values I want to be paired to a result set, I'll do something like:
select
next value for dbo.IdSequence as Seq,
someSource.Col1,
someSource.Col2 --> ... and so on
from
dbo.someSource
If I have a need for a specific number of sequence values, I'll use some kind of sql table-valued function that generates dummy values:
select
next value for dbo.IdSequence Seq
from
dbo.FromTo( 1, 5 )
Note that if you make two columns requesting values from the same sequence, they'll return the same value for each column. It's probably not what you want:
select
next value for dbo.IdSequence Seq1,
next value for dbo.IdSequence Seq2
from
dbo.FromTo( 1, 5 )
...returns something like:
Seq1 Seq2
--------------------------
549 549
550 550
551 551
552 552
553 553
The FromTo is a simple function that generates numbers. There are lots of great examples of functions that do this in (lots of) answers to this question.

What is the purpose of the "START WITH" part of a SQL sequence?

In oracle SQL, a sequence is dfined like so :
CREATE SEQUENCE sup_seq
MINVALUE 1
MAXVALUE 999
START WITH 5
INCREMENT BY 1
CACHE 20;
What is the point of having the "START WITH" part ? If we have a minimum value , doesn't that imply that we start with that value? What is useful regarding START WITH ?
Normally, it would be pretty unusual to specify both MINVALUE and START WITH when defining a sequence and to have those values be different. Personally, I don't think that I've ever explicitly specified a MINVALUE since I find START WITH to be more self-explanatory.
One case where two values would be useful would be a sequence that is set to cycle. For example
CREATE SEQUENCE sup_seq
MINVALUE 1
MAXVALUE 999
START WITH 5
INCREMENT BY 1
CACHE 20
CYCLE;
would start with 5 (the start with), generate values through 999 (the maxvalue), and then restart at 1 (the minvalue). That's not a particularly common use case but it's entirely possible.

Find value between two data range in MySql

I have a table with three fields id,minvalue and maxvalue.These fields carries multiple values like...
id Minvalue maxvalue
1 100 200
2 201 300
3 301 400
...and so on. If I supply 250 as input, how can I retrieve its corresponding id using mysql query?
Thanks
This should work:
SELECT * FROM table WHERE 250 BETWEEN maxvalue AND minvalue
SELECT Id FROM table WHERE 250 BETWEEN MinValue AND MaxValue