SQL Server常用语句
欢迎和大家交流技术相关问题:
邮箱: jiangxinnju@163.com
博客园地址: http://www.cnblogs.com/jiangxinnju
GitHub地址: https://github.com/jiangxincode
知乎地址: https://www.zhihu.com/people/jiangxinnju
-- 身份证验证(SQLServer)
-- 主要验证SQL数据库中已输入的15位 及18位 身份证号码的位数、出生年月日是否正确,可以过滤出大部分的输入错误。
select 身份证号
from 身份表
where (len(身份证号)<>15 and len(身份证号)<>18)
or (len(身份证号)=18 and (Substring(身份证号,7,2)<'19' or Substring(身份证号,7,2)>'20'
or (Substring(身份证号,11,2)>12)
or (Substring(身份证号,11,2) in (01,03,05,07,08,10,12) and Substring(身份证号,13,2)>31)
or (Substring(身份证号,11,2) in (04,06,09,11) and Substring(身份证号,13,2)>30)
or (Substring(身份证号,11,2)=02 and Substring(身份证号,13,2)>29)))
---------------------- 下面是针对 15位 及18位 身份证号码性别的验证语句 -------------------- Access 不支持 Substring 查询,可以替换为 mid 查询。
select 序号,姓名,身份证号,性别
from 身份表
where (((len(身份证号)=15) and (Substring(身份证号,15,1) in (1,3,5,7,9)) and 性别<>'男')
or ((len(身份证号)=15) and (Substring(身份证号,15,1) in (2,4,6,8,0)) and 性别<>'女'))
or (((len(身份证号)=18) and (Substring(身份证号,17,1) in (1,3,5,7,9)) and 性别<>'男')
or ((len(身份证号)=18) and (Substring(身份证号,17,1) in (2,4,6,8,0)) and 性别<>'女'))
----------------- 下面是针对 15位 及18位 身份证号码位数与出生年月日的验证 ------------- Access 不支持 Substring 查询,可以替换为 mid 查询
select 序号,姓名,身份证号,性别
from 身份表
where (len(身份证号)<>15 and len(身份证号)<>18)
or (len(身份证号)=15 and ((Substring(身份证号,9,2)>12)
or (Substring(身份证号,11,2) > 31)
or (Substring(身份证号,9,2) in (01,03,05,07,08,10,12) and Substring(身份证号,11,2)>31)
or (Substring(身份证号,9,2) in (04,06,09,11) and Substring(身份证号,11,2)>30)
or (Substring(身份证号,9,2)=02 and Substring(身份证号,11,2)>29)))
-- 更改列名:
exec sp_rename '表名.原列名','新列名','column';
exec sp_rename 'student.Ssex','Sex','column';
-- 成绩统计语句(SQLServer)
CREATE TABLE stuscore
(
id int NOT NULL PRIMARY KEY identity(0000,1),
name varchar(20),
subject varchar(20),
score int,
stuid varchar(10)
)
insert into stuscore(name,subject,score,stuid)
select '张三', '数学', 89, '1' UNION ALL
select '张三', '语文', 80, '1' UNION ALL
select '张三', '英语', 70, '1' UNION ALL
select '李四', '数学', 90, '2' UNION ALL
select '李四', '语文', 70, '2' UNION ALL
select '李四', '英语', 80, '2' UNION ALL
select '王五', '数学', 49, '3' UNION ALL
select '王五', '语文', 87, '3' UNION ALL
select '王五', '英语', 90, '3'
--计算每个人的总成绩并排名
select name,sum(score) as allscore
from stuscore
group by name
order by allscore desc
--计算每个人的总成绩并排名
select distinct t1.name,t1.stuid,t2.allscore
from stuscore t1,(select stuid,sum(score) as allscore from stuscore group by stuid) t2
where t1.stuid=t2.stuid
order by t2.allscore desc
--计算每个人单科的最高成绩
select t1.stuid,t1.name,t1.subject,t1.score
from stuscore t1,(select stuid,max(score) as maxscore from stuscore group by stuid) t2
where t1.stuid=t2.stuid and t1.score=t2.maxscore
--计算每个人的平均成绩
select distinct t1.stuid,t1.name,t2.avgscore
from stuscore t1,(select stuid,avg(score) as avgscore
from stuscore
group by stuid) t2
where t1.stuid=t2.stuid
--列出各门课程成绩最好的学生
select t1.stuid,t1.name,t1.subject,t2.maxscore
from stuscore t1,(select subject,max(score) as maxscore from stuscore group by subject) t2
where t1.subject=t2.subject and t1.score=t2.maxscore
--列出各门课程成绩最好的两位学生
select distinct t1.*
from stuscore t1
where t1.id in (select top 2 stuscore.id from stuscore where subject = t1.subject order by score desc)
order by t1.subject
--学号 姓名 语文 数学 英语 总分 平均分
select
stuid as 学号,
name as 姓名,
sum(case when subject='语文' then score else 0 end) as 语文,
sum(case when subject='数学' then score else 0 end) as 数学,
sum(case when subject='英语' then score else 0 end) as 英语,
sum(score) as 总分,
(sum(score)/count(*)) as 平均分
from stuscore
group by stuid,name
order by 总分 desc
--列出各门课程的平均成绩
select subject,avg(score) as avgscore
from stuscore
group by subject
--声明变量以便后续调用
declare @tmp table(pm int,name varchar(50),score int,stuid int)
declare @id int
--列出数学成绩的排名
insert into @tmp
select null,name,score,stuid
from stuscore
where subject='数学'
order by score desc
set @id=0
update @tmp set @id=@id+1,pm=@id
select * from @tmp
select DENSE_RANK () OVER(order by score desc) as row,name,subject,score,stuid
from stuscore
where subject='数学'
order by score desc
--列出数学成绩在2-3名的学生
select t3.*
from
(
select top 2 t2.* from(select top 3 name,subject,score,stuid from stuscore where subject='数学'order by score desc) t2
order by t2.score
) t3
order by t3.score desc
--求出李四的数学成绩的排名
insert into @tmp
select null,name,score,stuid
from stuscore where subject='数学'
order by score desc
set @id=0
update @tmp
set @id=@id+1,pm=@id
select * from @tmp where name='李四'
--课程 不及格(-59) 良(-80) 优(-100)
select subject,
(select count(*) from stuscore where score<60 and subject=t1.subject) as 不及格,
(select count(*) from stuscore where score between 60 and 80 and subject=t1.subject) as 良,
(select count(*) from stuscore where score >80 and subject=t1.subject) as 优
from stuscore t1
group by subject
--数学:张三(50分),李四(90分),王五(90分),赵六(76分)
declare @s varchar(1000)
set @s=''
select @s =@s+','+name+'('+convert(varchar(10),score)+'分)'
from stuscore
where subject='数学'
set @s=stuff(@s,1,1,'')
print '数学:'+@s
-- 怎样删除外键约束(SQLServer)
--测试环境
--主表
create table test1(id int primary key not null,value int)
insert test1 select 1,2
go
--从表
create table test2(id int references test1(id),value int)
go
--第一步:找出test2表上的外键约束名字
--Microsoft SQLServer 2000(及以上)
exec sp_helpconstraint 'test2'
--Microsoft SQLServer 2005(及以上)
select name
from sys.foreign_key_columns f join sys.objects o
on f.constraint_object_id=o.object_id
where f.parent_object_id=object_id('test2')
--第二步:删除外键约束
alter table test2
drop constraint FK__test2__id__08EA5793
SQL Server常用语句的更多相关文章
- SQL server 常用语句
SQL Server中常用的SQL语句 1.概述 2.查询概述 3.单表查询 4.连接查询 5.带有exists的相关子查询 6.SQL的集合操作 7.插入操作 8.删除操作 9.修改操作 10. ...
- sql server 常用的系统存储过程
系统存储过程 说明 sp_databases 列出服务上的所有数据库 sp_helpdb 报告有关指定数据库或所有数据库的信息 sp_renamedb 更改数据库的名称 sp_tables 返回当 ...
- SQL SERVER常用语法记录
用于记录SQL SERVER常用语法,以及内置函数. 以下语句包含: WITH 临时表语法 ROW_NUMBER()内置函数,我一般主要是用来分页.针对于查出来的所有数据做一个数字排序 分页的BETW ...
- 如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?
如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?(2006-12-14 09:25:36) 与这个问题具有相同性质的其他描述还包括:如何 ...
- SQL Server分页语句ROW_NUMBER,读取第4页数据,每页10条
SQL Server分页语句ROW_NUMBER,读取第4页数据,每页10条 SELECT Id,[Title],[Content],[Image] FROM ( SELECT ROW_NUMBER( ...
- SQL Server UPDATE语句的用法详解
SQL Server UPDATE语句用于更新数据,下面就为您详细介绍SQL Server UPDATE语句语法方面的知识,希望可以让您对SQL Server UPDATE语句有更多的了解. 现实应用 ...
- SQL Server中语句的自动参数化
原文:SQL Server中语句的自动参数化 use master go if exists(select * from sys.databases where name = 'test') drop ...
- sql server常用函数、常用语句
一.常用函数 1.字符串函数 : charindex(':','abc:123') --寻找一个字符在一段字符串中起始的位置 len('zhangsan') --获取一段字符串的长度 lef ...
- 自己整理的常用SQL Server 2005 语句、
--创建数据库 create database 数据库 go --打开数据库 use 数据库 --删除数据库 drop database 数据库 Go --创建数据表 create table 数据表 ...
随机推荐
- 白话学习MVC(九)View的呈现一
一.概述 本节来看一下ASP.NET MVC[View的呈现]的内容,View的呈现是在Action执行之后进行,Action的执行生成一个ActionResult,[View的呈现]的功能就是:通过 ...
- tableView和scrollView滚动起冲突
tableView和scrollView滚动起冲突 tableView也是继承的scrollView,所以在滚动的时候也会触发scrollView的代理方法,在scrollViewDidScroll中 ...
- VS 创建虚拟目录失败,映射到其他文件夹!
今天,改一哥们项目!立马,问了一下原因.支支吾吾的气死LZ! 算了,就不信自己琢磨不出来!哼 找了半天,坑爹的是在Web.csproj文件中! 用txt打开,发现这个东东! <UseIIS> ...
- <<卸甲笔记>>-基础语法对比
以Oracle中sottt用户下的数据为例,PPAS 中scott用户下面的数据由Oracle迁移而来 1 查询emp表中的数据 Oracle [root@test03 ~]# su - oracl ...
- zjuoj 3605 Find the Marble
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3605 Find the Marble Time Limit: 2 Seco ...
- 谷歌大神Jeff Dean:大规模深度学习最新进展 zz
http://www.tuicool.com/articles/MBBbeeQ 在AlphaGo与李世石比赛期间,谷歌天才工程师Jeff Dean在Google Campus汉城校区做了一次关于智能计 ...
- python的一些图像操作
from PIL import ImageGrabim = ImageGrab.grab()im.save("f:\\T.jpg",'jpeg') 直接用pyCharm安装PI ...
- 【笔记】jquery阻止冒泡事件发生的语句
时间触发时会执行两个步骤:1.捕获 2.冒泡,而很多浏览器包括jquery都不支持捕获动作所以只能执行冒泡动作. 所谓冒泡就是当点击就是事件的执行顺序,本人的理解为:但某一元素触发时间时它的祖先元素( ...
- winform插件机制学习
这两天在看自定义控件,原来有太多知识没有掌握.今天看到插件机制,心里突然一亮,这个东西听了不少次,就是不知道是啥回事.这次有幸书里包含一个案例,我就跟着它一步步来.终于知道是什么回事了.这个应该在软件 ...
- LeetCode算法题解
1.给定两个正整数(二进制形式表示)A和B,问把A变为B需要改变多少位(bit)?也就是说,整数A和B的二进制表示中有多少位是不同的?(181) 解法一:举例说明,为了减少复杂度,就使用八位二进制吧. ...