0
import requests
import json
from datetime import datetime
import pymssql

server = 'devserver'
database = 'devdb'
username = 'devuser'
password = 'passwd'


connection = pymssql.connect(server, username, password, database)

query = "select Name,DateTimeStamp from Newtable where DateTimeStamp >=       (SELECT DATEADD(day, -1, GETDATE())"
cursor = connection.cursor()
cursor.execute(query)
row = cursor.fetchone()
cursor.close()
print(row)

我得到以下异常:

_mssql.MSSQLDatabaseException: (102, b"Incorrect syntax near ')'.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")

如果我改为 query = "SELECT GETDATE()"工作,则相同的代码。谁能帮我解决这个问题?

数据库条目,

db 条目的屏幕截图

4

1 回答 1

1

乍一看,左括号的数量不等于右括号的数量。添加右括号并尝试:

query = "select Name,DateTimeStamp from Newtable where DateTimeStamp >= (SELECT DATEADD(day, -1, GETDATE()))"
于 2021-03-12T06:00:16.610 回答