需要帮助。基于此查询,我有2个问题。
SELECT cdd.FieldID,cdd.PlanTypeID,pt.IsFinancial
FROM ClientDataDictionary cdd
INNER JOIN #tblPlanTypes pt ON cdd.PlanTypeID = pt.PlanTypeID
INNER JOIN ClientDataDictionaryFieldType cddftype
ON cdd.FieldId = cddftype.FieldID AND cdd.PlanTypeId = cddftype.PlanTypeID
WHERE cdd.ClientId = @ClientID AND cdd.IsHidden = 0
AND cddftype.FieldTypeID = 4
AND cddftype.FieldID = cddftype.ParentFieldIDSectionTitle
AND (SELECT COUNT(cddftype2.FieldID)
FROM ClientDataDictionaryFieldType cddftype2
Where cddftype2.ParentFieldIDSectionTitle = cdd.FieldID
AND cddftype2.PlanTypeID = cdd.PlanTypeID
AND EXISTS(Select tbl.FieldID From #tblSelectedFields tbl
Where tbl.FieldID = cddftype2.FieldID AND tbl.PlantypeID =cdd.PlanTypeID COLLATE SQL_Latin1_General_CP1_CI_AS)
) != 0 COLLATE SQL_Latin1_General_CP1_CI_AS
COLLATE
关键字放在此查询中?我知道临时表有不同的整理“类型”。COLLATE DATABASE_DEFAULT
,但错误仍然存在。这会解决问题吗?请在这里帮助我。谢谢!
[更新1]
ClientID
设置为UNIQUEIDENTIFIER
数据类型。
[更新2] 这是表格定义
#tblPlanTypes
PlanTypeId nvarchar(50),
PlanType nvarchar(max),
Prefix NVARCHAR(10),
IsFinancial BIT,
TotalCount BIGINT,
PlanCount BIGINT
ClientDataDictonary
[ClientDataDictionaryId] [uniqueidentifier] NOT NULL ROWGUIDCOL CONSTRAINT [DF_ClientDataDictionary_ClientDataDictionaryId] DEFAULT (newid()),
[ClientId] [uniqueidentifier] NOT NULL,
[PlanTypeId] [uniqueidentifier] NULL,
[FieldId] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[FieldText] [nvarchar] (max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[FieldType] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DefaultValue] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[HelpText] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[IsHidden] [bit] NULL CONSTRAINT [DF_ClientDataDictionary_IsHidden] DEFAULT ((0)),
[IsSummable] [bit] NULL,
[IsRequired] [bit] NULL,
[IsContractRenewal] [bit] NULL,
[HasAlert] [bit] NULL CONSTRAINT [DF_ClientDataDictionary_HasAlert] DEFAULT ((0)),
[AlertMessage] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[CentralLanguageId] [uniqueidentifier] NOT NULL,
[LookupTypeName] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[RowVersion] [timestamp] NULL,
[LastUpdated] [datetime] NULL CONSTRAINT [DF_ClientDataDictionary_LastUpdated] DEFAULT (getutcdate()),
[Sequence] [int] NULL
ClientDataDictionaryFieldType
[ClientDataDictionaryFieldTypeID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_ClientDataDictionaryFieldType_ClientDataDictionaryFieldType] DEFAULT (newid()),
[PlanTypeID] [uniqueidentifier] NULL,
[FieldID] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[FieldTypeID] [int] NULL,
[ParentFieldIDSectionTitle] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
#tblSelectedFields
FieldID nvarchar(255),
PlanTypeID UniqueIdentifier,
FieldText nvarchar(MAX),
sequence int,
FieldType nvarchar(100),
IsFinancial bit
[更新3]
尝试了@Mack的建议,但出现了一个新的错误。 Expression type uniqueidentifier is invalid for COLLATE clause.
这是由具有uniqueidentifier数据类型的@ClientID引起的。有什么建议吗?
答案 0 :(得分:2)
首先 - 如果您在示例中包含表格的描述,那么这里的人们将能够更快地为您提供帮助。
第二个调试此查询自己将其分解为逻辑部分(分隔任何子查询,一次删除一个表),然后从每个逻辑部分中删除where子句,直到查询工作,这将帮助您确定原因问题,请记住,join子句也可能是问题所在。
最后,下面的代码将允许您确定要查询的每个列的排序规则类型,它来自Pinal Dave的this article
USE yourdb
GO
SELECT name, collation_name
FROM sys.columns
WHERE OBJECT_ID IN ( SELECT OBJECT_ID
FROM sys.objects
WHERE type = 'U'
AND name = 'yourtable')
AND name IN('yourcolumn1','yourcolumn2',.....,'yourcolumnX')
感谢表格定义...... 乍一看,你的整理问题在于这个领域
此联接中使用的PlanTypeId nvarchar(50)
:INNER JOIN #tblPlanTypes pt ON cdd.PlanTypeID = pt.PlanTypeID
如果您将COLLATE应用于联接,则应解决问题。
答案 1 :(得分:0)
您需要在比较/连接运算符中的textual(char,nchar,varchar,nvarchar)字段名称之后放置COLLATE关键字 - 'alias1.txtfield1 COLLATE something = alias2.txtfield2 COLLATE something'。将COLLATE与整数字段一起使用是没有意义的(我希望你的ID是整数种?)。