SQL 游标的存储过程示例
注意事项:两个小数运算的时候都是一种类型! USE [FoodMedicineExam] GO /****** Object: StoredProcedure [dbo].[P_DrugExamAnalysis] Script Date: 02/02/2016 10:53:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: PPL -- Create date: 2016-01-05 -- Description: 考试分析 -- ============================================= ALTER PROCEDURE [dbo].[P_DrugExamAnalysis] @ProvinceCode VARCHAR(10) , --省份 @CityCode VARCHAR(10) , --城市 @Area VARCHAR(10) , --县级 @DepartCode VARCHAR(10) , --机构 @TrainPlan VARCHAR(10) --培训计划 AS BEGIN --检查临时表是否存在,否则删除临时表 IF EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[#tempCounttable]') AND type IN ( N'U' ) ) BEGIN DROP TABLE [dbo].[#tempCounttable] END --创建临时表 CREATE TABLE #tempCounttable ( id INT IDENTITY(1, 1) NOT NULL ,--创建列id,并且每次新增一条记录就会加 passType VARCHAR(10) , lv FLOAT ) --总人数 CREATE TABLE #tmpPeoCount ( peoCount INT ) --通过率 CREATE TABLE #tmpTongguo ( Tongguo FLOAT ) --未通过率 CREATE TABLE #tmpWeitonguo ( Weitonguo FLOAT ) --缺考率 CREATE TABLE #tmpQuekao ( Quekao FLOAT ) --定义临时变量 DECLARE @where VARCHAR(1000) --条件SQL DECLARE @zongCountsql VARCHAR(1000) --总人数 DECLARE @tongGuoCountsql VARCHAR(1000) --通过率 DECLARE @weiTongGuoCountsql VARCHAR(1000) --未通过率 DECLARE @queKaosql VARCHAR(1000) --缺考 DECLARE @zongCount INT --总人数 DECLARE @tongGuoCount FLOAT --通过率 DECLARE @weiTongGuoCount FLOAT --未通过率 DECLARE @queKao FLOAT --缺考 SET @where = ' ' ; --市局 IF ( @ProvinceCode != '00000' ) BEGIN SET @where += ' AND c_project.Province=''' + @ProvinceCode + '''' ; END --市局单位 IF ( @CityCode != '00000' ) BEGIN SET @where += ' AND c_project.City=''' + @CityCode + '''' ; END --分局 IF ( @Area != '00000' ) BEGIN SET @where += ' AND c_project.Area=''' + @Area + '''' ; END --单位 IF ( @DepartCode != '00000' ) BEGIN SET @where += ' AND c_project.DepartCode=''' + @DepartCode + '''' ; END --培训 IF ( @TrainPlan != '00000' ) BEGIN SET @where += ' AND C_TrainPlan.id=''' + @TrainPlan + '''' ; END PRINT ( @where ) --总人数 SET @zongCountsql = 'SELECT COUNT(*) FROM dbo.c_project INNER JOIN dbo.C_TrainPlan ON c_project.TrainPlanId = C_TrainPlan.Id WHERE 1 = 1' + @where ; INSERT INTO #tmpPeoCount ( peoCount ) EXEC ( @zongCountsql ) SELECT @zongCount = peoCount FROM #tmpPeoCount PRINT ( @zongCount ) --通过率 SET @tongGuoCountsql = 'SELECT COUNT(*) FROM dbo.c_project JOIN C_TrainPlan ON c_project.TrainPlanId = C_TrainPlan.Id JOIN C_ExamPlan ON C_ExamPlan.TrainPlanId = C_TrainPlan.Id JOIN c_examanswer ON dbo.c_project.id = dbo.c_examanswer.projectid JOIN dbo.E_ExamWay ON E_ExamWay.Id = C_ExamPlan.ExamWayId WHERE c_examanswer.point >= E_ExamWay.PassScore' + @where ; INSERT INTO #tmpTongguo ( Tongguo ) EXEC ( @tongGuoCountsql ) SELECT @tongGuoCount = Tongguo FROM #tmpTongguo --缺考 SET @queKaosql = 'SELECT COUNT(*) FROM dbo.c_project INNER JOIN dbo.C_TrainPlan ON c_project.TrainPlanId = C_TrainPlan.Id WHERE 1=1 ' + @where + ' AND c_project.id NOT IN ( SELECT projectid FROM c_examanswer)' INSERT INTO #tmpQuekao ( Quekao ) EXEC ( @queKaosql ) SELECT @queKao = Quekao FROM #tmpQuekao --未通过率 SET @weiTongGuoCountsql = 'SELECT COUNT(DISTINCT c_project.id) FROM dbo.c_project left JOIN C_TrainPlan ON c_project.TrainPlanId = C_TrainPlan.Id left JOIN C_ExamPlan ON C_ExamPlan.TrainPlanId = C_TrainPlan.Id left JOIN c_examanswer ON dbo.c_project.id = dbo.c_examanswer.projectid JOIN dbo.E_ExamWay ON E_ExamWay.Id = C_ExamPlan.ExamWayId WHERE c_examanswer.point < E_ExamWay.PassScore' + @where ; INSERT INTO #tmpWeitonguo ( Weitonguo ) EXEC ( @weiTongGuoCountsql ) SELECT @weiTongGuoCount = Weitonguo FROM #tmpWeitonguo DECLARE @tonguolv FLOAT IF ( @tongGuoCount > 0 AND @zongCount > 0 ) BEGIN SET @tonguolv = ( @tongGuoCount / @zongCount ) * 100 END ELSE BEGIN SET @tonguolv = 0 END DECLARE @weitongguolv FLOAT IF ( @weiTongGuoCount > 0 AND @zongCount > 0 ) BEGIN SET @weitongguolv = ( @weiTongGuoCount / @zongCount ) * 100 END ELSE BEGIN SET @weitongguolv = 0 END DECLARE @quekaolv FLOAT IF ( @queKao > 0 AND @zongCount > 0 ) BEGIN SET @quekaolv = ( @queKao / @zongCount ) * 100 END ELSE BEGIN SET @quekaolv = 0 END INSERT INTO #tempCounttable ( passType, lv ) VALUES ( '通过率', -- passType - varchar(10) @tonguolv -- lv - float ) INSERT INTO #tempCounttable ( passType, lv ) VALUES ( '未通过率', -- passType - varchar(10) @weitongguolv -- lv - float ) INSERT INTO #tempCounttable ( passType, lv ) VALUES ( '缺考率', -- passType - varchar(10) @quekaolv -- lv - float ) DECLARE @sql VARCHAR(100) ; SET @sql = 'select * from #tempCounttable' ; EXEC(@sql) --检查临时表是否存在,否则删除临时表 IF EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[#tempCounttable]') AND type IN ( N'U' ) ) BEGIN DROP TABLE [dbo].[#tempCounttable] DROP TABLE [dbo].[#tmpPeoCount] DROP TABLE [dbo].[#tmpTongguo] DROP TABLE [dbo].[#tmpWeitonguo] DROP TABLE [dbo].[#tmpQuekao] END END GO