我正在尝试使用CodeFluent加载未在“FileObjectMetaData”表中引用的所有“标记”记录。基本上将查询从此帖子转换为CodeFluent:
Elegant way to delete rows which are not referenced by other table
我有这种表关系:
我怎样才能达到这种效果?
编辑一个:
这是CodeFluent模型中的多对多关系,因此在实际生成的SQL表中有一个中间链接表,其中包含:
Tag和FileObjectMetaData表本身没有直接的外键。
编辑二:
最后,我选择采用C#方法,因为使用CodeFluent CFQL似乎没有可靠的方法。
public static TagCollection LoadUnusedTags()
{
TagCollection unusedTags = new TagCollection();
TagCollection allTags = TagCollection.LoadAll();
foreach (Tag aTag in allTags)
{
FileObjectMetaDataCollection fileObjectMetaDatas = FileObjectMetaDataCollection._LoadByTag(aTag.Name);
if (fileObjectMetaDatas.Count < 1)
unusedTags.Add(aTag);
}
if (unusedTags.Count < 1)
return null;
return unusedTags;
}
答案 0 :(得分:1)
LOAD() RAW
SELECT $Tag{Columns}$
FROM $Tag$
WHERE $Tag::Id$ NOT IN
(
SELECT [Tag_Id]
FROM [FileObjectMetadata_Tag_Tag]
WHERE [Tag_Id] IS NOT NULL
)
这将产生以下存储过程:
CREATE PROCEDURE [dbo].[Tag_LoadUnused]()
AS
SET NOCOUNT ON
SELECT [Tag].[Tag_Id],[Tag].[Tag_Name],[Tag].[_trackLastWriteTime],[Tag].[_trackCreationTime],[Tag].[_trackLastWriteUser],[Tag].[_trackCreationUser],[Tag].[_rowVersion]
FROM Tag
WHERE [Tag].[Tag_Id] NOT IN
(
SELECT [Tag_Id]
FROM [FileObjectMetadata_Tag_Tag]
WHERE [Tag_Id] IS NOT NULL
)
RETURN
GO
您可以创建一个与上述CFQL方法相同的视图:
然后您可以使用CFQL方法中的此视图:
load() from UsusedTag
生成的SQL视图和存储过程:
CREATE VIEW [dbo].[vTagUsusedTag]
AS
SELECT [Tag].[Tag_Id],[Tag].[Tag_Name],[Tag].[_trackLastWriteTime],[Tag].[_trackCreationTime],[Tag].[_trackLastWriteUser],[Tag].[_trackCreationUser],[Tag].[_rowVersion]
FROM Tag
WHERE [Tag].[Tag_Id] NOT IN
(
SELECT [Tag_Id]
FROM [FileObjectMetadata_Tag_Tag]
WHERE [Tag_Id] IS NOT NULL
)
GO
CREATE PROCEDURE [dbo].[Tag_LoadFromView]()
AS
SET NOCOUNT ON
SELECT DISTINCT [vTagUsusedTag].[Tag_Id], [vTagUsusedTag].[Tag_Name]
FROM [vTagUsusedTag]
RETURN
GO