无法从存储过程IN子句中获取数据

时间:2022-02-28 12:52:10

I am trying to execute a store Procedure but not able to fetch the data from it. Below is the Store Procedure.

我正在尝试执行存储过程但不能从中获取数据。以下是商店程序。

USE test
GO
CREATE PROCEDURE SELECT_IDs @idList nvarchar(1750)
AS
BEGIN TRY
SELECT DISTINCT child FROM example WHERE parent IN (@idList)
UNION
SELECT DISTINCT parent FROM example WHERE child IN (@idList)
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber;
END CATCH
GO

EXEC SELECT_IDs @idList = ['100','101'];

After executing the above procedure it is giving me no data where as if i run the same select query it is giving me data.

执行上面的过程后,它没有给我任何数据,好像我运行相同的选择查询,它给我数据。

SELECT DISTINCT child FROM example WHERE parent IN ('100','101')

After reading the comments i tried the below code but not working.

阅读评论后,我尝试了下面的代码,但没有工作。

/*
Splits string into parts delimitered with specified character.
*/
CREATE FUNCTION [dbo].[SDF_SplitString]
(
@sString nvarchar(2048),
@cDelimiter nchar(1)
)
RETURNS @tParts TABLE ( part nvarchar(2048) )
AS
BEGIN
if @sString is null return
declare @iStart int,
        @iPos int
if substring( @sString, 1, 1 ) = @cDelimiter 
begin
    set @iStart = 2
    insert into @tParts
    values( null )
end
else 
    set @iStart = 1
while 1=1
begin
    set @iPos = charindex( @cDelimiter, @sString, @iStart )
    if @iPos = 0
        set @iPos = len( @sString )+1
    if @iPos - @iStart > 0          
        insert into @tParts
        values  ( substring( @sString, @iStart, @iPos-@iStart ))
    else
        insert into @tParts
        values( null )
    set @iStart = @iPos+1
    if @iStart > len( @sString ) 
        break
end
RETURN
END

And Modified the stored Procedure to

并修改了存储过程

USE test
GO
CREATE PROCEDURE SELECT_IDs @idList nVarchar(2048)
AS
BEGIN TRY
SELECT DISTINCT child FROM example WHERE parent IN ([dbo].[SDF_SplitString] 
('@idList',',')) or child IN ([dbo].[SDF_SplitString]('@idList',','))
UNION
SELECT DISTINCT parent FROM example WHERE parent IN ([dbo].[SDF_SplitString] 
(@idList,',')) or child IN ([dbo].[SDF_SplitString](@idList,','))
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber;
END CATCH
GO

But getting the issue : Cannot find either column "dbo" or the user-defined function or aggregate "dbo.SDF_SplitString", or the name is ambiguous.

但是得到问题:无法找到列“dbo”或用户定义的函数或聚合“dbo.SDF_SplitString”,或者名称不明确。

1 个解决方案

#1


1  

If you are using SQL Server 2016 and above, then try using STRING_SPLIT

如果您使用的是SQL Server 2016及更高版本,请尝试使用STRING_SPLIT

USE test GO
    CREATE PROCEDURE SELECT_IDs @idList nvarchar(1750) AS BEGIN TRY
    SELECT DISTINCT child
    FROM example
    WHERE parent IN
        (SELECT value
         FROM STRING_SPLIT(@idList, ','))
    UNION
    SELECT DISTINCT parent
    FROM example
    WHERE child IN
        (SELECT value
         FROM STRING_SPLIT(@idList, ',')) END TRY BEGIN CATCH
    SELECT ERROR_NUMBER() AS ErrorNumber; END CATCH 
GO

EXEC SELECT_IDs @idList = '100,101';

UPDATE 1.0 - Integrating function for split

更新1.0 - 集成拆分功能

Modify your STORED PROCEDUREas mentioned below, It seems you missed SELECT clause for the function call.

修改下面提到的STORED PROCEDURE,看来你错过了函数调用的SELECT子句。

USE test GO
CREATE PROCEDURE SELECT_IDs @idList nVarchar(2048) AS BEGIN TRY
SELECT DISTINCT child
FROM example
WHERE parent IN (SELECT * FROM SDF_SplitString(@idList, ','))
  OR child IN (SELECT * FROM SDF_SplitString(@idList, ','))
UNION
SELECT DISTINCT parent
FROM example
WHERE parent IN (SELECT * FROM SDF_SplitString(@idList, ','))
  OR child IN (SELECT * FROM SDF_SplitString(@idList, ',')) END TRY BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber; END CATCH GO

#1


1  

If you are using SQL Server 2016 and above, then try using STRING_SPLIT

如果您使用的是SQL Server 2016及更高版本,请尝试使用STRING_SPLIT

USE test GO
    CREATE PROCEDURE SELECT_IDs @idList nvarchar(1750) AS BEGIN TRY
    SELECT DISTINCT child
    FROM example
    WHERE parent IN
        (SELECT value
         FROM STRING_SPLIT(@idList, ','))
    UNION
    SELECT DISTINCT parent
    FROM example
    WHERE child IN
        (SELECT value
         FROM STRING_SPLIT(@idList, ',')) END TRY BEGIN CATCH
    SELECT ERROR_NUMBER() AS ErrorNumber; END CATCH 
GO

EXEC SELECT_IDs @idList = '100,101';

UPDATE 1.0 - Integrating function for split

更新1.0 - 集成拆分功能

Modify your STORED PROCEDUREas mentioned below, It seems you missed SELECT clause for the function call.

修改下面提到的STORED PROCEDURE,看来你错过了函数调用的SELECT子句。

USE test GO
CREATE PROCEDURE SELECT_IDs @idList nVarchar(2048) AS BEGIN TRY
SELECT DISTINCT child
FROM example
WHERE parent IN (SELECT * FROM SDF_SplitString(@idList, ','))
  OR child IN (SELECT * FROM SDF_SplitString(@idList, ','))
UNION
SELECT DISTINCT parent
FROM example
WHERE parent IN (SELECT * FROM SDF_SplitString(@idList, ','))
  OR child IN (SELECT * FROM SDF_SplitString(@idList, ',')) END TRY BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber; END CATCH GO