[英]Select from table, name is stored in the field
How can I join some data from some table whose name is a field of the dataset? 如何从名称为数据集字段的某些表中加入某些数据?
Like this: 像这样:
SELECT *
FROM dataset
INNER JOIN dataset.table_name
ON dataset.param_id = (dataset.table_name).id_(dataset.table_name)
You will have to construct the select statement as a string using T-SQL. 您必须使用T-SQL将select语句构造为字符串。 Then, use the execute command to run it. 然后,使用execute命令运行它。 For example: 例如:
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)
Update 更新
This is the syntax for Oracle (quoted from Andrewst 's answer here ): 这是(从Andrewst的回答引用用于Oracle的语法在这里 ):
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 http://www.dbforums.com/oracle/718534-ms-sql-exec-equivalent.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.