SQL2005以后的查询主键约束SQL

select s.name as TABLE_SCHEMA, t.name as TABLE_NAME, k.name as CONSTRAINT_NAME, c.name as COLUMN_NAME, ic.key_ordinal AS ORDINAL_POSITION
from sys.key_constraints as k
join sys.tables as t on t.object_id = k.parent_object_id
join sys.schemas as s on s.schema_id = t.schema_id
join sys.index_columns as ic on ic.object_id = t.object_id and ic.index_id = k.unique_index_id
join sys.columns as c on c.object_id = t.object_id and c.column_id = ic.column_id
where k.type = ‘PK’ and s.name=’fdp_bfm’
order by 1,2,3

已有1条评论

  1. CREATE TABLE #counts(table_name varchar(255),row_count int);
    EXEC sp_MSForEachTable @command1=’INSERT #counts (table_name, row_count) SELECT ”?”, COUNT(*) FROM ?';
    SELECT * FROM #counts order by 1;

    再发一个可以查询数据库里面,每个表数据记录条数的SQL, 估计很少人用到这个函数

发表评论

电子邮件地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>