sql 存储过程简单写 - MS-SQL Server / 基础类
题目是 创建一个存储过程,功能是根据输入的参数形式给定的学生从xs,kc和xk表中查询出这些学生的学号姓名和所选课程的课程名和成绩
create proc test_proc2
as
select xs.xh,xsxm,kcm,cj from xs,kc,xk
exec test_proc2 ' ',' ',' ',' ' --''里的具体参数我没写
创建一个存储过程,根据已经输入参数形式给定的学生的学号统计出该学生所选的课程的平均成绩,并有输出参数带回其平均成绩
create pro test_proc3
@xsxh char
as
select avg(cj) from xk where xh=@xsxh group by kch
print cj
exec '050202104'
这样可以吗??
SQL code:
create pro test_proc3
@xsxh varchar(20),
@cj numeric(8,2) output
as
select @cj=avg(cj) from xk where xh=@xsxh
go
--调用
declare @n numeric(8,2)
exec test_proc3 '050202104',@cj=@n output
select @n
SQL code:
--1.
create proc test_proc2
@参数 参数类型
as
select xs.xh,xsxm,kcm,cj from xs,kc,xk where 连接条件 and 参数列名=@参数
GO
exec test_proc2 ' ',' ',' ',' ' --''里的具体参数我没写
--2.
create pro test_proc3
@xsxh varchar(20),
@avgcj int out
as
select @avgcj=avg(cj) from xk where xh=@xsxh group by kch
GO
declare @avgcj int
exec '050202104',@avgcj out
print @avgcj
第二个不用group by kch,没看好,自己去掉
{{
相关问答:
可能因为工作的原因 接触数据库这块比较少,之前都是做程序这块,数据库这块都有专门的人来做 分工都很明细 所以对数据库这一块完全不了解。前段时间 去面试了几家公司 几乎都是在数据库这块挂掉的 连个简单的SQ ......
现在有两张表:文章主表A(articleId,articleTitle),文章评论表B(commentId,articleId,commentTitle)
现在我想实现这样的功能:列出文章列表,其中每篇文章标题下面列出此文章的前2个文章评论,请问sql语句怎么写啊 ......
tab1 字段:billdate,goodsid,incount,inmoney,outcount,outmoney,endprice,endcount,endamt
tab2 字段:goodsid,goodskind(商品类型)
tab3 字段:goodskind(商品类型),kindname
结果:
得到商品类型在一段时间 ......
字段1,字段2.....字段N,Status,ParentID
1,Name1....test1,1,99
1,Name1....test1,3,99
1,Name2....test2,1,101
1,Name2....test2,3,101
1,Name3....test3,2,101
1,Name1....test1,4,101
想要的结果是:
1,Na ......
执行数据库操作时,直接用SQL 语句好一些 还是用存储过程更佳呢?
各抒起见
这个的具体问题具体分析
简单的select 、update和insert当然sql解决了
复杂的放在sql服务端应该好点
楼主请参阅
http://msdn.micr ......