简体   繁体   中英

Multi-part identifier could not be bound (SQL SERVER)

I am programming in Lua programming language and using SQL along with it.

In my program , I have a select statement that is fetching userid from from a table named "users" and storing it in a variable called "U".

This is the statement :

local U = conn:query{live=true,sql="select top 1 userid from users where username = 'iguana01'"}

Now, since the top 1 userid is stored in U , I want that to us in my next statement which is

"UPDATE dbo.Messages SET Status='R' , ModifyDate = GetUTCDate(), ModifiedById = U.userid WHERE MessageId ='10'"

but if I am using U.userid, It says "Multipart identifier could not be bound".Secondly, this error is there because I am trying to use U.userId which is data from users table in the ModifiesById field of Messages table but I do not have any common column between these tables to make a join. My only requirement is to use top 1 value of UserId column of Users table in ModifiedById column of Message Table . How can I fix this ?

I believe the error is there because U.userid is not known to SQL query engine: U is a local variable, which the engine does not have access to. Try changing the second query to concatenate the value of U.userid into the string:

"UPDATE dbo.Messages SET Status='R' , ModifyDate = GetUTCDate(), ModifiedById = " .. U.userid .. " WHERE MessageId ='10'"

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