当前位置:Gxlcms > 数据库问题 > SQL Server基础操作(此随笔仅作为本人学习进度记录六 !--程序块和循环)

SQL Server基础操作(此随笔仅作为本人学习进度记录六 !--程序块和循环)

时间:2021-07-01 10:21:17 帮助过:7人阅读

@test_Var int \\声明变量用declare,声明局部变量用@符号,变量的数据类型为整型int。 (声明) set @test_Var=1000 \\为此变量赋值,赋值为1000。 select @test_Var \\用select 语句进行显示此变量的值。 变量的声明以及赋值显示 局部变量:就是用户自定义的变量,作用范围仅在程序内部,在程序执行过程中暂存的变量的值,也可以存储从表或视图中查询出来的结果。局部变量在命名过程中必须以@开头 全局变量: 声明变量:声明变量用Declare,可以声明一个变量,也可以声明多个变量,声明多个变量的时候中间应该用逗号隔开。declare @test_Var int,@test_Var2 char(60) 变量的赋值 Set :单一一个变量赋值 select:同时为多个变量赋值 select @test_Var=2000,@test_Var2=高级数据库管理 输出(变量的显示) Print: 单独显示其中一个变量 Select: 同时显示多个变量 全局变量 全局变量、注释、常用的运算符 全局变量是SQL server系统内部所使用的变量,与局部变量不同的是,局部变量只对当前程序起作用,它的作用范围不限于某个程序, 而是任何程序均可调用,由SQLserver所维护的,也无需声明它,通常情况下此变量存储一些SQLserver的配置设定值和效能统计数据,可以用全局变量来测试系统的设定值或T-SQL命令执行后的状态。 全局变量利用两个@@作为前缀。!!!全局变量不是自定义的是由SQLserver服务器来定义的,只能使用预先声明的全局变量,只能引用它用@@开头。 !!!局部变量的名称不能与全局变量相同。 注释 多行注释/* */ 单行注释-- 运算符 算术运算 + - * / 比较运算符 > < >= <= = !=(不等于) <>(不等于) !> !< 逻辑运算符: &(按位与) 两边都返回true,才能为true。 | (按位或) 两边只有一个为true,才能为true。 ~ (按位取反) true变换为false,false变换为true,0变1 1变0 。 ^ (按位异或) 两边值,一个为true,一个为false才能返回为true。 程序块(代码块) Begin \\程序开始 declare @test_Var1 int,@test_String varchar(60) \\declare声明变量 select @test_Var1=2000,@test_String=这是一个程序块 \\select为变量赋值 if @test_Var1=2000 \\条件 print @test_String \\输出 End Begin declare @testVar1 int,@test_String1 varchar(30),@test_String2 varchar(30) \\declare声明变量 select @testVar1=110,@test_String1=录取,@test_String2=未录取 \\select 为相应变量赋值 if @testVar1>100 \\条件 print @test_String1 \\输出 else print @test_String2 \\输出 End Begin declare @testVar1 int,@test_String1 varchar(30),@test_String2 varchar(30),@test_String3 varchar(30),@test_String4 varchar(30) \\声明 select @testVar1=100,@test_String1=优秀,@test_String2=良好,@test_String3=及格,@test_String4=不及格 \\赋值 if @testVar1>90 and @testVar1<=100 \\条件 print @test_String1 \\输出 else if @testVar1>=80 and @testVar1<90 \\条件 print @test_String2 else if @testVar1>=60 and @testVar1<80 print @test_String3 else print @test_String4 End Case语句 Begin declare @testVar1 int,@test_String varchar(30) \\声明变量 set @testVar1=100 \\变量赋值 set @test_String= \\变量赋值,=等号后面没有写真正的值,后面的值由case语句来完成(Case判断给她赋值)。 case when @testVar1>=90 and @testVar1<=100 then 优秀 \\条件 when @testVar1>=80 and @testVar1<90 then 良好 \\条件 when @testVar1>=60 and @testVar1<80 then 及格 \\条件 else 不及格 \\条件 end select 学员成绩为:+@test_String \\输出的格式为‘学生成绩’+(优秀 and 良好 and 及格) end While循环 declare @test_Var int,@sum int \\声明变量 select @test_Var=0,@sum=0 \\为变量赋值,初始值为0 while @test_Var<=200 \\循环条件,@test_Var<=200 begin set @sum=@sum+@test_Var \\每次循环时候的 set @test_Var=@test_Var+1 end select200以内所有整数之和为,@sum break和continue break declare @test_Var int,@sum int \\声明变量 select @test_Var=0,@sum=0 \\为变量赋值 while @test_Var<=200 \\条件,循环值<=200 begin set @sum=@sum+@test_Var \\为@sum赋值@test_Var=@test_Var+1 set @test_Var=@test_Var+1 \\为@test_Var赋值@test_Var等于@test_Var+1 if @test_Var=30 \\如果@test_Var=30 break \\直接跳出循环 end select 200以内所有整数之和为:@sum continue declare @test_Var int,@sum int \\声明变量 select @test_Var=0,@sum=0 \\为变量赋值 while @test_Var<200 \\条件,循环值<200 begin set @test_Var=@test_Var+1 \\为@test_Var赋值@test_Var=@test_Var+1 if @test_Var=100 \\条件声明,如果@test_Var=100 continue \\则跳出100这个循环,继续进行下一个循环 set @sum=@sum+@test_Var \\为@sum进行赋值@sum=@sum+@test_Var end \\符合所循环条件的时候,结束循环。 select运算结果为:,@sum \\输出结果,运算结果格式为:运算结果,加上@sum的值 (因为@sum数据类型是整型,所以用逗号,连接而不是加号+Go to (用来改变程序执行的流程) declare @test_Var int,@sum int \\声明变量 select @test_Var=0,@sum=0 \\为自变量赋值 test_Target: \\标记,跳转至某个地方的标记 set @test_Var=@test_Var+1 \\为 set @sum=@sum+@test_Var while @test_Var<200 goto test_Target select所有的整数之和为:,@sum

 

SQL Server基础操作(此随笔仅作为本人学习进度记录六 !--程序块和循环)

标签:显示   select   过程   配置   改变   tin   逻辑运算   引用   sel   

人气教程排行