use Study
--注释的第一种方法
/*注释的第二种方法*/
--变量声明及赋值
select * from student
declare @name varchar(20) --定义局部变量,以@符号开头
set @name=(select stuName from student where stuId=071002) --给变量赋值
select @name
go
declare @name varchar(20)
select @name=stuName from student where stuId=071002 --给变量赋值方法二
select @name
go
declare @rows int
select @rows=count(*) from student
select @rows
go
--全局变量
select getdate() as '当前日期和时间',@@connections as '尝试登录的次数' --全局变量以@符号开头,不能对其做任何修改
select @@cpu_busy --自SQL SERVER 启动以来,系统持续运行的毫秒数
select @@cursor_rows --最近打开的游标数
select @@datefirst --一星期的第一天为哪天,由set datefirst命令设置
select @@error --最后一个T-SQL错误的错误号
select @@fetch_status --最后一次提取数据(从游标)的状态,成功为0,出错为-1
select @@identity --最后一次插入的标识值
select @@language --当前使用的语言的名称
select @@max_connections --可以创建的最大同时连接数
select @@rowcount --上一个SQL语句所影响的行数
select @@servername --本地服务器名称
select @@servicename --该计算机上的SQL服务的名称
select @@version --SQL SERVER的版本信息
--数学函数
select abs(-12.34) --绝对值
select ceiling(10.3) --大于或等于指定数值表达式的最小整数
select floor(10.3) --小于或等于指定数值表达式的最大整数
select round(160.748,2),round(160.748,-2) --四舍五入
select power(2,3) --求2的3次幂
select sqrt(3) --指定的近似浮点数表达式的平方根
--字符串函数
declare @str varchar(30),@str1 varchar(30)
set @str=' A sql string'
set @str1='A sql stirng2 '
select @str,ltrim(@str),@str1,rtrim(@str1) --将字符串左右空格截除
select upper('abc'),lower('ABC') --大小写转换
select 'a'+space(20)+'b' --返回由n个空格组成的字符串
select stuff('abcdefghijk',3,5,'123') --删除指定长度的字符,并在指定的起点处插入另一组字符
select reverse('abcd') --返回字符表达式的反向表达式
declare @str varchar(30)
select @str='A sql string'
select charindex('sql',@str) --返回字符串sql在@str中首次出现的位置
select len('abcde') --返回字符串的长度
select datalength(123) --返回用于表示任何表达式的字节数,包括字符类型
select substring('abcdefghijkl',3,5) --返回字符串中指定位置的一部分
select right('abcdefg',3) --返回字符串中从右边开始指定个数的字符
select ascii('A') --返回字符串左侧第一个字符的ASCII值
select char(65) --将整型的ASCII代码值转换为字符
select str(213120) --将数字转为字符串
use Study
select stuName,len(stuName) as 'length' from student where stuId='071001'
select majorName,substring(majorName,3,2) from major where majorId=2
--日期和时间函数
select dateadd(year,3,getdate())
select dateadd(month,3,getdate())
select dateadd(day,3,getdate()) --以datepart指定的方式(年、月、日),返回date(第三个参数)加上n(第二个参数)后的日期
select datediff(year,'2000-09-09',getdate())
select datediff(month,'2000-09-09',getdate())
select datediff(day,'2000-09-09',getdate()) -- --以datepart指定的方式(年、月、日),返回date1与date2之差
select datename(year,'2000-09-09')
select datename(month,'2000-09-09')
select datename(day,'2000-09-09') --返回date(第二个参数)中datepart(第一个参数)指定部分所对应的整数值
select year('2000-09-09')
select month('2000-09-09')
select day('2000-09-09') --返回指定日期的年、月、日
select stuName,datediff(year,birthdate,getdate()) as 'age' from student where stuId='071002'
select getdate() as '当前日期',month(getdate()) as '当前月份'
select datepart(yy,getdate())
select datepart(mm,getdate())
select datepart(dd,getdate()) --当前日期的年月日分别是(分别为year month day 的缩写)
select datepart(dw,getdate()) --星期几(weekday的缩写)
select datepart(wk,getdate()) --一年的第几周(week的缩写)
select datepart(dy,getdate()) --一年的第几天(dayofyear的缩写)
select datepart(qq,getdate()) --季度(quarter的缩写)
select datepart(hh,getdate()) --小时(hour的缩写)
select datepart(mi,getdate()) --分钟(minute的缩写)
select datepart(ss,getdate()) --秒(second的缩写)
select datepart(ms,getdate()) --毫秒(millisecond的缩写)
--系统函数
use EBuy
select comName,comPrice from commodity where cast(comPrice as varchar) like '1%' --把一种数据类型强制转换为另一种数据类型
--不同格式显示日期
select convert(varchar, getdate(), 112) as '112'
select convert(varchar, getdate(), 120) as '120'
select convert(varchar, getdate(), 111) as '111'
select convert(varchar, getdate(), 8) as '8'
select convert(varchar, getdate(), 101) as '101'
select convert(varchar, getdate(), 110) as '110'
select convert(varchar, getdate(), 102) as '102'
--取值范围
select convert(varchar, getdate(), 0) as '0'
select convert(varchar, getdate(), 14) as '14'
select convert(varchar, getdate(), 20) as '20'
select convert(varchar, getdate(), 25) as '25'
select convert(varchar, getdate(), 100) as '100'
select convert(varchar, getdate(), 114) as '114'
select convert(varchar, getdate(), 120) as '120'
select convert(varchar, getdate(), 121) as '121'
--聚合函数
use Study
select sum(credit) as totalCredit from student --返回一列值的总和
select avg(credit) as aveCridit from student --返回一列值的平均值
select count(*) from student --检索表中满足给定条件的记录数
select max(credit) from student --返回一列中的最大值
select min(credit) from student --返回一列中的最小值
--流程控制
--if...else语句
declare @cnt int
select @cnt=count(*) from student where credit>40
if @cnt>50
print '总学分大于40分的人数超过50人'
else
print '总学分大于40分的人数不超过50人'
go
declare @ave decimal(5,2)
select @ave=avg(credit) from student
if @ave>=50
print '平均分大于等于50分'
else
begin
declare @lowCnt int,@highCnt int
select @lowCnt=count(*) from student where credit<50
select @highCnt=count(*) from student where credit>=50
print '总学分在50分以上的人数为:'+cast(@highCnt as varchar)
print '总学分在50分以下的人数为:'+cast(@lowCnt as varchar)
end
go
declare @credit int,@grade char(1)
select @credit=(select credit from student where stuId='071001')
if @credit>=60
set @grade='A'
else if @credit>=50
set @grade='B'
else
set @grade='C'
print '该学生的总学分为:'+cast(@credit as varchar)
print '该学生的学分等级为:'+@grade
go
--case语句
select case majorName
when '计算机' then '计算机专业'
when '通讯工程' then '通讯工程专业'
else '其他专业'
end as '专业分类'
from major
go
use EBuy
select '商品名'=comName,'价格分类'=
case
when comPrice is null then '尚未定价'
when comPrice<100 then '低档商品'
when comPrice>=100 and comPrice<5000 then '中档商品'
else '高档商品'
end
from commodity order by comPrice
go
--while语句
declare @sum int,@i int
set @i=1
set @sum=0;
while(@i<=100)
begin
set @sum=@sum+@i;
set @i=@i+1
end
print '1-100的和为:'+cast(@sum as varchar)
go
use EBuy
while (select avg(comPrice) from commodity where catId=2)>7000
begin
update commodity set comPrice=comPrice-500,salPrice=salPrice-500 from commodity where catId=2
if (select min(comPrice) from commodity where catId=2)<6500
break
else
continue
end
print '价格调整完毕'
go
select * from commodity
--goto语句和return语句
--waitfor语句
begin
waitfor time '15:13'
use EBuy
select * from customer
end
没有合适的资源?快使用搜索试试~ 我知道了~
SQL语句大全(程序设计、视图、索引、游标、事务、触发器、锁、存储过程、XML、权限管理……)
共7个文件
sql:7个
5星 · 超过95%的资源 需积分: 13 88 下载量 108 浏览量
2010-07-25
08:11:01
上传
评论
收藏 13KB RAR 举报
温馨提示
详细讲解你:程序设计、视图、索引、游标、事务、触发器、锁、存储过程、XML、权限管理…… 并加以注释!
资源推荐
资源详情
资源评论
收起资源包目录
SQL SERVER 2005.rar (7个子文件)
第三章 游标、事务和锁.sql 4KB
第四章 存储过程.sql 5KB
第二章 视图与索引.sql 2KB
第五章 触发器.sql 5KB
第七章 权限管理.sql 2KB
第一章 T-SQL程序设计.sql 7KB
第六章 Sql Server 2005中的XML.sql 5KB
共 7 条
- 1
alies1988
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页