0

SQL でいくつかの作業を行い、一時テーブルにデータを入力してから、NHibernate CreateSQLQuery を使用してその一時テーブルに JOIN し、最終結果を取得したいと考えています。私たちはNHibernateのバージョン1.2.0.4000を使用しており、同じセッションにいるにもかかわらず、後のクエリで一時テーブルにアクセスする際に問題があるようです(これは、私が同じSQLセッション/接続にいることを意味すると思います良い)。以下は私のコードの簡略版です

public void Work()
{
    SqlConnection connection = (SqlConnection)Session.Connection;

    SqlCommand command = new SqlCommand
                     {
                         CommandType = CommandType.Text,
                         CommandText = "SELECT ID = 1 INTO #TempTable",
                         Connection = connection,
                     };

    if ( Session.Transaction != null && Session.Transaction.IsActive )
    {
        Session.Transaction.Enlist( command );
    }

    command.ExecuteNonQuery();

    // Simplified example, I should have a temp table #TempTable with 1 row containing the values ID = 1

    // trying to fetch a list of Account objects where ID exists in #TempTable.
    // At this point, I get an error "Invalid object name '#TempTable'."
    IList<Account> accounts = Session.CreateSQLQuery(@"
        SELECT *
          FROM Account a
          JOIN #TempTable tt
            ON a.ID = tt.ID")
        .AddEntity("a", typeof(Account))
        .List<Account>();

    // Do some work on accounts list
}
4

1 に答える 1