Is it possible to Update the rows with data from a table connected with the foreign key?
I have a table [Book]
in 开发者_如何学编程a database that has a [AuthorId]
and [Description]
columns. The other table [Author]
is related to the [Book]
via foreign key [Book].[AuthorId] -> [Author].[ID]
.
I'd like to update the [Book].[Description]
column with the text "Nice book from #Author#"
, where #Author#
is the name of the author taken from the [Author].[Name]
.
Something like this:
UPDATE [Book] SET [Description] = 'Nice book from ' + [Author].[Name]
but the problem is that I don't know if there is a way to join the author and the book from within the UPDATE
statement, so that each updated row will know it's author's name.
Is this possible in a single SQL query?
update Book
set Description = 'Nice book from '+Author.Name
from Author
where Book.AuthorID = Author.AuthorID
UPDATE [Book] SET [Description] = 'Nice book from ' + [Book].[Name]
FROM [Book], [Author]
Where [Book].AuthorId = [Author].Id
精彩评论