方法一: use master;
go
--判断数据库Test是否存在
if exists(select * from sys.databases where name='Test')
drop database Test;  --存在则删除
go
--创建Test数据库
create database Test;
go
--使用Test数据库
use Test;
go
--判断数据库Test是否存在emp表
if exists(select * from sys.tables where name='emp')
drop table emp;  --存在则删除
go
--创建emp表
create table emp(
eid int primary key identity(10001,1),
ecd varchar(20) null,  -- 要求是YC0001  YC0002且有系统根据eid自动生成
);
go
--判断当前实例是否存在触发器tri_ecd
if exists(select * from sys.triggers where name='tri_ecd')
drop trigger tri_ecd;
go
--创建触发器用来实现工牌号的根据eid自动生成
create trigger tri_ecd  -- 创建触发器tri_ecd
on emp   --在emp表中
after insert  -- 插入操作后触发此触发器,执行下面的操作
as
declare @eid int;  --声明一个变量eid,用来存放最新插入的数据的eid
declare @ecd varchar(14); --声明一个变量ecd,用来存放截取的eid
--set @eid=(select max(eid) from emp);  --从emp表中查询出最新插入的数据的eid --select @eid=@@identity; set @eid= (select ident_current(‘emp‘));   ---获取emp表中最新生成的标识列的值
set @ecd=substring(cast(@eid as varchar),2,len(@eid)); --讲整型的eid转为字符型,然后截取除掉前面的1
update emp set ecd='YC'+@ecd where eid=@eid; -- 更新emp表中的ecd列
go
insert into emp values('');
insert into emp values('');
insert into emp values('');
select * from emp; 效果: eid              ecd 10001     YC0001 10002     YC0002  10003     YC0003方法二:

create table dept(
did varchar(20) primary key,
dname varchar(20)
);
go
drop trigger tri_did;
go
create trigger tri_did
on dept
after insert
as
declare @did varchar(20);
set @did=(select top 1 did from dept where did!='1' order by did desc);
if((select count(*) from dept)<=1)
begin
update dept set did='YC10001';
end
else
begin
declare @temp varchar(20);
declare @num int;
set @temp=substring(@did,3,len(@did));
set @num=(cast(@temp as int)+1);
update dept set did=('YC'+cast(@num as varchar)) where did='1';
end
go
insert into dept values('1','a');  --注意:did列初始值必须是1
insert into dept values('1','b');

insert into dept values('1','c');

效果:
    did               dname
YC10001               a
YC10002               b
YC10003               c

create trigger tri_did
on dept
after insert
as
declare @did varchar(20);
select top 1 @did=did from dept where did!=1 order by did desc;
if((select count(*) from dept)<=1)
begin
update dept set did='S001'
end
else
begin
declare @temp varchar(10);
set @temp=stuff((cast((cast(replace(@did,'S',1) as int)+1) as varchar)),1,1,'S');
update dept set did=@temp where did='1';
end
--触发器 模拟日志
create table users(
uid int identity primary key,
uname varchar(20),
pwd varchar(20)
);
go
create table loginfo(
lid int identity primary key,
logininfo varchar(100),
ldate date
);
go
if exists(select * from sys.triggers where name='tri_loginfo')
drop trigger tri_loginfo;
go
create trigger tri_loginfo
on users
after insert,delete
as
declare @info varchar(100);
if exists(select 1 from inserted)
begin
select @info='用户 '+uname+' 添加成功' from users;
end;
if exists(select 1 from deleted)
begin
select @info='用户 '+uname+' 删除成功' from users;
end;
--insert into loginfo values((select ident_current('users')),@info,getDate()); --ident_current('users')  users表最新生成的标识列的值
--insert into loginfo values((select @@identity),@info,getDate());
go

关闭标识列
set identity_insert 表名 on;

select case e.sex when 'M' then'男' when 'F' then '女' end 性别 from temp;

--替换显示
select sname,case sex when 'M' then'男' when 'F' then '女' end 性别 from stuinfo;
统计列印各科成绩,各分数段人数:课程名称,[100-85],[85-70],[70-60],[ <60] ,
select * from score
select cname,sum(case when score between 85 and 100 then 1 else 0 end) as [100 - 85],
sum(case when score between 70 and 85 then 1 else 0 end) as[85 - 70],
sum(case when score between 60 and 70 then 1 else 0 end) as [70 - 60],

sum(case when score < 60 then 1 else 0 end) as [60 -0] from score,course group by cname;

--去掉补考的
select cname,sum(case when score between 85 and 100 then 1 else 0 end) as [100 - 85],
sum(case when score between 70 and 85 then 1 else 0 end) as [85 - 70],
sum(case when score between 60 and 70 then 1 else 0 end) as [70 - 60],
sum(case when score < 60 then 1 else 0 end) as [60 -0] from (
select studentid,courseid,cid,cname,max(score) as score from
score,course where courseid=cid group by studentid,courseid,cid,cname) oo
) where courseid=cid group by cname;
查找学生姓名和出生日期,如果出生日期为空值,显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd。
select sname  姓名,isnull(CONVERT(char(10),e.BirthDate),'日期不详') 出生日期 from stuinfo;

--查询选修了两个以上课程的学生姓名
--1.先从score中查出选修了两门以上的学生的学号,去掉补考的
--2.通过查出来的学号从stuinfo中查出姓名
select sname from stuinfo stu where stu.stuid in(
select sc.studentid from score sc group by sc.studentid having COUNT(distinct(courseid)>=2);
--查出没有选修1号课程的学生信息
select * from stuinfo where stuid not in(select sc.studentid from score sc where sc.courseid=1);
select * from stuinfo stu  where not exists(
select * from score where stu.stuid=score.studentid and score.courseid=1);
--查询选修了所有课程的学生信息(不存在这样的课程,他没有参加选修)
select * from stuinfo where not exists(--不存在这样的学生不在
select * from course where not exists(  --不存在这样的课程不在学生的选修课成绩单中
select * from score where score.courseid=course.cid and stuinfo.stuid=score.studentid));
select * from stuinfo where stuid in(
select studentid from score group by studentid having COUNT(distinct(courseid)=(select count(*) from course));
--查询至少选修了2号学生所有课程的学生信息
select * from stuinfo where not exists(
select * from course where course.cid in( --查出课程2号学生学修了的课程
select score.courseid from score where score.studentid=2) and not exists(
select * from score where score.studentid=stuinfo.stuid and cid=score.courseid));
--选修了2号课程以上的学生信息
select * from stuinfo where stuid in(select studentid from score where courseid=2)  --选修了2号课程的
select * from stuinfo where stuid in(
select studentid from score group by studentid having count(distinct(courseid)>1 and studentid in(
select * from stuinfo where stuid in(select studentid from score where courseid=2)));
--5检索至少选修了1号和2的课程的学生信息
select * from stuinfo,score where studentid=stuid and courseid=1 and stuid in(select studentid from score where courseid=2);

select distinct * from stuinfo sf1,score sc1,score sc2 where stuid=sc1.studentid and stuid=sc2.studentid and sc1.courseid=1 and sc2.courseid=2;

--查询学生成绩,包括姓名,课程名,成绩,如有补考,只显示最高的一次

select sname,cname,max(score) from score,course,stuinfo where courseid=cid and studentid=stuid  group by sname,cname

转载于:https://www.cnblogs.com/navyzhou/archive/2013/05/07/3065714.html

sql下员工工牌(YC0001)的简单实现和一些特殊查询相关推荐

  1. 下班后还戴着大厂工牌招摇过市,秀大厂优越感吗?

    码个蛋(codeegg) 第 1115 次推文 作者:行走的印钞机 博客:https://mp.weixin.qq.com/s/wFIwZVJX_OSc4do8IoqlKA 你听说过互联网大厂圈子里的 ...

  2. 线下门店数字化转型核心武器,智能工牌+会话智能!

    麦肯锡的一份报告显示,全球受调研的800家企业当中,70%的企业已经开展数字化转型的工作,但仅有20%的企业成功实现数字化转型.因此,如何成功实现企业数字化转型是后疫情时代下每一家企业的新议题. 随着 ...

  3. 钉钉推出“钉工牌”,门禁、差旅、员工福利一码通用

    钉工牌将工作身份验证与支付功能相整合,不同于传统物理工牌,它是一张数字时代的"活"工牌. 编辑 | 宋慧 出品 | CSDN云计算 8月24日,钉钉向1700万企业组织正式推出&q ...

  4. 假如我拥有字节工牌......

    来源 | 码农小说家(ID:gh_1b8358c2f791) 我是一个经常跳槽的程序员. 这些年,待过字节搜索事业群中国事业部百度分部,也待过字节出行事业群中国事业部滴滴分部,所以对字节,一直有种莫名 ...

  5. 假如我拥有字节工牌。。。

    我是一个经常跳槽的程序员. 这些年,待过字节搜索事业群中国事业部百度分部,也待过字节出行事业群中国事业部滴滴分部,所以对字节,一直有种莫名的崇拜情节. 这一天摸鱼时,正好看到一个关于字节但是有点迷惑的 ...

  6. 我在大厂,下班了也戴着工牌

    作者 | 王敏 李秋涵 唐亚华 黎明 编辑 | 王敏 来源 | 深燃(ID:shenrancaijing) "我有字节工牌,你有吗?"最近,字节跳动的工牌火了.网友们吐槽,怎么哪里 ...

  7. 告别神秘客,人力成本节约90%,DuDuTalk工牌用AI帮你智慧巡店

    对于车企来说,一旦门店规模扩大到一定的量级,门店管理方面也会随之出现一系列问题: 1)门店遍布天南海北,安排"神秘客"现场巡店,不但人工.差旅成本高,而且覆盖面窄,无法准确及时地发 ...

  8. DuDuTalk:如何用智慧工牌打造汽车智慧门店

    当前,伴随中国汽车行业的竞争日益激烈,车企营销的 "创新突围战"逐渐白热化.车企当前面临新用户.新销售.新服务.新制造4个新的机遇和挑战. 而随着线上渠道越来越成熟,客户对于线下服 ...

  9. DuDuTalk:地产行业如何用智能工牌和语音分析打造标准化接访流程,批量复制金牌置业顾问?

    近年来,接访数字化悄然兴起,已经从其他行业的成熟应用延展到房地产行业,房地产行业想要通过接访数字化来打造标准化接访流程.批量复制金牌置业顾问,实现"寒冬"中的逆境穿越,离不开DuD ...

最新文章

  1. 360下载的mysql_MySQL数据库5.7
  2. windows cmd 窗口 显示信息慢_你玩过Windows 10新版CMD了吗?
  3. CVPR 2018 TRACA:《Context-aware Deep Feature Compression for High-speed Visual Tracking》论文笔记
  4. OpenCV Laplace point/edge detection拉普拉斯点/边缘检测的实例(附完整代码)
  5. java webapps路径_java 获取服务器端的webapps路径 | 学步园
  6. 原来这些行业的“潜规则”是这样的...
  7. alphac测试和bata测试区别_绝缘电阻测试仪和接地电阻测试仪的测试方式区别
  8. php 在模板中赋值数组变量,PHP自定义函数实现assign()数组分配到模板及extract()变量分配到模板功能示例...
  9. [USACO13OPEN]Fuel Economy【贪心】
  10. 硬币找零,最长上升子序列,背包问题等动态规划问题详解
  11. ubuntu12.04-- vi 使用
  12. 《高质量程序设计指南:C++/C语言》 林锐
  13. 【自动驾驶决策规划】RRT算法
  14. 等级保护三级基本要求
  15. Java poi 表格居中
  16. 费马小定理的归纳法证明和应用
  17. dhtml(灯火通明类似的词语)
  18. THINKPAD X250 加SSD安装WIN10 BIOS设置
  19. 微软的MSR paraphrase数据集
  20. shell脚本--三种引号的区别

热门文章

  1. CCF201409-2 画图 java(100分)
  2. SAP面向iOS设备推Cloud Platform SDK工具
  3. 分區策略與數據傾斜處理策略的區別
  4. flink二阶提交(没有搞完)
  5. intellij出现Initial job has not accepted any resources;
  6. python乐观锁和悲观锁
  7. django的数据库名字和models.py中类名的对应关系
  8. CCP(Cost complexity pruning) on sklearn with python implemention
  9. pyspark.zip/pyspark/worker.py:53: UnicodeWarning: Unicode equal comparison failed to convert both ar
  10. mysql-workbench运行存储过程