简体   繁体   English

mssql服务器中表的记录数

[英]Count of records of table in mssql server

I am trying to fetch the no. 我正在尝试获取否。 of records/count from a mssql table but getting: mssql表中的记录/计数,但得到:

pymssql.ColumnsWithoutNamesError: Specified as_dict=True and there are columns with no names: [0] pymssql.ColumnsWithoutNamesError:指定为as_dict = True,并且存在没有名称的列:[0]

Here is what I am trying: 这是我正在尝试的:

cur = hook.get_cursor()                        
cur.execute(self.sql)

And the query is: 查询是:

select count(*) from abc

See here : 这里

The problem is that if you open the connection or cursor with as_dict=True, then every column in the output has to have a name. 问题是,如果使用as_dict = True打开连接或游标,则输出中的每一列都必须具有名称。 With a regular database table column, the output name inherits from the column name. 对于常规数据库表列,输出名称将从列名称继承。 If you have a SQL expression like calling a function on a column name (eg: MAX(foo)), then the output column doesn't have a name and so it doesn't get displayed. 如果您有一个SQL表达式,例如在列名上调用函数(例如:MAX(foo)),则输出列没有名称,因此不会显示。 There are two things you can do to make it work: 您可以执行以下两项操作使其工作:

 1. Give the SQL expression a name -- eg: SELECT MAX(foo) AS [MAX(foo)]... 2. Don't use as_dict=True for these queries 

I think same for COUNT function. 我认为COUNT函数也是如此。 So try to give the result a name with "AS". 因此,尝试使用“ AS”为结果命名。

Or have a look for the _mssql module of pymsql (here is also an example for COUNT: 或者察看了pymsql_mssql模块(这里也是一个例子为COUNT:

import _mssql
conn = _mssql.connect(server='SQL01', user='user', password='password', database='mydatabase')
numemployees = conn.execute_scalar("SELECT COUNT(*) FROM employees")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM