我想按文章标题对我的网格视图进行排序,其下拉列表如下图所示。 我将网格视图连接到dataSqlsouce。 但它给了我这个错误: 将nvarchar值“XX”转换为数据类型int时转换失败。
我在datasqlsource中的Sql cod是:
SELECT addtitle.title,
submitmanuscript.status,
addarticletype.type,
addarticletype.username AS Expr1,
addarticletype.articletype,
submitmanuscript.date,
submitmanuscript.articlenum,
addarticletype.articleid,
CONVERT (VARCHAR(10), submitmanuscript.date, 103) AS date1,
addarticletype.checkfinish,
rolinfo.*
FROM addarticletype
INNER JOIN addtitle
ON addarticletype.articleid = addtitle.articleid
INNER JOIN submitmanuscript
ON addarticletype.articleid = submitmanuscript.articleid
INNER JOIN rolinfo
ON addarticletype.username = rolinfo.username
WHERE ( addarticletype.articleid IN (SELECT articleid
FROM addupload_4 AS AddUpload_4_1
WHERE
( addarticletype.checkfinish = '0' )) )
AND ( addarticletype.type = @Type )
ORDER BY CASE
WHEN @order = 'AddArticleType.ArticleID' THEN addarticletype.articleid
WHEN @order = 'Title' THEN CONVERT(VARCHAR, title)
WHEN @order = 'family' THEN family
WHEN @order = 'AddArticleType.ArticleType' THEN
addarticletype.articletype
END
任何人都可以帮助我吗?
答案 0 :(得分:2)
我认为问题出在你的ORDER BY CASE语句中 - 你试图按int和/或nvarchars进行排序,它不会允许:
看到这个小提琴:
http://sqlfiddle.com/#!3/db37b/1
您可以通过将所有排序值转换为nvarchar来解决此问题,尽管@Gordons答案似乎更合适:
ORDER BY CASE
WHEN @order = 'AddArticleType.ArticleID' THEN CONVERT(nvarchar,addarticletype.articleid)
WHEN @order = 'Title' THEN CONVERT(nvarchar, title)
WHEN @order = 'family' THEN CONVERT(nvarchar, family)
WHEN @order = 'AddArticleType.ArticleType' THEN
CONVERT(nvarchar, addarticletype.articletype )
END
您可能只需要在ArticleId字段上使用CONVERT,但我继续把它放在所有字段上,因为我不知道他们的数据类型。
祝你好运。答案 1 :(得分:1)
case
语句必须选择一种类型才能返回。第一个then
是导致问题的数字。
只需将子句拆分为单独的case
语句:
ORDER BY (CASE WHEN @order = 'AddArticleType.ArticleID' THEN AddArticleType.ArticleID end),
(case WHEN @order = 'Title' then CONVERT(VARCHAR,Title) end),
(case WHEN @order = 'family' THEN family end),
(case WHEN @order = 'AddArticleType.ArticleType' THEN AddArticleType.ArticleType END)