Sql server SQL Server中的动态SQL-如何使用?
我的问题是关于动态SQL的。我有两张桌子Sql server SQL Server中的动态SQL-如何使用?,sql-server,dynamic-sql,Sql Server,Dynamic Sql,我的问题是关于动态SQL的。我有两张桌子 客户(客户ID、fn\u名称、Ln\u名称) 供应商(供应商id、客户id、订单号、订单日期) 我还有其他的桌子 表(表ID(主键),名称) table_col(colid(pk)、tableid(fk)、colname) 表键(键id、表id、键列名称) 现在,如果用户从table\u col表中选择任何列,我需要动态获取列名,并根据名称匹配从table\u键中动态标识联接 可能是这样的 select t.name , c.co
- 客户(客户ID、fn\u名称、Ln\u名称)
- 供应商(供应商id、客户id、订单号、订单日期)
- 表(表ID(主键),名称)
- table_col(colid(pk)、tableid(fk)、colname)
- 表键(键id、表id、键列名称)
现在,如果用户从
table\u col
表中选择任何列,我需要动态获取列名,并根据名称匹配从table\u键中动态标识联接 可能是这样的
select
t.name
, c.colname
, case k.key_col_name when not null then 'X' else '' end as iskey
from table_col as c
left join table_keys as k on k.key_col_name = c.colname
inner join table as t on t.tableid = c.tableid
where t.name in ('customers', 'vendors');
可能是这样的
select
t.name
, c.colname
, case k.key_col_name when not null then 'X' else '' end as iskey
from table_col as c
left join table_keys as k on k.key_col_name = c.colname
inner join table as t on t.tableid = c.tableid
where t.name in ('customers', 'vendors');