English 中文(简体)
如何从ql服务器主数据库中找到任何表格的栏目?
原标题:How to find columns count of any table in any database from sql server master database?

如果我知道数据库名称和表格名称,那么我如何能够从ql服务器主数据库中找到表格的栏目。

找到任何数据库中各栏最快的方法是什么?

你们想到的是这种问答的表现如何?

select count(*) from SYSCOLUMNS where id=(select id from SYSOBJECTS where name= Categories )

我需要支持2000年及以后的ql服务器。

最佳回答

视服务器的版本可能略有不同,但这项工作将在2005年进行:

SELECT
     COUNT(*)
FROM
     <database name>.sys.columns
WHERE
     object_id = OBJECT_ID( <database name>.<owner>.<table name> )

2000年:

SELECT
     COUNT(*)
FROM
     <database name>.sysobjects o
INNER JOIN <database name>.syscolumns c ON
     c.id = o.id
WHERE
     o.name =  <table name> 

如果你可能有多个表格,在不同的业主下有相同的确切表格名称,那么你需要说明。 我忘记了在座标上的一栏名,以 look头顶。

更新服务器和ANSI遵守情况:

SELECT COUNT(*)
FROM
    <database name>.INFORMATION_SCHEMA.COLUMNS
WHERE
    TABLE_SCHEMA =  <table schema>  AND
    TABLE_NAME =  <table name> 
问题回答

你可以(而且应当)这样做——试图避免使用“物体”的观点——它不再得到支持,可能在服务器2008R2或以后被拆除。

相反,在数据库中使用“sys”数据:

SELECT COUNT(*) 
FROM yourdatabase.sys.columns
WHERE object_id = OBJECT_ID( yourdatabase.dbo.tablename )

这样做应该trick,而且可能最容易和最快地这样做。

如何

select count(*) from <database name.information_schema.columns where table_name =  <table_name> 

你可以提出这样的问题。

 select count(*) from information_schema.columns where table_name= yourtablename 
select Object_name(object_id) as "Object Name", count(*) as "Column Count" from Course_Plannning_Expense.sys.columns
where Object_name(object_id) not like  sys% 
group by object_id
order by "Column Count" desc

或者,你可以把所有表格列在其栏目中。

SELECT COUNT(column_name) as "column_count", table_name FROM INFORMATION_SCHEMA.COLUMNS GROUP BY table_name ORDER BY "column_count" DESC





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签