"for" loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adam

    "for" loop

    hello

    Does exist in SQL language "for" loop ? If yes, what syntax does it has ?

    best wishes
    Adam
  • Simon Hayes

    #2
    Re: "for&qu ot; loop

    No - see WHILE and "Cursors" in Books Online. But in SQL, a loop is
    often not a good solution - people may use them when they should use a
    single, set-based statement such as UPDATE or DELETE.

    Of course, there are times when a while or cursor loop is the best
    solution, so you might have a good reason to do this - if you can give
    more details of what you need to achieve, someone may be able to
    comment on whether a loop is appropriate for you or not.

    Simon

    Comment

    • David Portas

      #3
      Re: "for&qu ot; loop

      Mostly it pays to find a set based solution rather than write loops. If
      you need to loop then TSQL has WHILE.

      --
      David Portas
      SQL Server MVP
      --

      Comment

      • adam

        #4
        Re: "for&qu ot; loop

        I try to substitute "for" loop by this excample:

        declare @i int
        while (@i<10)
        begin
        insert into dbname.dbo.tabl e_name values @i
        @i=@i+1
        end

        but in line @i=@i+1 ocured an error. Why ?
        I know - it is stupid reason to use "for" loop , but what shoul I changed to
        work this example ?
        What are better ways to do such task ?

        Comment

        • David Portas

          #5
          Re: &quot;for&qu ot; loop

          A common solution is to keep a table of numbers in your database
          (single column of numbers from 0 to some very large number). This can
          help avoid loops in many places:

          INSERT INTO table_name (...)
          SELECT num, ...
          FROM Numbers
          WHERE num BETWEEN 1 AND 10

          Of course you'll still probably use a loop to populate the Numbers
          table, but that only has to be done once and at install time, not at
          runtime.

          --
          David Portas
          SQL Server MVP
          --

          Comment

          • David Portas

            #6
            Re: &quot;for&qu ot; loop

            Numbers table:

            CREATE TABLE numbers (num INTEGER NOT NULL CONSTRAINT pk_numbers
            PRIMARY KEY) ;

            INSERT INTO numbers (num) VALUES (0) ;

            WHILE (SELECT MAX(num) FROM numbers)<65535
            INSERT INTO numbers
            SELECT num+(SELECT MAX(num)+1 FROM numbers)
            FROM numbers ;

            --
            David Portas
            SQL Server MVP
            --

            Comment

            • Simon Hayes

              #7
              Re: &quot;for&qu ot; loop

              As David suggested, a numbers table is definitely a useful tool:



              A calendar table is another good one:



              Simon

              Comment

              • jsfromynr

                #8
                Re: &quot;for&qu ot; loop

                Hi David,
                We can do this simple trick

                Select identity(int,1, 1) MyId into MyTable from
                anySystemTable, anySystemTable, anySystemTable .......

                With warm regards
                Jatinder Singh

                Comment

                • Erland Sommarskog

                  #9
                  Re: &quot;for&qu ot; loop

                  adam (ereuseen@wp.pl ) writes:[color=blue]
                  > I try to substitute "for" loop by this excample:
                  >
                  > declare @i int
                  > while (@i<10)
                  > begin
                  > insert into dbname.dbo.tabl e_name values @i
                  > @i=@i+1
                  > end
                  >
                  > but in line @i=@i+1 ocured an error. Why ?[/color]

                  You need to say SET or SELECT in front. I prefer SELECT, as you can assign
                  more that one variable in one bang:

                  SELECT @a = 1, @b = @c + d, @t = NULL




                  --
                  Erland Sommarskog, SQL Server MVP, esquel@sommarsk og.se

                  Books Online for SQL Server SP3 at
                  Get the flexibility you need to use integrated solutions, apps, and innovations in technology with your data, wherever it lives—in the cloud, on-premises, or at the edge.


                  Comment

                  • --CELKO--

                    #10
                    Re: &quot;for&qu ot; loop

                    Here is a way to gemerate the Sequence table without proprietary code
                    and create the rows in parallel instead of one at a time.

                    WITH
                    (SELECT 0
                    UNION ALL
                    SELECT 1
                    ..
                    UNION ALL
                    SELECT 9) AS Digits(digit)
                    SELECT D1.digit + 10*D2.digit + 100*D3.digit + 1000*D4.digit + ..
                    FROM Digits AS D1, Digits AS D2, Digits AS D3, Digits AS D4, ..
                    WHERE (D1.digit + D2.digit + D3.digit + D4.digit + ..) > 0;

                    IDENTITY has be done one at a time, while this can be parallelized and
                    can generate numbers in sets. The CTE can be repalced with a TABLE or
                    VIEW.

                    Comment

                    Working...