代码之家  ›  专栏  ›  技术社区  ›  Kade Williams

找不到列或用户定义的函数或聚合,或名称为ambigous

  •  1
  • Kade Williams  · 技术社区  · 7 年前

    我在尝试内部联接2个表时遇到此错误。

    找不到列“TE”或用户定义的函数或聚合“TE”。用户ID”,或名称为ambigous

    有什么想法吗?

    我尝试将表名添加到Select语句中,如下所示。实际上是在SQL Server中运行查询的。但它对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。

    2 回复  |  直到 7 年前
        1
  •  4
  •   Tab Alleman    7 年前

    有一个问题是 (nolock) 在错误的地方。它应该紧跟在表别名之后,而不是连接条件之后:

    在构建这个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 ?")
    
        2
  •  3
  •   Eray Balkanli    7 年前

    您需要通过定义select和where子句中需要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)的位置不正确。更新了我的答案。