Examples of use of loops and conditional statements in SQL Server

Source: Internet
Author: User
Tags execution
  --╔════════╗--===============================║if Statement Use example ║--    
               ╚════════╝declare @a int set @a=12 if @a>100  
                   Begin print @a end ELSE begin print ' No ' end--╔══════════╗--==========   
The =====================║while statement uses the example ║--╚══════════╝declare @i int set @i=1 While @i<30 begin insert to test (userid) VALUES (@i) Set @i=@i+1 end--set to repeat execution of SQL statements or statements Blocks of the condition. Executes the statement repeatedly as long as the specified condition is true. You can use the break and CONTINUE keywords to control the execution of statements within the while loop inside the loop.  
This article is for previous search from the Internet to get!                                   --More highlights: http://www.bianceng.cnhttp://www.bianceng.cn/database/SQLServer/-- ╔════════╗--================================║ ProTime table and try║--╚════════╝--add temporary table select * Into #c  Sj_temp from CSJ--removing temporary tables used to try begin try--detection Code start drop table                                  #csj_temp End Try begin catch--error start end catch--                                  ╔═════════╗--===============================║ cursor Looping read records ║--    
            ╚═════════╝declare @temp_temp int--declare @Cur_Name --@Cur_Name = "AAA"---------------------------------Create cursor--local (local cursor) DECLA RE AAA CURSOR for select house_id from House_house where deleted=0 or Deleted is null----------------------    
                       
            -------------Open the cursor open AAA-----------------------------------traverse and fetch the cursorFETCH NEXT from AAA to @temp_temp--print @temp_temp while @ @fetch_status =0    
         
              Begin--Do what you want to do select * from House_monthend where house_id= @temp_temp  
         
            FETCH NEXT from AAA into @temp_temp--value assigned to variable--end -----------------------------------closes the cursor close AAA---------------------  --------------Delete a cursor deallocate AAA--
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.