我使用此查询在我的数据库中创建了一个函数:
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}}是1,2,4
,我需要来自maj_Posts
的行FeedID
为1或2或4。)
有什么问题?
答案 0 :(得分:3)
也许你的意思是:
SELECT * FROM maj_Posts a
LEFT OUTER JOIN dbo.maj_Magazines b
ON 1 = 1 AND b.OwnerID = 1
OUTER APPLY dbo.fnSplit(b.FeedIDs, ',') AS s
WHERE a.FeedID = s.item
ORDER BY countOfComments DESC;
或者
SELECT * FROM maj_Posts a
WHERE FeedID IN
(
SELECT item FROM maj_Magazines AS b
CROSS APPLY dbo.fnSplit(b.FeedIDs, ',')
WHERE b.OwnerID = 1
)
ORDER BY countOfComments DESC;
答案 1 :(得分:1)
USE [oberoi]
GO
/****** Object: StoredProcedure [dbo].[sp_AssetReport] Script Date: 03/23/2013 12:27:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_AssetReport]
AS
BEGIN
SELECT distinct am.assetid,am.computerName,
ao.os_name,ao.user_name,ao.reg_code, ao.reg_org, ao.reg_to,
ao.localization, ao.product_key,
-- an.ip_address,
amem.physical_mem,amem.virtual_mem,(Select top 1 ip_address from Asset_Network where assetId=am.assetid) as ip_address,ap.processor_name,ap.speed, ap.manufacturer,
api.serial_number,api.product_name, api.product_manufacturer,am.TagNo,am.PONo,dbo.capacity_checkn(apd.capacity) as capacity,am.Location,am.AssetOwner,am.CompanyCode,am.PurchaseDate,am.AssetCategory,am.Remarks
FROM Asset_mst As am INNER JOIN
-- Asset_Network AS an ON am.assetId = an.assetId INNER JOIN
--Asset_Inventory As AI on an.assetId=AI.assetId INNER JOIN
Asset_OperatingSystem AS ao on am.assetId=ao.assetId JOIN
Asset_Memory AS amem ON ao.assetId = amem.assetId INNER JOIN
Asset_Processor AS ap ON amem.assetId = ap.assetId INNER JOIN
Asset_ProductInfo AS api ON ap.assetId = api.assetid INNER JOIN
Asset_PhysicalDrive AS apd ON am.assetId = apd.assetid
where CONVERT(datetime,CONVERT(char(12),Am.createdatetime )) = CONVERT(datetime,CONVERT(char(12),ao.inventory_date ))
--And CONVERT(datetime,CONVERT(char(12),an.inventory_date )) = CONVERT(datetime,CONVERT(char(12),AI.inventoryDate ))
--And CONVERT(datetime,CONVERT(char(12),an.inventory_date )) = CONVERT(datetime,CONVERT(char(12),ao.inventory_date ))
And
CONVERT(datetime,CONVERT(char(12),Ao.inventory_date )) = CONVERT(datetime,CONVERT(char(12),amem.inventory_date ))
And CONVERT(datetime,CONVERT(char(12),amem.inventory_date )) = CONVERT(datetime,CONVERT(char(12),ap.inventory_date ))
And CONVERT(datetime,CONVERT(char(12),ap.inventory_date )) = CONVERT(datetime,CONVERT(char(12),api.inventory_date ))
And CONVERT(datetime,CONVERT(char(12),api.inventory_date )) = CONVERT(datetime,CONVERT(char(12),apd.inventory_date ))
and am.Isdelete=0
enter code here
End