1

以前に結合されたテーブルのデータから取得したテーブルに結合を作成する方法はありますか? たとえば、既存のテーブル名、それらのテーブルの表示フィールド、およびそれらのテーブルのメイン ID フィールドを含むオブジェクト テーブルがあります。オブジェクト テーブルだけでなく、オブジェクト テーブルで指定されたテーブルにも参加したいと考えています。

 select T.field1,T.field2,T.field3,O.DisplayName
 from tableT as T
 inner join objects as O on T.SystemObjectID = O.SystemObjectID
 inner join O.TableName as X on X.O.ID = T.SystemObjectRecordID

上記のスクリプトが正しくないことは理解しています。しかし、このタスクを達成するための最も簡単で効率的な方法は何でしょうか? 私が何を求めているのかが明確であることを願っています...

事前に助けてくれてありがとう。

4

1 に答える 1

1

You can't do that.

What you can do is take the results of that and create a string that is a SQL query.

You can even make that query parameterised and execute it using sp_executesql.
- That link has a few good examples to get you started.


This is because SQL is compiled to an execution plan before it is executed. At this point in time it needs to know the tables, decides the indexes to use, etc.

This means that data is data and sql is sql. You can't use late binding scripting tricks to modify the query that is already running.

You're stuck with SQL that writes SQL (Dynamic SQL) I'm afraid.

于 2012-06-22T13:46:51.730 に答える