Blog Content

    티스토리 뷰

    12.MS_SQL 2008 - T-SQL 문법 : 제어문

    반응형

    --제어문

           Use tempdb

           Go

          

           --If문조건처리

           Declare @num Int

           Set @num = 9

                 if @num % 2 = 0

                        Select '짝수'

                 else

                        Select '홀수'

                 Go

                 ---

                 Declare @Data Int

                 Set @Data = 10

                 If @Data % 2 <> 0

                 Begin

                        Select '홀수'

                        Print '홀수'

                 End

                 Else

                 Begin

                        Select '짝수'

                        Print '짝수'

                 End

                

           --While문조건처리

           --1부터100까지3의배수또는4의배수의합을구하는프로그램

                 --[1]

                 Declare @sum Int

                 Declare @i Int            

                 Set @sum = 0

                 --[2]

                 Set @i = 1                         --[1]초기식

                 While @i <=100

                 Begin

                        if @i % 3 = 0 Or @i % 4 = 0   --[3]조건식:

                        Begin

                               Set @sum += @i       --[4]실행문

                        End

                       

                        Set @i = @i + 1                --[2]증감식

                       

                        if @i > 100   --[!] 멈춤/계속: 여기서는의미가없다.

                               Break

                 End

                 --[3]

                 Select @sum

                 Print @sum



    12T-SQL(5).sql


    반응형

    Comments