Conversion failed in cursor in stored procedure

前端 未结 1 1541
甜味超标
甜味超标 2020-12-21 16:02

Input like 111111 and 101,102,103,104

I want to check whether user has access on this requests or not...

I tried a cursor as shown,

1条回答
  •  自闭症患者
    2020-12-21 16:33

    Depending on your SQL-Server Verion you can use a Table-Valued Function like in this short example

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

    Function defined as

    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)-@i)
              Set @i = CHARINDEX(',',@txt)
            end
        insert into @tab (id) values (@txt) 
        RETURN 
    END
    GO
    

    0 讨论(0)
提交回复
热议问题