在 select 语句中显示 %ROWCOUNT 值

Display %ROWCOUNT value in a select statement

%ROWCOUNT的结果如何显示在SQL语句中。

示例

Select top 10 * from myTable.

我希望结果集中返回的每一行都有一个 rowCount

+----------+--------+---------+
|rowNumber |Column1 |Column2  |
+----------+--------+---------+
|1         |A       |B        |
|2         |C       |D        |
+----------+--------+---------+

没有任何简单的方法可以做到这一点。您可以添加具有此功能的 Sql 过程,并在 SQL 语句中使用它。 例如,class:

Class Sample.Utils Extends %RegisteredObject
{

ClassMethod RowNumber(Args...) As %Integer [ SqlProc, SqlName = "ROW_NUMBER" ]
{
  quit $increment(%rownumber)
}

}

然后,你可以这样使用它:

SELECT TOP 10 Sample.ROW_NUMBER(id) rowNumber, id,name,dob 
FROM sample.person 
ORDER BY ID desc

你会得到类似下面的东西

+-----------+-------+-------------------+-----------+
|rowNumber  |ID     |Name               |DOB        |
+-----------+-------+-------------------+-----------+
|1          |200    |Quigley,Neil I.    |12/25/1999 |
|2          |199    |Zevon,Imelda U.    |04/22/1955 |
|3          |198    |O'Brien,Frances I. |12/03/1944 |
|4          |197    |Avery,Bart K.      |08/20/1933 |
|5          |196    |Ingleman,Angelo F. |04/14/1958 |
|6          |195    |Quilty,Frances O.  |09/12/2012 |
|7          |194    |Avery,Susan N.     |05/09/1935 |
|8          |193    |Hanson,Violet L.   |05/01/1973 |
|9          |192    |Zemaitis,Andrew H. |03/07/1924 |
|10         |191    |Presley,Liza N.    |12/27/1978 |
+-----------+-------+-------------------+-----------+

如果您愿意重写您的查询,那么您可以使用视图计数器来完成您正在寻找的事情。这里是 link 到 docs.

简短的版本是将查询移动到 FROM 子句子查询中并使用特殊字段 %vid。

SELECT v.%vid AS Row_Counter, Name 
  FROM (SELECT TOP 10 Name FROM Sample.Person ORDER BY Name) v 

Row_Counter Name
1           Adam,Thelma P.
2           Adam,Usha J.
3           Adams,Milhouse A.
4           Allen,Xavier O.
5           Avery,James R.
6           Avery,Kyra G.
7           Bach,Ted J.
8           Bachman,Brian R.
9           Basile,Angelo T.
10          Basile,Chad L.