Click here to monitor SSC

Printing 1 to 100 by using a batch.....

Last post 06-24-2009, 8:43 AM by sinshu. 3 replies.
Sort Posts: Previous Next
  •  06-22-2009, 6:43 AM Post number 73831

    Printing 1 to 100 by using a batch.....

    Anybody can suggest any other method than this(Without creating any DB objects,Without passing any parameter)?

    http://sinshith.wordpress.com/

    /*DIFFERENT METHODS FOR PRINTING 1 TO 100*/

    ------RECURSIVE METHOD USING COMMON TABLE EXPRESSION-------

    WITH CTE
    AS
    (
    SELECT COUNT=1
    UNION ALL
    SELECT COUNT=COUNT+1
    FROM CTE WHERE COUNT
    )
    SELECT COUNT FROM CTE

    ----------USING WHILE LOOP-------------------------

    DECLARE @V1 INT
    SET @V1=0
    WHILE (@V1
    BEGIN
    SET @V1=@V1+1
    PRINT @V1
    END

    -----------USING CURSOR----------------------

    DECLARE @TB TABLE
    (
    V2 INT
    )
    INSERT INTO @TB
    SELECT 0
    DECLARE @V1 INT
    DECLARE CUR CURSOR
    FOR SELECT V2 FROM @TB
    OPEN CUR
    FETCH NEXT FROM CUR INTO @V1
    WHILE (@@FETCH_STATUS<>-1)
    BEGIN
    SET @V1=@V1+1
    PRINT @V1
    IF(@V1=100)
    BREAK
    END
    FETCH NEXT FROM CUR INTO @V1
    CLOSE CUR
    DEALLOCATE CUR

    ---------USING GOTO--------------

    DECLARE @V1 INT
    SET @V1=0
    LABEL:
    SET @V1=@V1+1
    PRINT @V1
    IF (@V1<>100)
    GOTO LABEL
  •  06-24-2009, 5:09 AM Post number 73853 in reply to post number 73831

    Re: Printing 1 to 100 by using a batch.....

    SQL Server 2008 for this, of course, but it isn't much harder in 2005, just more ungainly

    SELECT CAST(X.digit+Y.digit AS INT)+1

      
    FROM ( VALUES ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9')
            )
    X (digit)  
    CROSS
    JOIN ( VALUES ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9')
            )
    y(digit)
            

  •  06-24-2009, 5:19 AM Post number 73854 in reply to post number 73853

    Re: Printing 1 to 100 by using a batch.....

    and here is one that will work in all previous versions.

    SELECT  CAST(X.digit+Y.digit AS INT)+1 AS [COUNTER] FROM        
    ( SELECT '0' AS digit  UNION SELECT '1'   UNION SELECT '2' UNION SELECT '3'
      
    UNION SELECT '4' UNION SELECT '5' UNION SELECT '6'
      
    UNION SELECT '7' UNION SELECT '8' UNION SELECT '9' UNION SELECT '9')X
    CROSS JOIN  
    (SELECT '0' AS digit  UNION SELECT '1'   UNION SELECT '2' UNION SELECT '3'
      
    UNION SELECT '4' UNION SELECT '5' UNION SELECT '6'
      
    UNION SELECT '7' UNION SELECT '8' UNION SELECT '9' UNION SELECT '9')Y
    ORDER BY [counter]
            


  •  06-24-2009, 8:43 AM Post number 73857 in reply to post number 73854

    Re: Printing 1 to 100 by using a batch.....

    Gr8 Answers.......you can visit my Blog

    http://sinshith.wordpress.com/

    Let me know your comments....
    I Started writing blog recently
View as RSS news feed in XML