码迷,mamicode.com
首页 > 其他好文 > 详细

存储过程

时间:2018-07-26 21:14:27      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:value   div   pre   inner   delete   name   等于   begin   bsp   

--添加
CREATE PROCEDURE p_StudentAdd
    @StudentName VARCHAR(50),
    @Gender SMALLINT,
    @ClassesCode INT
    
AS
BEGIN
    BEGIN TRAN 
        BEGIN TRY
            INSERT INTO dbo.Student
                    ( StudentName, Gender, ClassesCode )
            VALUES  (@StudentName, -- StudentName - varchar(50)
                      @Gender, -- Gender - smallint
                      @ClassesCode  -- ClassesCode - int
                      )

            COMMIT TRAN
        END TRY


        BEGIN CATCH
            ROLLBACK TRAN
        END CATCH
    


END
GO
--删除
CREATE PROCEDURE p_StudentDel
    @ID INT 
AS 
BEGIN 
    DELETE FROM dbo.Student WHERE ID=@ID

END


GO

--修改
CREATE PROCEDURE p_StudentUpt
    @StudentName VARCHAR(50),
    @Gender SMALLINT,
    @ClassesCode INT,
    @ID INT 
AS 
BEGIN
    UPDATE dbo.Student SET StudentName=@StudentName,Gender=@Gender,ClassesCode=@ClassesCode WHERE ID=@ID

END
    
GO
    
--显示+分页+模糊查询
ALTER PROCEDURE p_GetStudentList
    @PageIndex int ,--当前页
    @Pagesize FLOAT,--每页显示条数
    @PageCount INT OUTPUT,--总页数
    @StudentName VARCHAR(50),
    @ClassesCode VARCHAR(50)     

AS
BEGIN
    BEGIN TRAN  
        BEGIN TRY
        --定义变量存储sql语句
        DECLARE @sql NVARCHAR(max)
        --三表联查条件恒真
SET @sql=SELECT a.ID,a.StudentName,a.Gender,b.ClassesCode,b.ClassesName,c.SchoolCode, c.SchoolName,ROW_NUMBER() OVER(ORDER BY a.ID) rownum FROM dbo.Student a INNER JOIN dbo.Classes b ON b.ClassesCode = a.ClassesCode INNER JOIN dbo.School c ON c.SchoolCode = b.SchoolCode where 1=1 IF(@StudentName!= ‘‘) SET @sql+= and a.StudentName=‘‘‘+@StudentName+‘‘‘‘; IF (@ClassesCode!=‘‘) SET @sql+= and b.ClassesCode=‘‘‘+@ClassesCode+‘‘‘‘; DECLARE @sql2 NVARCHAR(max)
        --分页
SET @sql2=SELECT * FROM (+@sql+) t WHERE t.rownum BETWEEN +STR(((@PageIndex-1)*@Pagesize+1)) +AND+ STR(@PageIndex*@Pagesize) PRINT @sql2 EXEC (@sql2) DECLARE @count FLOAT --总条数 EXEC (@sql) SET @count=@@ROWCOUNT --总条数等于受影响行数 SET @PageCount=CEILING(@count/@Pagesize)--总页数等于总条数除以每页显示条数,取整 COMMIT TRAN END TRY BEGIN CATCH ROLLBACK TRAN END CATCH END GO DECLARE @PageCount INT EXEC p_GetStudentList 1,2,@PageCount OUTPUT,张三,1

 

存储过程

标签:value   div   pre   inner   delete   name   等于   begin   bsp   

原文地址:https://www.cnblogs.com/cxyboke/p/9373847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!