select ... as ... from ... where ... orderby ... groupby ...
top
1 2
select top 10 select top 10percent
like 语句
% 0-n个字符
_ 单个字符
[ ] 在方括号里列出的任意字符
任意没有在方括号内出现的字符
order by
1 2
orderby ... desc--倒序 orderby ... asc--正序(默认)
二次排序,中间逗号隔开
一些函数
获取当前时间getdate() year() month() day()某个时间的年、月、日
快速建表
1 2 3 4 5 6 7 8 9 10
/*** 新建表 ***/ /*select * into st from SubjectTable where 1=2 */
select* into teacher_new from Teacher
用where 1=2 就创建了一个没有内容但结构一样的表。
建表的时候比避免重名
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if exists(select [TABLE_NAME] from [INFORMATION_SCHEMA].[TABLES] where [TABLE_NAME]='place' ) droptable place go select 上课校区 ,上课楼宇 ,上课教室 into place from SubjectTable groupby 上课校区 ,上课楼宇 ,上课教室
插入
insert into 表名(字段1,…) 内容 内容可以是:values(……) 也可以是select的结果。
更新
1 2 3
update 表 set 字段=。。 where ...
删除
1 2 3
delete from 表 where 。。。没有就是删除全部
视图
创建带检查的视图
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if exists(select* from [INFORMATION_SCHEMA].[VIEWS] where [TABLE_NAME]='st_200611') dropview st_200611 go createview st_200611 as select 上课班级 ,课程名 ,教师姓名 ,学期 from SubjectTable where 上课班级='200611' withcheck option
加入 with encryption 再去[INFORMATION_SCHEMA].[VIEWS]中[VIEW_DEFINITION]中查看 没有一些代码。
数据库编程
1 2
declare@xxint set@xx=..
while if print
case
1 2 3 4 5
性别 =case [Stu_sex] when'男'then'男生' when'女'then'女生' end ,....
1 2 3 4 5 6 7
,[Score等级] =case when [Score]>=90and [Score]<=100then'A' when [Score]>=80and [Score]<90then'B' when [Score]>=70and [Score]<80then'C' when [Score]>=60and [Score]<70then'D' when [Score]<60then'E' end
游标
1 2 3 4 5 6 7 8 9 10 11 12
declare cur cursorscroll--声明一个游标,名字是cur,scroll表示可以往前往后,对应的是FORWARD_ONLY 只能往前 forselect*from Student -- for数据范围 open cur --打开游标 fetchfirstfrom cur --提取第一个 fetch ... from 游标 fetchlastfrom cur --提取最后一个 fetch absolute 2from cur --提取全部的第2个 fetch prior from cur --当前的上一个 fetch next from cur --当前的下一个 fetch relative -1from cur --当前的第-1个
close cur --关闭游标 deallocate cur --释放游标
实现遍历输出名字:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
declare cur3 cursorscroll forselect Stu_name from Student declare@numint set@num=(selectcount(*) from Student) declare@iint set@i=1 declare@namevarchar(8) open cur3 while @i<=@num begin fetch absolute @ifrom cur3 into@name print @name set@i=@i+1 end close cur3 deallocate cur3
存储过程
基本
1 2 3
CreateProcedure 过程名 As 代码
例如:
1 2 3 4
CreateProcedure od_sp --存储过程名 As Select*from orders Where orderid<10250
调用:
1
exec od_sp
带默认值参数
1 2 3 4
createprocedure s_age @aint as select*from Student where Stu_age<@a
调用:
1
exec s_age 20
带默认值参数
先看ppt的写法
1 2 3 4 5 6 7 8 9 10 11 12
CreateProcedure CheckFreight @resultint=null As IF @resultISNULL Begin select*from [order details] where Freight <30 End Else Begin select*from [order details] where Freight <@result End
再是我的写法:
1 2 3 4 5 6 7
createprocedure s_age2 @aint=null as select*from Student where Stu_age<case@a whennullthen20 else@a end
调用:
1 2
exec s_age2 -- 使用默认参数 exec s_age2 19-- 设定参数
带输出的参数
1 2 3 4 5 6
createprocedure s_age3 @aint ,@sumint output as select*from Student where Stu_age<@a set@sum=(selectcount(*) from Student where Stu_age<@a)
createtrigger stu_delete on Student after delete as select*from deleted
注意最后是from deleted 在有删除操作的时候会自动执行,显示删除掉的数据。
Update触发器
1 2 3 4 5 6 7 8 9 10 11 12 13
createtrigger stu_update on Student after update as if update(Stu_id) begin raiserror('数据不能被修改',10,1) rollback transaction end else begin select*from inserted end
在更新数据的额时候会触发
insert触发器
1 2 3 4 5
createtrigger stu_insert on Student after insert as select*from inserted
每次操作都对某个数据更新
1 2 3 4 5 6 7 8 9 10 11 12 13
createtrigger stu_srh on Student after insert,delete,update as update Student set Score=Score+0.1 where Stu_name='盛日辉' select Stu_name, Stu_id, Score from Student where Stu_name='盛日辉'