【问题标题】:Not able to fetch Data from Stored Procedure IN Clause无法从存储过程 IN 子句中获取数据
【发布时间】:2018-05-28 09:50:03
【问题描述】:

我正在尝试执行存储过程,但无法从中获取数据。 下面是存储过程。

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'];

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

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

阅读 cmets 后,我尝试了以下代码,但无法正常工作。

/*
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

并将存储过程修改为

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

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

【问题讨论】:

  • 您的代码将不起作用。您传递的参数必须使用动态 sql。
  • 您还可以使用表值函数将逗号分隔的列表转换为表变量,并在连接子句中使用它。
  • TRYsplit函数/TVP

标签: sql-server stored-procedures


【解决方案1】:

如果您使用的是 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 - 用于拆分的集成功能

如下所述修改您的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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 2010-09-19
  • 2013-07-12
  • 1970-01-01
相关资源
最近更新 更多