如何循环SELECT语句sql的结果并将结果用于存储过程?

时间:2016-03-24 17:28:43

标签: sql sql-server tsql stored-procedures cursor

我想通过使用游标将select查询的结果用于存储过程,但它不起作用。

这是我的代码:

DECLARE @NumberPhone varchar(50)
DECLARE CUR CURSOR STATIC FOR

SELECT  MobilePhone
FROM info_client

OPEN CUR
FETCH NEXT FROM CUR INTO @NumberPhone 

WHILE @@FETCH_STATUS = 0
BEGIN
     DECLARE @return_value int, @idCli int, @sCode varchar(2)

     EXEC   @return_value = StoredP_Test
            @sLignePhone = @NumberPhone,
            @sIMEI ='00000000000000000',
            @idCli = @idCli OUTPUT,
            @sCode = @sCode OUTPUT

    SELECT @idCli as N'@idCli',
           @sCode as N'@sCode'  

    FETCH NEXT FROM CUR INTO @NumberPhone 

    SELECT 'Return Value' = @return_value
END

CLOSE CUR 
DEALLOCATE CUR

此代码与选择查询的行数重复相同的结果。

1 个答案:

答案 0 :(得分:1)

您的代码初次获取

FETCH NEXT FROM CUR INTO @NumberPhone 

你的代码在循环中有下一个提取

FETCH NEXT FROM CUR INTO @sLigneAssValue

您不想在@NumberPhone变量中获取下一个吗?

@NumberPhone变量永远不会改变。