Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么可以';我是否在SQL视图中查询单个列?_Sql_Sql Server_View - Fatal编程技术网

为什么可以';我是否在SQL视图中查询单个列?

为什么可以';我是否在SQL视图中查询单个列?,sql,sql-server,view,Sql,Sql Server,View,我使用脚本创建了一个视图,其中包含数据库中每个表的表名以及相应表中的数据行数。使用选择*一切正常。然而,我只想查询某些行,但我似乎不能这样做 视图是使用以下脚本创建的(该脚本归功于): 当我对结果视图运行Select*时,会得到如下结果: name tableName RowCount dbo Table1 2 dbo Table2 1230 dbo Table3 0 如果我只查询tableName列,

我使用脚本创建了一个视图,其中包含数据库中每个表的表名以及相应表中的数据行数。使用
选择*
一切正常。然而,我只想查询某些行,但我似乎不能这样做

视图是使用以下脚本创建的(该脚本归功于):

当我对结果视图运行
Select*
时,会得到如下结果:

name    tableName     RowCount
dbo     Table1          2
dbo     Table2          1230
dbo     Table3          0
如果我只查询tableName列,则可以正常工作并返回表名。但是,如果我也尝试查询RowCount列,我会在关键字“RowCount附近得到错误消息
语法不正确。无论我是否限定数据库,都会发生这种情况——它似乎无法将
RowCount
识别为我可以在查询中调用的有效列。所以这个脚本失败了:

SELECT RowCount 
FROM RowCount_AllTables;
但这一条有效:

SELECT tableName 
FROM RowCount_AllTables;
发生什么事了?我希望能够在查询的视图中为列名添加别名,但我不能这样做,只要我不能引用
RowCount


(仅供参考在SQL Server 2014中运行此功能)

行数是保留字,您可以使用[]选择保留字作为:

[Rowcount]

感谢@sgedes为我们指路。删除了该视图,将用于创建该视图的脚本更改为使用另一个名称作为行计数列,现在它可以按预期工作。该问题与
Rowcount
为保留字存在冲突

更改了此行的创建表脚本:

SELECT  distinct  sys.schemas.name, sys.tables.name AS tableName,
 sys.dm_db_partition_stats.row_count AS 'RowCount'
致:


…此时,我可以根据需要引用
行数
列。

行数
是一个保留字:宾果!谢谢-我在脚本中重命名了列名以创建视图,现在可以使用了。
SELECT  distinct  sys.schemas.name, sys.tables.name AS tableName,
 sys.dm_db_partition_stats.row_count AS 'RowCount'
SELECT  distinct  sys.schemas.name, sys.tables.name AS tableName,
 sys.dm_db_partition_stats.row_count AS 'Row_Count'