如何执行内部联接,因为我想从另一个名为Medication的表中检索信息,其中包含属性描述而不是位置描述。我该怎么做呢?
public static void UpdateLocationDescription( int locationID, string description, SqlConnection connection, SqlTransaction transaction )
{
StringBuilder sqlString = new StringBuilder();
SqlCommand command;
sqlString.Append( "UPDATE [Location] SET " );
sqlString.Append( "description = @description " );
sqlString.Append( "WHERE locationID = @locationID " );
command = new SqlCommand( sqlString.ToString(), connection );
if( ( transaction != null ) ) command.Transaction = transaction;
command.Parameters.Add( "@locationID", SqlDbType.Int ).Value = locationID;
command.Parameters.Add( "@description", SqlDbType.VarChar ).Value = description;
int rowsAffected = command.ExecuteNonQuery();
if( !( rowsAffected == 1 ) )
{
throw new Exception( "An error has occurred while updating UpdateMedicationDispenseStatus." );
}
}
答案 0 :(得分:0)
使用“FROM”关键字可以做到这一点。
UPDATE [Medication]
SET [Medication].description = @description
FROM [Medication] INNER JOIN [Location] ON [Medication].LocationID = [Location].LocationID
WHERE ...
Ofcouse我不知道你的结构或要求。但是你把它写成一个带有SELECT语句的INNER JOIN。您可以使用要考虑的字段编写SELECT语句。然后用“UPDATE [tableyouwantochange] SET [field] = [value]
替换”SELECT [fields]“部分要检索数据,只需使用新的SELECT语句。
答案 1 :(得分:-1)
UPDATE [Location]
SET [Location].description = (SELECT top 1* m.description FROM Medication m WHERE m.LocationID =@LocationId)
WHERE Location.ID =@LocationId