SqlServer 存储过程中使用游标示例

USE [Test]
GO
/****** Object:  StoredProcedure [dbo].[testKenter]    Script Date: 06/17/2010 16:08:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		kenter
-- Create date: 2010-06-17
-- Description:	存储过程联系,将Table_1表插入10条数据,
-- 然后每个字段加"_2"复制到Table_2表

/*--调用示例

    USE [Test]
    GO
    DECLARE	@return_value int
    EXEC	@return_value = [dbo].[testKenter]
    SELECT	'Return Value' = @return_value
    GO
--*/
-- =============================================
ALTER PROCEDURE [dbo].[testKenter] 
	-- 此存储过程用到的参数入口设置
	--<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
	--<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
declare @insertCount int;
declare @iName varchar(20);
declare @iPwd varchar(50);
declare @GoodsCode varchar(20);
declare @GoodsName varchar(20);
BEGIN
	SET NOCOUNT ON;
	SET @insertCount = 0;
	delete from dbo.Table_1;
	while(@insertCount<10) begin
	  SET @insertCount = @insertCount+1;
	  set @iName = '账号'+Convert(char(5),@insertCount);
	  set @iPwd = '密码'+Convert(char(5),@insertCount);
	  insert into dbo.Table_1 (name,pwd) values (@iName,@iPwd);
	end
    --遍历Table1
    delete from dbo.Table_2;
    Declare table1Cusror Cursor Scroll FOR 
      SELECT name,pwd FROM dbo.Table_1;
    Open table1Cusror;
    FETCH NEXT FROM table1Cusror into @iName,@iPwd;
    WHILE @@FETCH_STATUS = 0 
    BEGIN 
      print @iName+':'+@iPwd;
      set @iName = @iName + 'C'; 
      insert into dbo.Table_2 (name2,pwd2) values (@iName,@iPwd);
      FETCH NEXT FROM table1Cusror into @iName,@iPwd ;
    END 
    CLOSE table1Cusror;
    Deallocate table1Cusror;
END

 

你可能感兴趣的:(C++,c,C#,Go)