找不到列或用户定义的函数或聚合,或者名称不明确
cannot find column or the user-defined function or aggregate, or the name is ambigous
我在尝试 INNER JOIN 2 tables 时遇到此错误。
"cannot find column "TE" 或用户自定义函数或聚合 "TE.UserID",或名称有歧义"
有什么想法吗?
我试过将 table 名称添加到 Select 语句中,如下所示。实际上 运行 SQL 服务器中的查询。但它对 Python ODBC 没有任何作用。
"Select TP.UserID, Phone "
但这并没有帮助。
userid = 'dd88'
try:
connection = pypyodbc.connect('Driver={SQL Server};Server=MyServerName;Database=MyDatabaseName;Trusted_Connection=yes;')
except pypyodbc.Error as ex:
sqlstate = ex.args[0]
if sqlstate == '28000':
print("You do not have access.")
cursor = connection.cursor()
SQLCommand = ("SELECT UserID, Phone "
"FROM dbo.SEC_UserProfile as TP INNER JOIN dbo.SEC_User as TE "
"ON TP.UserID = TE.UserID "
"(nolock)"
"WHERE UserID LIKE ?")
Values = ['%' + userid + '%']
cursor.execute(SQLCommand,Values)
results = cursor.fetchone()
if results:
print(connections + " " + str(results[0]) + " " + str(results[1])) # enters results in entry
connection.close()
else:
print(connections + " - NO")
connection.close()
UserID 在两个 table 中。
您需要通过在 select 和 where 子句中定义您需要来自哪个 table 的 UserID 来更改您的 sql 语句,例如:
SQLCommand = ("SELECT TP.UserID, Phone "
"FROM dbo.SEC_UserProfile as TP (nolock) INNER JOIN dbo.SEC_User as TE "
"ON TP.UserID = TE.UserID "
"WHERE TP.UserID LIKE ?")
@TabAlleman 说的对,(nolock) 的地方不对。更新了我的答案。
好吧,一个问题是你把 (nolock)
放错了地方。它应该紧跟在 table 别名之后,而不是连接条件:
此外,您在构建此 SQLCommand 字符串时不需要添加连接运算符吗?我从未使用过 Python,但如果这是 .net 代码,它甚至无法编译。
SQLCommand = ("SELECT TP.UserID, Phone " +
"FROM dbo.SEC_UserProfile as TP (nolock) INNER JOIN dbo.SEC_User as TE (nolock)" +
"ON TP.UserID = TE.UserID " +
"WHERE TP.UserID LIKE ?")
我在尝试 INNER JOIN 2 tables 时遇到此错误。
"cannot find column "TE" 或用户自定义函数或聚合 "TE.UserID",或名称有歧义"
有什么想法吗?
我试过将 table 名称添加到 Select 语句中,如下所示。实际上 运行 SQL 服务器中的查询。但它对 Python ODBC 没有任何作用。
"Select TP.UserID, Phone "
但这并没有帮助。
userid = 'dd88'
try:
connection = pypyodbc.connect('Driver={SQL Server};Server=MyServerName;Database=MyDatabaseName;Trusted_Connection=yes;')
except pypyodbc.Error as ex:
sqlstate = ex.args[0]
if sqlstate == '28000':
print("You do not have access.")
cursor = connection.cursor()
SQLCommand = ("SELECT UserID, Phone "
"FROM dbo.SEC_UserProfile as TP INNER JOIN dbo.SEC_User as TE "
"ON TP.UserID = TE.UserID "
"(nolock)"
"WHERE UserID LIKE ?")
Values = ['%' + userid + '%']
cursor.execute(SQLCommand,Values)
results = cursor.fetchone()
if results:
print(connections + " " + str(results[0]) + " " + str(results[1])) # enters results in entry
connection.close()
else:
print(connections + " - NO")
connection.close()
UserID 在两个 table 中。
您需要通过在 select 和 where 子句中定义您需要来自哪个 table 的 UserID 来更改您的 sql 语句,例如:
SQLCommand = ("SELECT TP.UserID, Phone "
"FROM dbo.SEC_UserProfile as TP (nolock) INNER JOIN dbo.SEC_User as TE "
"ON TP.UserID = TE.UserID "
"WHERE TP.UserID LIKE ?")
@TabAlleman 说的对,(nolock) 的地方不对。更新了我的答案。
好吧,一个问题是你把 (nolock)
放错了地方。它应该紧跟在 table 别名之后,而不是连接条件:
此外,您在构建此 SQLCommand 字符串时不需要添加连接运算符吗?我从未使用过 Python,但如果这是 .net 代码,它甚至无法编译。
SQLCommand = ("SELECT TP.UserID, Phone " +
"FROM dbo.SEC_UserProfile as TP (nolock) INNER JOIN dbo.SEC_User as TE (nolock)" +
"ON TP.UserID = TE.UserID " +
"WHERE TP.UserID LIKE ?")