变量

变量分为

     

局部变量

       声明局部变量     declare @变量名   数据类型

       赋值       set @变量名 =值

                     select @变量名=值

     

     declare @name varchar(50)

     set @name='余电费'
     select @name=‘余电费 ’
     print @name

     用select语句为局部变量赋值

--查询学员信息表中与张三性别相同的学员信息

declare @name int
select * from StuInfo
where Sex in
(select sex from StuInfo where StuName='张三')
go
declare @name varchar(50)='张三'
declare @sex varchar(20)

select @sex=sex from StuInfo
where StuName=@name

select * from StuInfo
where @sex=Sex
go
--查询与赵六相邻班级的学员信息
declare @name varchar(20)='赵六'
declare @ClassId int

select @ClassId=ClassId from StuInfo where stuname=@name
select * from StuInfo
where ClassId in(@ClassId+1,@ClassId-1)

全局变量

        全局变量是用来记录SQL Server服务器活动状态的一组数据,是SQL Server 系统提供并赋值的变量,

        用户不能建立全局变量,也不能给全局变量赋值或直接更改全局变量的值。

       --查看上一个错误的编号
       select @@ERROR

      

 

posted @ 2018-01-15 20:27  X先森  阅读(108)  评论(0)    收藏  举报