我有数据库A,其中载有一个表格(核心表格),其中在数据库B中储存了本组织用户向数据库B发送数据的活跃表格清单。
我希望能够有一个基于固定的查询,能够输出一份清单,其中仅列出核心表格中含有数据的那些表格。
我通常会做一些诸如:
For each row in CoreTables Get the table name If table is empty Do nothing Else Print table name
如果没有光标或其他动态方法,有办法做到这一点吗?谢谢任何帮助...
我有数据库A,其中载有一个表格(核心表格),其中在数据库B中储存了本组织用户向数据库B发送数据的活跃表格清单。
我希望能够有一个基于固定的查询,能够输出一份清单,其中仅列出核心表格中含有数据的那些表格。
我通常会做一些诸如:
For each row in CoreTables Get the table name If table is empty Do nothing Else Print table name
如果没有光标或其他动态方法,有办法做到这一点吗?谢谢任何帮助...
也许最有效的选择是:
SELECT c.name
FROM dbo.CoreTables AS c
WHERE EXISTS
(
SELECT 1
FROM sys.partitions
WHERE index_id IN (0,1)
AND rows > 0
AND [object_id] = OBJECT_ID(c.name)
);
只需指出的是,由于飞行中交易,在sys.sysindexs、sys.sys.parties和sys.dm_db_parties_stats中的计数不能保证完全同步。
当您可以在数据库中运行此查询时, 您可以对以下不同的数据库运行此查询( 假设Core Tables不包括名称中的图案 ):
SELECT c.name
FROM DatabaseA.CoreTables AS c
WHERE EXISTS
(
SELECT 1
FROM DatabaseB.sys.partitions AS p
INNER JOIN DatabaseB.sys.tables AS t
ON p.[object_id] = t.object_id
WHERE t.name = c.name
AND p.rows > 0
);
如果您需要为多个数据库这样做,这些数据库都包含相同的模型(或至少是重叠的模型),而这些模型是在核心表格表格中汇总的,您可能需要构建一个视图,例如:
CREATE VIEW dbo.CoreTableCounts
AS
SELECT db = DatabaseB , t.name, MAX(p.rows)
FROM DatabaseB.sys.partitions AS p
INNER JOIN DatabaseB.sys.tables AS t
ON p.[object_id] = t.[object_id]
INNER JOIN DatabaseA.dbo.CoreTables AS ct
ON t.name = ct.name
WHERE p.index_id IN (0,1)
GROUP BY t.name
UNION ALL
SELECT db = DatabaseC , t.name, rows = MAX(p.rows)
FROM DatabaseC.sys.partitions AS p
INNER JOIN DatabaseC.sys.tables AS t
ON p.[object_id] = t.[object_id]
INNER JOIN DatabaseA.dbo.CoreTables AS ct
ON t.name = ct.name
WHERE p.index_id IN (0,1)
GROUP BY t.name
-- ...
GO
您的查询效率不高, 但不需要将硬代码数据库名称作为对象前缀, 代之以 :
SELECT name
FROM dbo.CoreTableCounts
WHERE db = DatabaseB
AND rows > 0;
如果执行时痛苦的话, 您可以为每个数据库创建视图 。
在 SQL 服务器中, 您可以做一些类似 :
SELECT o.name, st.row_count
FROM sys.dm_db_partition_stats st join
sys.objects o
on st.object_id = o.object_id
WHERE index_id < 2 and st.row_count > 0
顺便说一句,这具体不使用 OBJECT_ID () 或 OBJECT_NAME (), 因为它们在当前数据库中被评估。 上述代码继续使用三部分命名,为另一个数据库工作。 此版本还考虑到多个分区 :
SELECT o.name, sum(st.row_count)
FROM <dbname>.sys.dm_db_partition_stats st join
<dbname>.sys.objects o
on st.object_id = o.object_id
WHERE index_id < 2
group by o.name
having sum(st.row_count) > 0
像这样的东西吗?
//
foreach (System.Data.DataTable dt in yourDataSet.Tables)
{
if (dt.Rows.Count != 0) { PrintYourTableName(dt.TableName); }
}
//
这是你可以做到这一点的方法,它依靠系统表格,所以可以预警它不一定在今后的SQL版本中起作用。 牢记这一强烈的警告。
select distinct OBJECT_NAME(id) as tabName,rowcnt
from sys.sysindexes si
join sys.objects so on si.id=si.id
where indid=1 and so.type= U
您会添加到您感兴趣的表格和行当 & lt; 1 所在的条款中。
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 ...
I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域
We have a restaurant table that has lat-long data for each row. We need to write a query that performs a search to find all restaurants within the provided radius e.g. 1 mile, 5 miles etc. We have ...
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
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 ...
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 ...
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 ...
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: ...