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.
Related
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;
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;
I would like to insert a value retrieved from a counter in SQL and repeat it 300 times.
Something like:
DECLARE #Counter = 0;
-- BEGIN Loop
SET #Counter = #Counter + 1
INSERT INTO tblFoo VALUES(#Counter)
-- REPEAT 300 times
How can I achieve this?
Thanks
You may try it like this:
DECLARE #i int = 0
WHILE #i < 300
BEGIN
SET #i = #i + 1
/* your code*/
END
DECLARE #first AS INT = 1
DECLARE #last AS INT = 300
WHILE(#first <= #last)
BEGIN
INSERT INTO tblFoo VALUES(#first)
SET #first += 1
END
I would prevent loops in general if i can, set approaches are much more efficient:
INSERT INTO tblFoo
SELECT TOP (300) n = ROW_NUMBER()OVER (ORDER BY [object_id])
FROM sys.all_objects ORDER BY n;
Demo
Generate a set or sequence without loops
In ssms we can use GO to execute same statement
Edit
This mean if you put
some query
GO n
Some query will be executed n times
Found some different answers that I combined to solve simulair problem:
CREATE TABLE nummer (ID INTEGER PRIMARY KEY, num, text, text2);
WITH RECURSIVE
for(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM for WHERE i < 1000000)
INSERT INTO nummer SELECT i, i+1, "text" || i, "otherText" || i FROM for;
Adds 1 miljon rows with
id increased by one every itteration
num one greater then id
text concatenated with id-number like: text1, text2 ... text1000000
text2 concatenated with id-number like: otherText1, otherText2 ... otherText1000000
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 do I populate a int column, currently empty, with random numbers with no duplicates?
If this is an existing table to which you added a new INT column, you can do something like this:
UPDATE MyTable
SET MyIntColumn = CONVERT(int, RAND(CHECKSUM(NEWID())) * 10000);
This will populate the empty column with random numbers between 1 and 10000.
I suppose you could make the column a primary key to prevent duplicates, though that's kind of a hack. You can remove the key later.
---- Create the variables for the random number generation
DECLARE #Random INT;
DECLARE #Upper INT;
DECLARE #Lower INT;
DECLARE #Index integer
---- This will create a random number between 1 and 999
SET #Lower = 1 ---- The lowest random number
SET #Upper = 999 ---- The highest random number
SET #Index = 0 --- A while loop counter
--- Loop from 0 to 10
WHILE #Index < 10
BEGIN
SELECT 'loop counter = ', #index
SELECT #Random = ROUND(((#Upper - #Lower -1) * RAND() + #Lower), 0)
--Insert #Random here.
SET #index = #index + 1
END