Oracle:
@"select col.column_name , con.table_name
from all_constraints con, all_cons_columns colwhere con.constraint_name = col.constraint_name
and con.constraint_type='P'
and upper(con.table_name) ='" + strTableName.ToUpper() + "'"
SqlServer:
@"select c.name,o.name
from sysindexes i
join sysindexkeys k on i.id = k.id and i.indid = k.indid
join sysobjects o on i.id = o.id
join syscolumns c on i.id=c.id and k.colid = c.colid
where o.xtype = 'U'
and exists(select 1 from sysobjects where xtype = 'PK' and name = i.name)
and o.name='" + strTableName + "'
其中,strTableName 为表名。查询该表的主键列名,同时也返回表名。
在Oracle中SQL语句,表名大小写敏感,所以全都转换为大写了。
这些语句应该不是最佳方案,望抛砖引玉。