每个数据库中都是有着存储过程功能的,存储过程能够帮助我们极大的实现代码通用,本文我们就来看看SqlServer中要如何编写存储过程。
例1:
ALTER PROC[dbo]. [SP_mm_NS]( @ID NVARCHAR(60) , @ReturnCode NVARCHAR(30) OUTPUT , @ErrorMessage NVARCHAR(2000) OUTPUT ) AS BEGIN DECLARE @rtnCode NVARCHAR(30) , @rtnText NVARCHAR(2000) DECLARE @P_BillID NVARCHAR(60) , @P_LineItemNo INTEGER SELECT @rtnCode = '0', @rtnText = '' BEGIN TRY SELECT * FROM dbo.temp_table DECLARE Table_crsr CURSOR FOR SELECT CVLevel FROM# tempTable OPEN Table_crsr FETCH Table_crsr INTO @CVLevel WHILE @ @FETCH_STATUS = 0 BEGIN -- - FETCH Table_crsr INTO @CVLevel END CLOSE Table_crsr DEALLOCATE Table_crsr DECLARE @C NVARCHAR(60) DECLARE @sql NVARCHAR(500) DECLARE @i INT SET @i = 1 WHILE(@i <= @CVLevel) BEGIN SET @C = 'CVLevel_' + CONVERT(NVARCHAR(60), @i) SET @sql = 'ALTER TABLE @Table ADD ' + @C + ' NVARCHAR(60)' PRINT '@sql =' + @sql EXEC(@sql) SET @i = @i + 1 END END TRY BEGIN CATCH SET @rtnText = ' ERROR_PROCEDURE=' + ERROR_PROCEDURE() + ', ERROR_LINE=' + convert(varchar(10), ERROR_LINE()) + ', ERROR_NUMBER=' + convert(varchar(10), ERROR_NUMBER()) + ', ERROR_MESSAGE=' + left(ERROR_MESSAGE(), 255) SET @rtnCode = '1' END CATCH L_END: SELECT @ReturnCode = @rtnCode, @ErrorMessage = @rtnText END
例2:
if exists(select * from sysobjects where id = object_id(N 'dbo.test_cursor') and type = 'P') drop PROCEDURE dbo.test_cursor GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE test_cursor AS Declare @acctNbr varchar(100); Declare @acctName nvarchar(100); Declare MyCursor Cursor for Select AcctNbr, AcctName From base._Member Order By ID Open MyCursor Fetch next From MyCursor Into @acctNbr, @acctName while (@ @fetch_status = 0) begin begin Select @acctNbr = Convert(varchar(100), @acctNbr) Select @acctName = Convert(nvarchar(100), @acctName) print N '会员卡号:' + @acctNbr + N '-----会员姓名:' + @acctName end fetch next From MyCursor Into @acctNbr, @acctName end Close MyCursor Deallocate MyCursor --execute test_cursor
以上就是本篇文章的所有内容了,如果有小伙伴还需要了解其他相关java入门知识,可以来本站官网了解详情。
推荐阅读: