Har*_*ngh 4 sql postgresql dynamic-sql
原始表的规则存储在一个名为md_formula的表中,该表用于映射到目标表中
放置/创建/插入以下内容raw_dbs_transactiondetailscash:
DROP TABLE raw_dbs_transactiondetailscash
CREATE TABLE raw_dbs_transactiondetailscash(
accountnumber VARCHAR(100),
referencecurrency VARCHAR(100),
transactiondate datetime)
INSERT INTO raw_dbs_transactiondetailscash(
accountnumber, referencecurrency, transactiondate)
SELECT 'XYZ','$','01/01/2016'
Run Code Online (Sandbox Code Playgroud)
放置/创建/插入以下内容md_formula:
DROP TABLE MD_Formula
CREATE TABLE MD_Formula (
Format VARCHAR(20),
tbl_Src VARCHAR(200),
Col_src VARCHAR(500),
tbl_Des VARCHAR(200),
Col_des VARCHAR(100),
Condition VARCHAR(500) )
INSERT INTO md_formula(format, tbl_src, Col_src, tbl_des,Col_des)
SELECT 'Dbs','raw_dbs_transactiondetailscash','accountnumber',
'normalized_transaction','account_number'
UNION ALL
SELECT 'Dbs','raw_dbs_transactiondetailscash','referencecurrency',
'normalized_transaction','currency'
UNION ALL
SELECT 'Dbs','raw_dbs_transactiondetailscash','transactiondate',
'normalized_transaction','trade_date'
Run Code Online (Sandbox Code Playgroud)
从存储在md_Formula中的原始表中获取数据TSQL(例如,仅选择一列)
这实际上会执行
SELECT accountnumber
FROM raw_dbs_transactiondetailscash
Run Code Online (Sandbox Code Playgroud)
并从raw_dbs_transactiondetailscash表中获取数据集
DECLARE @sql VARCHAR(100)
SELECT TOP 1 @sql= 'SELECT '+Col_src+ ' FROM '+tbl_Src FROM MD_Formula
EXEC (@sql)
Run Code Online (Sandbox Code Playgroud)
通过Postgres(仅准备好动态查询,如何从动态sql中的原始表中获取数据仍然是一个问题)
这需要执行
SELECT accountnumber,referencecurrency,transactiondate
FROM raw_dbs_transactiondetailscash
Run Code Online (Sandbox Code Playgroud)
并得到结果
SELECT 'SELECT '|| string_Agg(col_src,',') ||' FROM ' || tbl_src FROM md_formula
WHERE format='Dbs'
GROUP BY tbl_src
Run Code Online (Sandbox Code Playgroud)
对于动态查询,您需要使用“执行”命令。
EXECUTE dynamic-query-string INTO target-variable...
Run Code Online (Sandbox Code Playgroud)
手册页面在这里:http : //www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
高温超导