5

如何从名称为数据集字段的表中加入一些数据?

像这样:

SELECT *
FROM dataset
INNER JOIN dataset.table_name 
ON dataset.param_id = (dataset.table_name).id_(dataset.table_name)
4

1 回答 1

3

您必须使用 T-SQL 将 select 语句构造为字符串。然后,使用执行命令运行它。例如:

DECLARE @sql VARCHAR(MAX);
DECLARE @table_name VARCHAR(100);
SET @table_name = (SELECT TOP 1 table_name FROM dataset)   ' TODO set criteria correctly here
SELECT @sql = 'SELECT * FROM dataset INNER JOIN ' & @table_name & ' ON dataset.param_id = ' & @table_name & '.id_' & @table_name & ';'
EXEC (@sql)

更新

这是 Oracle 的语法(引自Andrewst在此处的回答):

DECLARE
  TYPE rc_type REF CURSOR;
  rc rc_type;
  table_rec table%ROWTYPE;
BEGIN
  OPEN rc FOR 'select * from table';
  LOOP
    FETCH rc INTO table_rec;
    EXIT WHEN rc%NOTFOUND;
    -- Process this row, e.g.
    DBMS_OUTPUT.PUT_LINE( 'Name: '||table_rec.name );
  END LOOP;
END;

http://www.dbforums.com/oracle/718534-ms-sql-exec-equivalent.html

于 2012-05-14T00:14:53.863 回答