December 11, 2020 at 12:59 pm
CREATE Table TABLEA(N varchar(50),Q int,R int)
ALTER PROCEDURE [dbo].[SP](@N varchar(100),@Q int=0,@R int=0)
AS
BEGIN
DECLARE @I int = 1
BEGIN
WHILE (@I <= @R)
BEGIN
WHILE (@I <= @Q)
BEGIN
INSERT INTO [dbo].[TABLEA] SELECT @N,@Q,@R
SET @Q = @Q - 1
END
SET @R = @R -1
END
END
END
exec [dbo].[SP] 'NameA',10,2
Current TableA values after executing SP:
NQR
NameA102
NameA92
NameA82
NameA72
NameA62
NameA52
NameA42
NameA32
NameA22
NameA12
But I need the values to be inserted again based on @R value . If R is 2 then then the result in
TABLEA Should be as below..
NQR
NameA102
NameA92
NameA82
NameA72
NameA62
NameA52
NameA42
NameA32
NameA22
NameA12
NQR
NameA102
NameA92
NameA82
NameA72
NameA62
NameA52
NameA42
NameA32
NameA22
NameA12
Thanks
December 11, 2020 at 3:16 pm
Why is this necessary? Here's a solution using (two of) the fnTally function to generate the rows instead of WHILE loops. Re-using declared variables is generally not a good practice in SQL imo. Anyway, if Q and R become large-ish then this could blow up in a hurry. CROSS JOIN'ing two tally functions is not generally recommended but that's what you're asking for.
alter procedure dbo.sp(
@n varchar(100),
@q int=0,
@r int=0)
as
insert dbo.tablea(n, q, t)
select @n, fnq.N, @r
from dbo.fnTally(1, @q) fnq
cross join dbo.fnTally(1, @r) fnr;
go
exec dbo.sp 'NameA', 10, 2;
select * from dbo.tablea
order by n, q desc;
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
December 11, 2020 at 4:14 pm
thanks for the response but I need it only with a nested while loop.
December 11, 2020 at 4:39 pm
homework?
December 11, 2020 at 4:49 pm
abridged version of what I need
December 11, 2020 at 6:11 pm
got it
DECLARE @I int = 1
DECLARE @R int = 2
DECLARE @Q int = 3
DECLARE @Q1 int
SET @Q1 =@Q
DECLARE @N varchar(100)
SET @N = 'NameA'
WHILE (@I <= @R+1)
BEGIN
WHILE (@I <= @Q)
BEGIN
SELECT @N as Name,@I,@R,@Q
SET @Q = @Q - 1
END
SET @Q = @Q1
SET @R = @R -1
END
NameA123
NameA122
NameA121
NameA113
NameA112
NameA111
NameA103
NameA102
NameA101
December 11, 2020 at 6:43 pm
just wonder why people still insist in using cursors/while loops when a better solution was given.
for those seeing this thread and thinking its a good way to do- this approach is how you should NOT do it.
December 13, 2020 at 5:39 am
abridged version of what I need
I'd recommend sharing what the unabridged version is because it will be in even more of a need of speed and a While loop or rCTE isn't going to be your friend there or when it come to maintainability of the code.
got it
DECLARE @I int = 1
DECLARE @R int = 2
DECLARE @Q int = 3
DECLARE @Q1 int
SET @Q1 =@Q
DECLARE @N varchar(100)
SET @N = 'NameA'
WHILE (@I <= @R+1)
BEGIN
WHILE (@I <= @Q)
BEGIN
SELECT @N as Name,@I,@R,@Q
SET @Q = @Q - 1
END
SET @Q = @Q1
SET @R = @R -1
END
NameA123
NameA122
NameA121
NameA113
NameA112
NameA111
NameA103
NameA102
NameA101
@mtz676 ...
Please, just so we know, why does this has to be a While loop? For example, will you be calling a stored procedure in every iteration of the loop?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply