how to roll back a sequence in sql server - sql-server

I have declared this sequence:
CREATE SEQUENCE [dbo].[Monitor_Seq]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
CACHE
GO
and I use it like this:
declare #y int
declare #i int
select #y = 1
while #y < 100 begin
SELECT #i = NEXT VALUE FOR Monitor_Seq;
PRINT CAST ( #I AS VARCHAR)
-- use #i here
select #y = #y + 1
end /*while*/
the question is:
where it says in the code:
-- use #i here
suppose I decide not to carry on with a particular number, how can I revert it back my sequence?
Let's say when I get the NEXT VALUE = 10
situation made me NOT to carry on with the number 10.
I want that on the next time I get the
SELECT #i = NEXT VALUE FOR Monitor_Seq;
I want to get #i = 10 again.
is this possible?

You can restart a sequence:
ALTER SEQUENCE Monitor_Seq RESTART WITH 10;

Related

How to split a dynamic string in rows (right to Left)

This query converts dynamic int to binary, but I want to split into rows.
declare #value int
set #value = 96
declare #result varchar(32)
set #result = ''
while 1 = 1
begin
select
#result = convert(char(1), #value % 2) + ',' +#result,
#value = convert(int, #value / 2)
if #value = 0 break
end
select substring(#result, 1, len(#result)-1) as result
Please help me to find a solution.
This is the result of my query.
1,1,0,0,0,0,0
My question is: how can I split this result into rows from right to left?
My result will need to be this (I'm trying to insert into a #table):
0
0
0
0
0
1
1
Thanks
Using a WHILE seems like a really bad idea. If you want to achieve what you have this would be a far faster solution:
DECLARE #I int = 96
SELECT CONVERT(bit,#i & V.b)
FROM (VALUES(1),(2),(4),(8),(16),(32),(64)) V(b)
ORDER BY V.b;

How to draw a triangle in SQL Server?

How to draw triangles in SQL Server as shown below?
I want to implement it by using WHILE loops, but I am unable to print 20 '*' in a single line in SQL Server.
How can I achieve this?
Use REPLICATE inside a WHILE. I think, you can achieve your desired output if you do it correctly?
DECLARE #i INT = 20
WHILE(#i>0)
BEGIN
PRINT REPLICATE('* ', #i);
SET #i = #i - 1;
END
You can use REPLICATE to repeat a character a certain number of times. To generate a sequence of numbers from 1 to 20 you don't need a WHILE anyway - SQL doesn't really need the WHILE statement to work with data.
Number sequences are always useful which is why almost every SQL developer creates a Numbers table.
If you don't already have one, a quick and dirty way to generate 20 numbers is to select the top 20 rows from a systems table, and use ROW_NUMBER to calculate row numbers eg:
select top 20 replicate('*',21-row_number() over (order by id) )
from sys.sysobjects
With a Numbers table, the query is simpler:
select replicate('*',Number )
from dbo.Numbers
where Numbers.Number <= 20
order by Number desc
Numbers tables are extremely useful, eg for sets of elements like 200 days starting from 2017/1/1 :
select dateadd(d,Number,cast('20170101' as date))
from dbo.Numbers
where Numbers.n<= 20
order by Number desc
Try This,
DECLARE #StrLen INT = 20
WHILE #StrLen >= 1
BEGIN
PRINT REPLICATE('*',#StrLen)
SET #StrLen = #StrLen - 1
END
Without replicate() solution
BEGIN
DECLARE #i int = 20
DECLARE #j int
DECLARE #line varchar(max)
WHILE #i > 0
BEGIN
SET #line = ''
SET #j = 0
WHILE #j < #i
BEGIN
SET #line += '* '
SET #j = #j + 1
END
PRINT #line
SET #i = #i - 1
END
END
Using recursive CTE (Press Ctrl+T for results in text)
;WITH A AS (
SELECT REPLICATE('* ', 20) X
UNION ALL
SELECT LEFT(X, LEN(X) - 2)
FROM A
WHERE X > '* '
)
SELECT * FROM A
Another way:
;WITH A AS (
SELECT 20 X
UNION ALL
SELECT X - 1 FROM A
WHERE X > 1
)
SELECT REPLICATE('* ', X) FROM A

SQL Server : update every 5 records with serial number

I'd like to add serial number based on every 5 records. There are some columns in my table: flag, seq_no and page_no.
If seq_no is equal to 1,2,3,4,5, page_no would be updated and set to 1.
If seq_no is equal to 6,7,8,9,10, page_no would be updated and set to 2, and so on.
I'm using SQL Server 2000.
My code as below but wrong:
--set page_no
Declare #page_number int
Declare #i int, #k int
set #i = 1
set #k = 1
while #k <= 5
begin
update dbo.test
set #page_number = page_no = case
when #k = 5 then (#i + 1)
else #i
end
where flag = 'right'
set #k = #k + 1
end
How can I do so? Please help me and so much thanks.
You could try something like:
UPDATE dbo.test
SET page_no = ((seq_no - 1) / 5) + 1
This will set the value of page_no to the value you want based on the value of seq_no.

update value based on row number?

i'm pretty new to SQL and stored procedures and i'm a bit stuck - so any help would be appreciated
how do i loop through each row and assign it the random value i'm generating?
Here is my Storedproc:
CREATE PROCEDURE StoredProc8
AS
BEGIN
DECLARE #total INT
DECLARE #Count INT = 0
DECLARE #Random INT = 0
SELECT #total = COUNT(CustomerID) FROM Customers
WHILE(#Count<= #total)
BEGIN
SELECT #Random = 2 * RAND()
EXEC ('update Customers set col1= ' + #Random )
SELECT #Count = #Count+1
END
END
If you simple need to assign 0 or 1 randomly - you can use RAND() with random seed:
UPDATE Customers SET COL1 = RAND(CHECKSUM(NEWID()))*2
Demo: http://sqlfiddle.com/#!3/31699/9

how can I add 2 digit together in SQL Server 2000

suppose I have a number 62. This is composed of 2 digits
How can add 2 digit together divide by 10 and if result = something like 6.2 just take reminder
declare #Number int,#Result int
set #Number =62
if len(#Number) > 1
set #Result=????=--Add 6 and 2 =8
set #result=#result % 10 --Mod operator
print #result
-- the result should be 2 in this case
what Am I doing wrong?
Thanks a lot
set #tens = floor(#Number / 10);
set #ones = #number - #tens;
set #Result = #tens + #ones;
Or use left and right to access substrings.
I have no way to try it:
DECLARE #Number VARCHAR(2)
SET #Number = '62'
declare #firstNum INT, #secondNum INT
SET #firstNum = CAST(SUBSTRING(#Number, 1, 1) AS INT)
SET #secondNum = CAST(SUBSTRING(#Number, 2, 1) AS INT)
DECLARE #Result int
SET #Result = (#firstNum + #secondNum) % 10
I think #Number % 10is what you are looking for. It returns the last digit of any number. 62 -> 2, 97 -> 7, etc...
Update:
I may have misunderstood the question. Maybe you want 10 % ((#Number / 10) + #Number) instead.
(#Number / 10) + #Number is the sum of the digits of a two-digit number.
If the number is always two digits, then I would use LEFT(#Number,1) and RIGHT(#Number,1) to access each digit. If it's not, comment back and I'll give you some help with the loop you'll need.
More pressing an issue is that you're expecting 2, but the result of 8 mod 10 is 8. If you're looking for 2, the sum is 10 mod 8 (10 % #Result).
In any case, hit me back if this doesn't answer exactly what you wanted.
Try this
declare #myNumber float,#result float
set #myNumber = 62
select
#result = (case when len(#myNumber) = 1 then #myNumber
else (#myNumber /10) + cast(#myNumber as int) % 10 end)
select [Output] = STUFF(
cast(#result as varchar(50))
,1
,charindex('.',cast(#result as varchar(50)))
,'')
Output
2
Hope this helps
And if u just want to add 2 numbers try this(though it has been implemented above)
declare #myNumber int,#result int
set #myNumber = 62
select
Result = (case when len(#myNumber) = 1 then #myNumber
else (#myNumber /10) + #myNumber % 10 end)
Result
8
So I guess you want something like this - parse the string representing your number, adding up the individual digits as integer values. This gives you a total result at the end - then you do whatever you need to do with that. This code works for any length of string (up to 50 characters = 50 digits in your original number):
DECLARE #Number INT
SET #Number = 62
DECLARE #NumString VARCHAR(50)
SET #NumString = CAST(#Number AS VARCHAR(50))
DECLARE #Index INT
SET #Index = 1
DECLARE #Sum INT
SET #Sum = 0
WHILE #Index <= LEN(#NumString)
BEGIN
SET #Sum = #Sum + CAST(SUBSTRING(#NumString, #Index, 1) AS INT)
SET #Index = #Index + 1
END
SELECT #Sum AS 'Sum of all digits'
With the initial value of "62" for #Number, I get a result of 8 - now you can continue on using that value.
If you need this function often, I would probably encapsulate it into a user-defined function so you can call it from everywhere in your code.

Resources