Sqlserver中存储过程和游标的一些使用例子
/*带输入输出参数存储过程*/ ALTER PROCEDURE pro_test2 @userID INT, @maxUserID INT OUTPUT, @countUser INT OUTPUT AS BEGIN SELECT * FROM dbo.SY_ADMIN WHERE UserID=@userID --10075 SELECT @maxUserID=10089 SET @countUser=20000 END; GO /*测试*/ DECLARE @maxUserID INT,@countUser INT EXECUTE pro_test2 10075,@maxUserID OUTPUT,@countUser OUTPUT SELECT a=@countUser, b=@maxUserID /*带输入输出参数 0登录成功 1密码错误 2用户名错误*/ ALTER PROC proc_test3 @UserName VARCHAR(50), @UserPwd VARCHAR(50), @Result INT OUT AS BEGIN IF @UserName = 'admin' BEGIN IF @UserPwd = '111' SET @Result = 0 ELSE SET @Result = 1 END; ELSE SET @Result = 2 END /*测试*/ DECLARE @Result INT EXECUTE proc_test3 'hystu1', '111',@Result OUT SELECT a= @Result /*游标使用例子*/ --声明2个变量 DECLARE @D_Id NVARCHAR(MAX); DECLARE @D_Name NVARCHAR(MAX); DECLARE @D_Password NVARCHAR(MAX); --声明一个游标mycursor,select语句中参数的个数必须要和从游标取出的变量名相同 DECLARE mycursor CURSOR FOR SELECT D_Id , D_Name, D_Password FROM dbo.TestTable; --打开游标 OPEN mycursor; --从游标里取出数据赋值到我们刚才声明的2个变量中 FETCH NEXT FROM mycursor INTO @D_Id, @D_Name, @D_Password; --判断游标的状态 -- 0 fetch语句成功 ---1 fetch语句失败或此行不在结果集中 ---2 被提取的行不存在 WHILE ( @@fetch_status = 0 ) BEGIN --显示出我们每次用游标取出的值 print (@D_Id+'--------'+@D_Name+'--------'+@D_Password) --用游标去取下一条记录 FETCH NEXT FROM mycursor INTO @D_Id, @D_Name, @D_Password; END; --关闭游标 CLOSE mycursor; --撤销游标 DEALLOCATE mycursor;
/* SQL游标的优点是可以方便从一个结果集中进行循环遍历数据在进行操作。 1、游标允许应用程序对查询语句select 返回的行结果集中每一行进行相同或不同的操作, 而不是一次对整个结果集进行同一种操作; 2、它还提供对基于游标位置而对表中数据进行删除或更新的能力; 3、游标把作为面向集合的数据库管理系统和面向行的程序设计两者联系起来, 使两个数据处理方式能够进行沟通。 然而游标也有缺点——复杂和低效,是游标的最大缺点,也是致使很多时候在使用存储过程中没有使用游标的主要原因。 */ --将book表中的LookCount(int型)字段加上800-1000的随机整数 DECLARE @bid INT; DECLARE cur CURSOR READ_ONLY FOR SELECT bid FROM Book; OPEN cur; FETCH NEXT FROM cur INTO @bid; WHILE ( @@fetch_status = 0 ) BEGIN UPDATE dbo.Book SET LookCount = LookCount + CAST(( RAND() * ( 1000 - 800 ) + 800 ) AS INT) WHERE bid = @bid; FETCH NEXT FROM cur INTO @bid; END; CLOSE cur; DEALLOCATE cur;
--声明3个变量 DECLARE @D_Id NVARCHAR(MAX); DECLARE @D_Name NVARCHAR(MAX); DECLARE @D_Password NVARCHAR(MAX); create table #tmp (id NVARCHAR(MAX),name NVARCHAR(MAX),pwd NVARCHAR(MAX)) --建立临时数据表 --声明一个游标mycursor,select语句中参数的个数必须要和从游标取出的变量名相同 DECLARE mycursor CURSOR FOR SELECT D_Id , D_Name, D_Password FROM dbo.TestTable; --打开游标 OPEN mycursor; --从游标里取出数据赋值到我们刚才声明的3个变量中 FETCH NEXT FROM mycursor INTO @D_Id, @D_Name, @D_Password; --判断游标的状态 -- 0 fetch语句成功 ---1 fetch语句失败或此行不在结果集中 ---2 被提取的行不存在 WHILE ( @@fetch_status = 0 ) BEGIN --显示出我们每次用游标取出的值 --print (@D_Id+'--------'+@D_Name+'--------'+@D_Password) INSERT INTO #tmp(id,name,pwd) VALUES(@D_Id,@D_Name,@D_Password) --用游标去取下一条记录 FETCH NEXT FROM mycursor INTO @D_Id, @D_Name, @D_Password; END; --关闭游标 CLOSE mycursor; --撤销游标 DEALLOCATE mycursor; SELECT * FROM #tmp; --查询结果 DROP TABLE #tmp --删除临时表
USE [JianKu] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[pro_GetProductSale] @BeginDate NVARCHAR(20), @EndDate NVARCHAR(20), @PageIndex INT, @PageSize INT, @Keyword NVARCHAR(50) AS DECLARE @PageSql NVARCHAR(MAX) DECLARE @Sql NVARCHAR(MAX) DECLARE @Where NVARCHAR(200) DECLARE @WhereEnd NVARCHAR(200) DECLARE @CountSql NVARCHAR(MAX) SET @Where='' SET @WhereEnd='' IF ISNULL(@BeginDate, '') <>'' BEGIN SET @Where += ' AND k.PayDate >='''+ @BeginDate + '''' END IF ISNULL(@EndDate, '') <>'' BEGIN SET @Where += ' AND k.PayDate <='''+ @EndDate + '''' END IF ISNULL(@Keyword, '') <>'' BEGIN SET @WhereEnd += ' AND (b.Name like ''%'+ @Keyword + '%'' OR c.Name like ''%'+ @Keyword + '%'' OR d.Name like ''%'+ @Keyword + '%'' OR a.Part like ''%'+ @Keyword + '%'' OR a.GoodsName like ''%'+ @Keyword + '%'') ' END SET @Sql=' SELECT a.*,b.Name AS CategoryName,c.Name AS BrandName,d.Name AS ModelName, t.SumSaleCount,t.SumSaleAmount FROM dbo.Goods a LEFT JOIN dbo.GoodsPlatType b ON a.PlatId=b.Id LEFT JOIN dbo.GoodsBrand c ON a.BrandId=c.Id LEFT JOIN dbo.GoodsModel d ON a.ModelId=d.Id LEFT JOIN ( SELECT TOP 1 g.GoodsId, SUM(g.GoodsIdNum) AS SumSaleCount,SUM(g.GoodsIdNum*g.GoodsPrice) AS SumSaleAmount FROM dbo.GoodsOrderDetails g INNER JOIN dbo.GoodsOrder k ON g.OrderId=k.Id AND k.Statuc=100 ' + @Where + ' GROUP BY g.GoodsId ) as t ON a.Id=t.GoodsId WHERE a.IsDelete=1 ' + @WhereEnd SET @CountSql='SELECT count(1) as TotalRecord FROM (' + @Sql + ') as tp' SET @PageSql='SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY Id) AS ROWID, * FROM ('+@Sql+') as tpp ) AS t WHERE ROWID BETWEEN ' + CAST((@PageIndex - 1) * @PageSize + 1 AS NVARCHAR(20)) +' AND '+ CAST(@PageIndex * @PageSize AS NVARCHAR(20)) PRINT @PageSql EXEC(@PageSql) EXEC(@CountSql) --EXEC pro_GetProductSale @BeginDate='',@EndDate='',@PageIndex=2,@PageSize=10,@Keyword='小米'