2014年11月6号----变量
变量
变量的定义,declare @ hello
--也可加上as declare @hello as varchar(20)
declare @hello varchar(20)
set @hello ='你好'
select @hello --结果框中打印出来
--print @hello --消息框中打印出来
--变量,@变量,变量类型
--赋值:set @变量=所要赋的值
--取值:select @变量
--这三句必须同时执行,这些不起什么作用,
declare @hello varchar(20)
set @hello ='销售部'
select * from bumen where name=@hello
--结果是将销售部的信息给打印出来
declare @hello varchar(20)
select @hello=name from bumen where code=1
select @hello as 部门
--当放到select 和from之间时,可以当做赋值语句,不执行查询功能
--系统内部内置了好多的变量,
--全局变量,,包含连数据库没连上,增删改查的记录嘛的都会记录下来
print @@connections
--返回SQL自上次启动以来的连接数,不管成功还是失败
print @@version
--获取版本的信息
select from aaa
print @@error
--返回执行上次SQL时的错误,返回代表没错,0之外的数字代表错误
print @@language
--返回当前我们使用的语言
print @@rowcount
--受上一句影响的行数
例子:
update bumen set phone ='1010101'
--改变新值
select '此查询语句共影响了'+CAST(@@ROWCOUNT as varchar(10))+'行'
declare @text varchar(20)
set @text='123''11'
--两个单引号是一个单引号
print @text
--运算符
declare @jia int
set @jia=1+1--加法
print @jia
结果:2
declare @jia int
set @jia=10%3--取余数
print @jia
结果:1
declare @jia int
set @jia=2*4--乘法
print @jia
结果:8
declare @jia int
set @jia=10/2--除法
print @jia
结果:5
--比较运算符
-- <><=,>=,<>,!=,!<(>=),!>(<=)
select * from bumen where code !=3
select * from bumen where code <>2--选出code不等于的部门
--逻辑运算符
--all,后面的条件全部满足
--any,some 任何一个条件满足
--between,范围
--in(),满足其中任何一个
--like ,好像
--not 非
--and,or
--exists,存在,子查询
--配合关系算符使用的all ,查出所有满足列元素的数据
--只需查询一个
select * from student where code=all(select code from student where name='王超' )
--查询大于王雷雷编号的同学的消息
select * from student where code>all(select code from student where name='王雷雷' )
--只许查询一个,查在和三之间的编号学员的信息
select * from student where name=all(select name from student where code<3 and code>1)
--满足任何一个条件即可
--满足code<=3的人的信息,任意一个
select * from student where name=any(select name from student where code<=3)
select * from student where name=some(select name from student where code<=3)