我使用这个查询在我的数据库中创建了一个函数:
CREATE FUNCTION dbo.fnSplit(
@sInputList VARCHAR(MAX) -- List of delimited items
, @sDelimiter VARCHAR(MAX) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(MAX))
BEGIN
DECLARE @sItem VARCHAR(MAX)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
BEGIN
SELECT
@sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
END
IF LEN(@sInputList) > 0
INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END
GO
SQL Server 返回:Command(s) completed successfully.
然后我尝试运行这个查询:
SELECT * FROM maj_Posts a
WHERE FeedID = (SELECT dbo.fnSplit(b.FeedIDs) FROM maj_Magazines b WHERE OwnerID = 1)
ORDER BY countOfComments DESC
它返回一个错误:Msg 4121, Level 16, State 1, Line 1
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fnSplit", or the name is ambiguous.
请注意,这b.FeedIDs
是一个包含逗号分隔数字的字符串,如下所示:1,2,4
我想获取maj_Posts
它们是...a.FeedID
中的数字之一的b.FeedIDs
行(例如,如果b.FeedIDs
是1,2,4
,我需要maj_Posts
它们FeedID
是 1 或 2 或 4 的行。)
问题是什么?