2013-02-22 2 views
2

entrée comme 111111 et 101,102,103,104Échec de la conversion en curseur dans la procédure stockée

Je veux vérifier si l'utilisateur a accès à cette demande ou non ...

J'ai essayé un curseur comme indiqué, mais je reçois ce erreur:

Conversion failed when converting the varchar value '101,102,103,104' to data type int.

code:

ALTER PROCEDURE [dbo].[ValidateRqstId] 
    @UserID VARCHAR(50), 
    @RsqtIDs VARCHAR(300) 
AS 
BEGIN 
    Declare @RqstId int 
    Declare @Result int 
    Declare @UserIDToCheck VARCHAR(50) 
    Declare @RqstUserVal cursor for 
       Select RequestId 
       from REQUEST_LIST 
       where RequestId in (@RsqtIDs) 

    BEGIN 

     OPEN RqstUserVal 
     FETCH NEXT from RqstUserVal into @RqstId 

     WHILE(@@fetch_status <> -1) 
     BEGIN 
     SET @UserIDToCheck = (
       select UserId from dbo.REQUEST_LIST where RequestId = @RqstId) 

     Print(@UserIDToCheck) 

     If(@UserIDToCheck != @UserID) 
      SET @Result = 99 ; 

     --Fetch the next row from the cursor 
     FETCH RqstUserVal into @RqstId 
     END 
    END 

    CLOSE RqstUserVal 
    Deallocate RqstUserVal 

    RETURN @Result 
END 

Merci dans un AVANCES

+1

Vous ne pouvez pas automagiquement développer un varchar en éléments d'une liste in. – muhmud

Répondre

1

En fonction de votre Verion SQL Server, vous pouvez utiliser une fonction de valeur table comme dans ce court exemple

Select * from dbo.test1 
Where ID in(
Select * from dbo.F_SplitAsIntTable('1,123,234,456')) 

Fonction définie comme

CREATE FUNCTION F_SplitAsIntTable 
(
@txt varchar(max) 
) 
RETURNS 
@tab TABLE 
(
ID int 
) 
AS 
BEGIN 
    declare @i int 
    declare @s varchar(20) 
    Set @i = CHARINDEX(',',@txt) 
    While @i>1 
     begin 
      set @s = LEFT(@txt,@i-1) 
      insert into @tab (id) values (@s) 
      Set @txt=RIGHT(@txt,Len(@txt)[email protected]) 
      Set @i = CHARINDEX(',',@txt) 
     end 
    insert into @tab (id) values (@txt) 
    RETURN 
END 
GO 
+0

Merci bummi ... j'ai créé une fonction et un problème résolu ... :) – Rohit

Questions connexes