2

如何执行内部联接,因为我想从另一个名为 Medication 的表中检索信息,该表的属性为 Description 而不是 Location description。我该怎么做呢?

 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." );
        }
    }
4

2 回答 2

0

使用“FROM”关键字可以做到这一点。

UPDATE [Medication]

SET [Medication].description = @description

FROM [Medication] INNER JOIN [Location] ON [Medication].LocationID = [Location].LocationID

WHERE ...

Ofcouse 我不知道你的结构或要求。但是您将其编写为带有 SELECT 语句的 INNER JOIN。您可以使用要考虑的字段编写 SELECT 语句。然后只需将“SELECT [fields]”部分替换为“UPDATE [tableyouwantochange] SET [field]=[value]

要检索数据,只需使用新的 SELECT 语句。

于 2013-07-29T14:28:17.617 回答
-1

你的意思是这样吗?

UPDATE [Location]

SET [Location].description = (SELECT top 1* m.description FROM Medication m WHERE m.LocationID =@LocationId)

WHERE Location.ID =@LocationId
于 2013-07-29T14:53:25.813 回答