SQL 出错记录
1 "count aggregate 运算不能以uniqueidentifier 数据类型作为参数"
出错语句:
COUNT(Answer_1.aListenId)
解决方法:
COUNT(cast(Answer_1.aListenId as varchar(40)))
将uniqueidentifier类型的数据通过cast函数转换为varchar类型
2 "无法解决 equal to 操作的排序规则冲突"
出错语句:
CREATE PROCEDURE [dbo].[AnswerShow]
@lId uniqueidentifier
AS
/* SET NOCOUNT ON */
SELECT aUserId,aTime,aId,aTitle,aAnswer,
(SELECT uAnswerCount FROM [User]
WHERE (uId=Answer.aUserId))
AS aUserAnswerCount
FROM Answer LEFT JOIN [User] On Answer.aUserId=[User].uId
WHERE aListenId=@lId
ORDER BY aTime ASC
RETURN
GO
@lId uniqueidentifier
AS
/* SET NOCOUNT ON */
SELECT aUserId,aTime,aId,aTitle,aAnswer,
(SELECT uAnswerCount FROM [User]
WHERE (uId=Answer.aUserId))
AS aUserAnswerCount
FROM Answer LEFT JOIN [User] On Answer.aUserId=[User].uId
WHERE aListenId=@lId
ORDER BY aTime ASC
RETURN
GO
解决方法:
CREATE PROCEDURE [dbo].[AnswerShow]
@lId uniqueidentifier
AS
/* SET NOCOUNT ON */
SELECT aUserId,aTime,aId,aTitle,aAnswer,
(SELECT uAnswerCount FROM [User]
WHERE (uId=Answer.aUserId
COLLATE Chinese_PRC_CS_AS))
AS aUserAnswerCount
FROM Answer LEFT JOIN [User] On Answer.aUserId=[User].uId
COLLATE Chinese_PRC_CS_AS
WHERE aListenId=@lId
ORDER BY aTime ASC
RETURN
GO
@lId uniqueidentifier
AS
/* SET NOCOUNT ON */
SELECT aUserId,aTime,aId,aTitle,aAnswer,
(SELECT uAnswerCount FROM [User]
WHERE (uId=Answer.aUserId
COLLATE Chinese_PRC_CS_AS))
AS aUserAnswerCount
FROM Answer LEFT JOIN [User] On Answer.aUserId=[User].uId
COLLATE Chinese_PRC_CS_AS
WHERE aListenId=@lId
ORDER BY aTime ASC
RETURN
GO
出错的原因是指定了User表中排序规则Chinese_PRC_CS_AS,在使用WHERE语句以及连接表的时候需要再次置顶排序规则.