简体   繁体   中英

Server-specific error: The multi-part identifier could not be bound

I am running some scripts on identical databases on different servers.

While the script runs successfully on most of them, on some I am getting this error:

UPDATE DB.T1
SET SONUM = 0
WHERE SONUM <> 0
    AND NOT EXISTS (
        SELECT SERVICEORDER
        FROM DB.T2
        WHERE SERVICEORDER = T1.SONUM
        )

The multi-part identifier T1.SONUM could not be bound

My question is, why it is running successfully on some servers but not all? and how can I fix it?

Can I replace the query above with this one?:

UPDATE DB.T1
SET SONUM = 0
WHERE SONUM NOT IN (
        SELECT SERVICEORDER
        FROM DB.T2
        )
    AND SONUM <> 0

The reason you might be getting the error is because of inconsistencies between the column names between your tables. The column "SONUM" needs to exist in every table you're running this on.

After checking the table schema, if you decide you still want to make these updates. The following query should do the same thing as you seem to want without a subquery.

It will Update the SONUM of DB.T1 to be 0 for each record that is not currently 0 and has no associated SERVICEORDER's from DB.T2.

First it would be a good idea to preview the columns you're going to update with a select statement. The update statement to actually change them will follow.

SELECT DB.T1.SONUM
    , DB.T2.SERVICEORDER
FROM DB.T1
    LEFT JOIN DB.T2
        ON DB.T1.SONUM = DB.T2.SERVICEORDER
WHERE DB.T2.SERVICEORDER IS NULL
AND DB.T1.SONUM <> 0

And Update...

UPDATE DB.T1
SET SONUM = 0
FROM DB.T1
    LEFT JOIN DB.T2
        ON DB.T1.SONUM = DB.T2.SERVICEORDER
WHERE DB.T2.SERVICEORDER IS NULL
AND DB.T1.SONUM <> 0

Give your table an alias. It seems like T1 is in schema DB , the EXISTS is probably looking in the default DBO .

Try this:

UPDATE T
SET    SONUM = 0
FROM   DB.T1 T
WHERE  SONUM <> 0
AND NOT EXISTS (SELECT SERVICEORDER
                FROM   DB.T2
                WHERE  SERVICEORDER = T.SONUM)

Note: And of course, convert this to a select and make sure you are getting the correct results before updating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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