【问题标题】:Can I Use Order by to sort Stored Procedure results?我可以使用 Order by 对存储过程结果进行排序吗?
【发布时间】:2010-10-26 12:49:46
【问题描述】:

简单地说,我有这个 SQL 语句:

EXEC xp_cmdshell 'tasklist' 

我们可以使用order bywhere 对结果进行排序或过滤吗?

谢谢,

【问题讨论】:

标签: tsql stored-procedures


【解决方案1】:

我检查了 jamietre 链接,这是完整的答案:

Create table  #MyTempTable
(output varchar(max))

INSERT INTO #MyTempTable
EXEC xp_cmdshell 'tasklist' 

select * from #MyTempTable where output like 'ie%' order by output 

谢谢大家...

【讨论】:

    【解决方案2】:

    您需要先将结果输出到临时表中。这应该告诉你怎么做

    Insert results of a stored procedure into a temporary table

    【讨论】:

      【解决方案3】:

      不直接。您可以将 exec 插入到临时表或表变量中,然后对其进行排序

      【讨论】:

        【解决方案4】:

        多次运行上述查询时,您可能会遇到此错误:There is already an object named '#MyTempTable' in the database.

        为了缓解这种情况,您可以在创建temp table 之前使用如下DROP IF EXISTS 语句。

        IF OBJECT_ID(N'tempdb..#MyTempTable') IS NOT NULL
        BEGIN
        DROP TABLE #MyTempTable
        END
        CREATE TABLE #MyTempTable
        (OUTPUT VARCHAR(max))
        
        INSERT INTO #MyTempTable
        EXEC xp_cmdshell 'tasklist' 
        
        SELECT * FROM #MyTempTable WHERE OUTPUT like 'ie%' ORDER BY OUTPUT
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-03
          • 2012-03-11
          • 1970-01-01
          相关资源
          最近更新 更多