SQL Server动态Order By查询,不同数据类型

发布于 2024-08-06 13:03:07 字数 615 浏览 6 评论 0原文

SQL Server 2000中,

查询

SELECT DISTINCT A.COLUMN1, B.COLUMN2 FROM TABLEA A, TABLEB B WHERE 
A.KEY_ID = B.FK_ID

ORDER BY CASE @ORDER_NAME
         WHEN 'COL1' THEN COLUMN1
         WHEN 'COL2' THEN COLUMN2
         ELSE
         COLUMN2
         END ASC

我有一个类似于Here A.COLUMN1 is varchar(50) and B.COLUMN2 is datetime的 。当 @ORDER_NAME 的值为“COL2”时,即当 order by 为 datetime 类型时,此查询完美运行,但当我使用“COL1”时,它会给出错误“从字符串转换日期时间时出现语法错误”。

我认为这是因为 SQL Server 试图将所有列转换为日期时间类型。但我找不到替代语法来动态对列进行排序。由于性能问题, EXEC 是不可能的,

我需要提到的是,我试图避免分支,否则上述操作也可以通过 IF ELSE 子句来完成。

In SQL Server 2000

I have a query like

SELECT DISTINCT A.COLUMN1, B.COLUMN2 FROM TABLEA A, TABLEB B WHERE 
A.KEY_ID = B.FK_ID

ORDER BY CASE @ORDER_NAME
         WHEN 'COL1' THEN COLUMN1
         WHEN 'COL2' THEN COLUMN2
         ELSE
         COLUMN2
         END ASC

Here A.COLUMN1 is varchar(50) and B.COLUMN2 is datetime. This query works perfectly when value of @ORDER_NAME is 'COL2' i.e. when order by is of type datetime but when I used 'COL1' it gives error 'Syntax error converting datetime from character string.'

I think that this is because SQL Server is trying to convert all the columns to datetime type. But I can't find an alternative syntax to dynamically sort the columns. EXEC is out of question due to performance issues

I need to mention that I am trying to avoid branching otherwise the above can be done by ane IF ELSE clause also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诠释孤独 2024-08-13 13:03:07

没有任何数据类型转换问题的版本:

SELECT  DISTINCT 
        A.COLUMN1, 
        B.COLUMN2 
FROM    TABLEA A, 
        TABLEB B 
WHERE   A.KEY_ID = B.FK_ID
ORDER BY 
        CASE @ORDER_NAME WHEN 'COL1' THEN COLUMN1 ELSE NULL END,
        CASE @ORDER_NAME WHEN 'COL2' THEN COLUMN2 ELSE NULL END

Version without any data type conversion issues:

SELECT  DISTINCT 
        A.COLUMN1, 
        B.COLUMN2 
FROM    TABLEA A, 
        TABLEB B 
WHERE   A.KEY_ID = B.FK_ID
ORDER BY 
        CASE @ORDER_NAME WHEN 'COL1' THEN COLUMN1 ELSE NULL END,
        CASE @ORDER_NAME WHEN 'COL2' THEN COLUMN2 ELSE NULL END
暖阳 2024-08-13 13:03:07

这应该有效:

IF @ORDER_NAME = 'COL1'
BEGIN
    SELECT DISTINCT A.COLUMN1, B.COLUMN2 FROM TABLEA A, TABLEB B WHERE A.KEY_ID = B.FK_ID
    ORDER BY COLUMN1 ASC
END

ELSE
BEGIN
    SELECT DISTINCT A.COLUMN1, B.COLUMN2 FROM TABLEA A, TABLEB B WHERE A.KEY_ID = B.FK_ID
    ORDER BY COLUMN2 ASC
END

This should work:

IF @ORDER_NAME = 'COL1'
BEGIN
    SELECT DISTINCT A.COLUMN1, B.COLUMN2 FROM TABLEA A, TABLEB B WHERE A.KEY_ID = B.FK_ID
    ORDER BY COLUMN1 ASC
END

ELSE
BEGIN
    SELECT DISTINCT A.COLUMN1, B.COLUMN2 FROM TABLEA A, TABLEB B WHERE A.KEY_ID = B.FK_ID
    ORDER BY COLUMN2 ASC
END
零度℉ 2024-08-13 13:03:07

你可以做类似的事情:

SELECT DISTINCT A.COLUMN1, B.COLUMN2, 
    CASE @ORDER_NAME
        WHEN 'COL1' THEN COLUMN1
        ELSE CONVERT(COLUMN2,  --convert to nvarchar in a way that orders properly, cant remember off the top of my head 
    END SortCol
FROM TABLEA A, TABLEB B
WHERE A.KEY_ID = B.FK_ID
ORDER BY SortCol ASC

update:

实际上你可以在查询中将日期转换为 varchar 并且它会起作用,我刚刚像上面那样做了,当排序列来自 xquery 表达式时,所以发布它像那样。

You could do something like:

SELECT DISTINCT A.COLUMN1, B.COLUMN2, 
    CASE @ORDER_NAME
        WHEN 'COL1' THEN COLUMN1
        ELSE CONVERT(COLUMN2,  --convert to nvarchar in a way that orders properly, cant remember off the top of my head 
    END SortCol
FROM TABLEA A, TABLEB B
WHERE A.KEY_ID = B.FK_ID
ORDER BY SortCol ASC

update:

Actually you could probably convert the date to an varchar in you query and it would work, I've just done it like the above before, when the sort columns where coming from an xquery expression so posted it like that.

飞烟轻若梦 2024-08-13 13:03:07

将它们全部转换为 varchar:

SELECT DISTINCT A.COLUMN1, B.COLUMN2 FROM TABLEA A, TABLEB B WHERE 
A.KEY_ID = B.FK_ID

ORDER BY CASE @ORDER_NAME
         WHEN 'COL1' THEN CONVERT(varchar(nn),COLUMN1)
         WHEN 'COL2' THEN CONVERT(varchar(23),COLUMN2,121)
         ELSE
         CONVERT(varchar(23),COLUMN2,121)
         END ASC

然后您甚至可以按多列排序,当您在按列排序中有许多相似的值时,这很好:

SELECT DISTINCT A.COLUMN1, B.COLUMN2 FROM TABLEA A, TABLEB B WHERE 
A.KEY_ID = B.FK_ID

ORDER BY CASE @ORDER_NAME
         WHEN 'COL1' THEN CONVERT(varchar(nn),COLUMN1)+CONVERT(varchar(23),COLUMN2,121)
         WHEN 'COL2' THEN CONVERT(varchar(23),COLUMN2,121)+RIGHT(REPLICATE('0',nn)+CONVERT(varchar(nn),COLUMN1),nn)
         ELSE CONVERT(varchar(23),COLUMN2,121)+RIGHT(REPLICATE('0',nn)+CONVERT(varchar(nn),COLUMN1),nn)
         END ASC

convert them to all to varchar:

SELECT DISTINCT A.COLUMN1, B.COLUMN2 FROM TABLEA A, TABLEB B WHERE 
A.KEY_ID = B.FK_ID

ORDER BY CASE @ORDER_NAME
         WHEN 'COL1' THEN CONVERT(varchar(nn),COLUMN1)
         WHEN 'COL2' THEN CONVERT(varchar(23),COLUMN2,121)
         ELSE
         CONVERT(varchar(23),COLUMN2,121)
         END ASC

you could then even sort by multiple columns then, which is good when you have many similar values in the sort by column:

SELECT DISTINCT A.COLUMN1, B.COLUMN2 FROM TABLEA A, TABLEB B WHERE 
A.KEY_ID = B.FK_ID

ORDER BY CASE @ORDER_NAME
         WHEN 'COL1' THEN CONVERT(varchar(nn),COLUMN1)+CONVERT(varchar(23),COLUMN2,121)
         WHEN 'COL2' THEN CONVERT(varchar(23),COLUMN2,121)+RIGHT(REPLICATE('0',nn)+CONVERT(varchar(nn),COLUMN1),nn)
         ELSE CONVERT(varchar(23),COLUMN2,121)+RIGHT(REPLICATE('0',nn)+CONVERT(varchar(nn),COLUMN1),nn)
         END ASC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文