Need Help In Getting Required Result

  • 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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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"

  • 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

  • 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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Trevor,

    This thread kinda got hijacked... are you all set?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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

  • 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

  • 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