August 18, 2008 at 5:20 am
karthikeyan (8/18/2008)
And, sorry, the Tally table is not the solution here...
How and why ? Can you explain it ?
Yes... the OP gave us a table with numbers in it and is required to use just that table. That's why it's not the solution here.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 18, 2008 at 5:29 am
DECLARE@Loop INT,
@Value VARCHAR(3)
SELECT@Loop = 1,
@Value = NumberValue
FROMNumber
WHERENumberID = 1
WHILE @Loop < 8
BEGIN
PRINTREPLICATE(@Value, 4 - ABS(4 - @Loop))
SET@Loop = @Loop + 1
END
N 56°04'39.16"
E 12°55'05.25"
August 18, 2008 at 5:35 am
Yes... the OP gave us a table with numbers in it and is required to use just that table. That's why it's not the solution here.
oh...yeah ! I got it ! We need to use that table only.
Apart from the requirement, how about my code ?
I am expecting your valuable comments !
karthik
August 18, 2008 at 5:09 pm
Like I said, the Tally table is unnecessary here. If you want to make something that will be programmable for this, why are you still referring to the OP's number table? Use one or the other but not both.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 18, 2008 at 7:41 pm
Trevor,
This thread kinda got hijacked... are you all set?
--Jeff Moden
Change is inevitable... Change for the better is not.
August 19, 2008 at 1:41 am
yes yes, i am still here :D, check looking at all the solutions, i used this peace of code in my SP and it seems to be working just the i need it 2 🙂
DECLARE @Loop1 INT, @Loop2 INT
SET @Loop1 = 0
SET @Loop2= 3
WHILE @Loop1 <= 7
BEGIN
IF @Loop1 <=4
BEGIN
PRINT REPLICATE (@NumberValue, @Loop1)
SET @Loop1 = @Loop1 + 1
END
ELSE
BEGIN
PRINT REPLICATE(@NumberValue,@Loop2)
SET @Loop2 = @Loop2 -1
SET @Loop1 = @Loop1 +1
END
END
August 19, 2008 at 1:54 am
here is my whole procedure 😉
:hehe:
CREATE PROC sp_Display
@NumberId INT,
@AlphabetID INT
AS
DECLARE @Loop1 INT, @Loop2 INT,@Loop3 INT, @Loop4 INT, @NTimesUsed INT, @ATimesUsed INT,@AlphaValue CHAR(1), @alphaid INT, @NumID INT, @NumberValue INT
SET @NumberValue = (SELECT NumberValue FROM Number WHERE NumberID = @NumberID)
SET @Loop1 = 0
SET @Loop2 = 3
SET @AlphaValue = (SELECT AlphabetValue FROM Alphabet WHERE AlphabetID = @AlphabetID)
SET @alphaid = (SELECT AlphabetID FROM Alphabet WHERE AlphabetID = @AlphabetID)
SET @NumID = (SELECT NumberID FROM Number WHERE NumberID = @NumberID)
UPDATE Alphabet
SET AlphabetTimesUsed = AlphabetTimesUsed + 1
WHERE AlphabetID = @alphaid
SET @ATimesUsed = (SELECT AlphabetTimesUsed FROM Alphabet WHERE AlphabetID = @AlphabetID)
SET @ATimesUsed = @ATimesUsed + 1
UPDATE Number
SET NumberTimesUsed = NumberTimesUsed + 1
WHERE NumberID = @NumID
SET @NTimesUsed = (SELECT NumberTimesUsed FROM Number WHERE NumberID = @NumberID)
SET @NTimesUsed = @NTimesUsed + 1
PRINT( 'Alphabet '+@AlphaValue+ ' Number ' +CAST(@NumberValue AS VARCHAR(40))+' was selected')
PRINT ' '
PRINT 'Alphabet ID: '+(CAST(@AlphaID AS VARCHAR(40)))
PRINT 'Alphabet Value: '+@AlphaValue
PRINT 'Alphabet Number Times Used: '+(CAST(@ATimesUsed AS VARCHAR(40)))
PRINT ' '
PRINT 'Number ID: '+(CAST(@NumID AS VARCHAR(40)))
PRINT 'Number Value: '+(CAST(@NumberValue AS VARCHAR(40)))
PRINT 'Number Times Used: '+(CAST(@NTimesUsed AS VARCHAR(40)))
PRINT ''
PRINT 'Max Length:4'
WHILE @Loop1 <= 7
BEGIN
IF @Loop1 <=4
BEGIN
PRINT REPLICATE (@NumberValue, @Loop1)
SET @Loop1 = @Loop1 + 1
END
ELSE
BEGIN
PRINT REPLICATE(@NumberValue,@Loop2)
SET @Loop2 = @Loop2 -1
SET @Loop1 = @Loop1 +1
END
END
SET @AlphaValue = (SELECT AlphabetValue FROM Alphabet WHERE AlphabetID = @AlphabetID)
SET @ATimesUsed = 0
SET @Loop3 = 0
SET @Loop4 = 3
WHILE @Loop3 <= 7
BEGIN
IF @Loop3 <=4
BEGIN
PRINT REPLICATE (@AlphaValue, @Loop3)
SET @Loop3 = @Loop3 + 1
SET @ATimesUsed = @ATimesUsed + 1
END
ELSE
BEGIN
PRINT REPLICATE(@AlphaValue,@Loop4)
SET @Loop4 = @Loop4 -1
SET @Loop3 = @Loop3 +1
END
END
August 19, 2008 at 2:41 am
You find that WHILE with IF easier than this?
DECLARE @Loop INT
SET @Loop = 1
WHILE @Loop < 8
BEGIN
PRINT REPLICATE('1', 4 - ABS(4 - @Loop))
SET @Loop = @Loop + 1
END
N 56°04'39.16"
E 12°55'05.25"
Viewing 8 posts - 16 through 23 (of 23 total)
You must be logged in to reply to this topic. Login to reply