求几个SQL语句,十分感谢

zdlou 2007-10-11 03:06:21
1、创建一个表,表结构如下:
学号char (10),姓名 varchar(8),入学日期 char(10) --备注:格式为yyyy-mm-dd
问题:如何使得 Char类型的 入学日期 格式约束为 YYYY-MM-DD ???

2、写一存储过程
把成绩表中字段 chinese ,math 求和,并把和值写入到总分字段中;
按如下规则:先总分排名,若总分一样 则按语文成绩排名,如语文成绩一样,则名次相同,把名次写入名次字段中。

如:姓名 语文 数学 总分 名次
张三 98 90 ? ?
李四 100 88 ? ?

请问:该如何写这个存储过程?

十分感激!
...全文
188 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinwenjian 2007-10-11
  • 打赏
  • 举报
回复
收下了
wuxinyuzhu 2007-10-11
  • 打赏
  • 举报
回复

--对输入的数据进行约束
create table t(studentID char(10), [name] varchar(8),
startDate char(10) Check (isdate(startdate)=1 and cast(startdate as datetime)=convert(datetime,startdate,120)))
insert into t
select 'aa','bb','77799820'
union all select 'bb','ggg','2007-10-11'
union all select 'cc','ddd','2007-20-11'
select * from t
--创建测试表
create table tbl ([name] varchar(8), chinese float, math float,zong float,px int)
insert tbl
select 'a', 80, 90,null,null
union all select 'b', 85, 88,null,null
union all select 'c', 77, 93,null,null
union all select 'd', 80, 90,null,null
union all select 'e', 99, 100,null,null
--创建存储过程
create proc mysql
as
update bb set zong=tt.zong,px=tt.px from tbl bb,
(SELECT name,zong , px=(SELECT COUNT(zong) FROM
(
select name,chinese,math,zong = chinese + math from tbl
) t
WHERE zong > a.zong or (zong = a.zong and chinese > a.chinese)) + 1
FROM
(
select name,chinese,math,zong = chinese + math from tbl
) a
) tt
where bb.name=tt.name
go
--调用存储过程
exec mysql
--查看
select * from tbl

--删除
drop table tbl
drop proc mysql



wangyongahz88 2007-10-11
  • 打赏
  • 举报
回复
第2个问题:
create table #tc ([name] varchar(8), chinese float, math float)
insert #tc
select 'a', 80, 90
union all select 'b', 85, 88
union all select 'c', 77, 93
union all select 'd', 80, 90
union all select 'e', 99, 100

select * from #tc


select *,total=chinese + math,rank=(select count(1)+1 from #tc b where b.chinese + b.math>a.chinese + a.math ) from #tc a order by rank


drop table #tc
dawugui 2007-10-11
  • 打赏
  • 举报
回复
1、创建一个表,表结构如下:
学号char (10),姓名 varchar(8),入学日期 char(10) --备注:格式为yyyy-mm-dd
问题:如何使得 Char类型的 入学日期 格式约束为 YYYY-MM-DD ???

create table tb(学号 char(10),姓名 varchar(8),入学日期 datetime)

2、写一存储过程
把成绩表中字段 chinese ,math 求和,并把和值写入到总分字段中;
按如下规则:先总分排名,若总分一样 则按语文成绩排名,如语文成绩一样,则名次相同,把名次写入名次字段中。

如:姓名 语文 数学 总分 名次
张三 98 90 ? ?
李四 100 88 ? ?

SELECT * , 名次=(SELECT COUNT(总分) FROM
(
select 姓名,语文,数学,总分 = 语文 + 数学 from tb
) t
WHERE 总分 > a.总分 or (总分 = a.总分 and 语文 > a.语文)) + 1
FROM
(
select 姓名,语文,数学,总分 = 语文 + 数学 from tb
) a
ORDER BY 名次
Generics 2007-10-11
  • 打赏
  • 举报
回复
Sorry, I think you need the higher the total, the lower the rank.

-- step 1
create Table #t (studentID char(10), [name] varchar(8), startDate char(10) Check(
substring(startDate,5,1)='-' AND substring(startDate,8,1)='-' AND
isdate(startDate)=1
))

INSERT #t values('1','a','012956')
INSERT #t values('1', 'a', '1129-9-6')
insert #t values('1','a','2001-01-04')
SELECT * FROM #t

drop table #t



-- step 2
create table #tc ([name] varchar(8), chinese float, math float)
insert #tc
select 'a', 80, 90
union all select 'b', 85, 88
union all select 'c', 77, 93
union all select 'd', 80, 90
union all select 'e', 99, 100
union all select 'f', 90, 80

alter procedure showRank
as
begin
select [name], chinese, math, chinese+math as total,
rank() over (order by chinese+math desc, chinese desc) as rank
from #tc
order by rank
end

exec showRank

drop table #tc
Generics 2007-10-11
  • 打赏
  • 举报
回复
here you go:

-- step 1
create Table #t (studentID char(10), [name] varchar(8), startDate char(10) Check(
substring(startDate,5,1)='-' AND substring(startDate,8,1)='-' AND
isdate(startDate)=1
))

INSERT #t values('1','a','012956')
INSERT #t values('112996')
insert #t values('1','a','2001-01-04')
SELECT * FROM #t

drop table #t



-- step 2
create table #tc ([name] varchar(8), chinese float, math float)
insert #tc
select 'a', 80, 90
union all select 'b', 85, 88
union all select 'c', 77, 93
union all select 'd', 80, 90
union all select 'e', 99, 100

create procedure showRank
as
begin
select [name], chinese, math, chinese+math as total,
rank() over (order by chinese+math, chinese) as rank
from #tc
order by chinese+math
end

exec showRank

drop table #tc
zdlou 2007-10-11
  • 打赏
  • 举报
回复
谢谢大家,还有一个问题请教一下:

创建一个表,其中的字段有中文说明的。
例如:

创建有两个字段std_id,std_name的成绩表
如下: std_id 中文意思是 学号
std_name 中文意思是 姓名
请问如何实现这个中文意思的说明呢?

22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧