在 SQL 服务器上使用 pymssql 时,如何获取包含 IF ELSE 的查询结果?

How can I get the query result which includes IF ELSE when using pymssql on SQL Server?

我在 python 应用程序中使用 pymssql 连接到 SQL 服务器数据库。我有一个定义如下的查询语句:

DECLARE @type int
SELECT @type = type FROM myTable WHERE id = 1000

IF @type >= 700
    SELECT 1
ELSE
    SELECT 0

当我在 SQL 服务器中 运行 以上 sql 时,我可以获得 10 的输出,这取决于 type 行的值。但是当我 运行 python 中的代码时,它总是 returns None:

cursor.execute(sql)  # sql is the above select statement
cursor.fetchone()  # this line return None

我期望得到的值是 1 或 0,如何在 pymssql 中实现?

为此更新您的代码。 type 是 sql 服务器中的保留关键字。添加了 TOP 1 以确保我们只会得到 [type].

的 1 个结果
DECLARE @type int
SET @type = 0
SELECT TOP 1 @type = [type] FROM myTable WHERE id = 1000

IF @type >= 700
    SELECT 1
ELSE
    SELECT 0