存储过程应用实例

来源:互联网 发布:上古5捏脸数据 编辑:程序博客网 时间:2024/05/17 04:23

     在做机房收费系统的下机操作时,由于要查询学生上下机表的上机时间等,学生信息表的学生余额,和基本数据设定表的半小时收费情况等信息,如果一张表一张表的实现会比较麻烦,如图:


      这时应用存储过程就会减少我们的工作量,提高效率!如图:


     代码如下:

Create procedure [dbo].[pro_stuOffline] @card_no char(20)asbegin   select  t_student .studentname ,t_student .Card_no,t_student.money , t_basedate .rate ,t_online .Ontime ,t_student.department, t_student.sex ,t_student.Student_no ,t_online.Consume ,t_online .Leftmoney  from t_student  right join (t_basedate cross join  t_online) on t_student .Card_no= t_online.card_no where t_student.Card_no =@card_no and t_online  .offtime is null end 
    对于D层来说:

'学生下机时,应用存储过程同时查看3张表的信息    Function Query_pro(ByVal enOnline As OnorOfflineEntity) As DataTable Implements IDAL.IOnorOfflineDAL.Query_pro        'Sql查询语句        Dim sqlstr As String        sqlstr = "pro_stuOffline"        '设置参数          Dim sqlParams As SqlParameter() = {            New SqlParameter("@card_no", enOnline.Online_cardno)            }        '执行         Dim sqlhelper As SQLHelper = New SQLHelper        Return sqlhelper.ExecSelect(sqlstr, CommandType.StoredProcedure, sqlParams)    End Function
     下面就来介绍一下存储过程的多表查询应用:


外连接可分为:左连接、右连接、完全外连接。


1、左连接  left join 或 left outer join
SQL语句:select * from student left join course on student.ID=course.ID
执行结果:
         SQL多表连接查询(详细实例)_新客网
左外连接包含left join左表所有行,如果左表中某行在右表没有匹配,则结果中对应行右表的部分全部为空(NULL).


2、右连接  right join 或 right outer join
SQL语句:select * from student right join course on student.ID=course.ID
执行结果:
         SQL多表连接查询(详细实例)_新客网
右外连接包含right join右表所有行,如果左表中某行在右表没有匹配,则结果中对应左表的部分全部为空(NULL)。


3、完全外连接  full join 或 full outer join
SQL语句:select * from student full join course on student.ID=course.ID
执行结果:
         SQL多表连接查询(详细实例)_新客网
完全外连接包含full join左右两表中所有的行,如果右表中某行在左表中没有匹配,则结果中对应行右表的部分全部为空(NULL),如果左表中某行在右表中没有匹配,则结果中对应行左表的部分全部为空(NULL)。


二、内连接  join 或 inner join
SQL语句:select * from student inner join course on student.ID=course.ID
执行结果:
         SQL多表连接查询(详细实例)_新客网
inner join 是比较运算符,只返回符合条件的行。
此时相当于:select * from student,course where student.ID=course.ID




原创粉丝点击