Chapter 4.SQL编程

----------------------------------SQL编程---------------------------------
--1、声明变量
--declare @name varchar(50)
--declare @age int

--同时声明多个变量
declare @name varchar(50) , @age int 

--2、为变量赋值
set  @name='lisa'  --方式1
select @age=18  --方式2

--3、输出
select '姓名', @name
select '年龄', @age

---4、while 循环 declare @i int=1 --声明变量同时赋值 while @i<=100 begin print 'hello' set @i=@i+1 end

--练习:计算1-100之间的所有整数的和 declare @a int=1 declare @sum int=0 while @a<=100 begin set @sum=@sum+@a set @a=@a+1 end print @sum


--5、if else语句 declare @n int=3 if @n>10 begin print '@n>10' end else if @n>5 and @n<=10 begin print '@n>5 and @n<=10' end else begin print'@n<=5' end


--练习:计算1-100之间所有奇数/偶数的和 declare @sumJishu int=0 declare @j int =1 while @j<=100 begin if @j%2<>0 begin set @sumJishu=@sumJishu+@j end set @j=@j+1 end print @sumJishu
declare @sumOushu int=0,@o int=2
while @o<=100
    begin
        set @sumOushu=@sumOushu+@o
        set @o=@o+2
    end
print @sumOushu


--continue:跳出本次循环 --break:跳出循环 --全局变量(系统变量) --两个@@ 开头,一般都是系统变量 --注: 系统变量只能查询,不能赋值,不能修改
select @@VERSION


select @@ERROR


select @@LANGUAGE

  
select @@SERVERNAME

  
原文地址:https://www.cnblogs.com/xiao55/p/5583589.html