Tsql 如何在特定表上使用sp_space?

Tsql 如何在特定表上使用sp_space?,tsql,sql-server-2008-r2,Tsql,Sql Server 2008 R2,我试图调用sp_spaceused来获取我想要监视的几个特定表中的行数,但我试图避免使用游标 我已经制作了一张桌子来容纳我想要监控的所有桌子: CREATE TABLE MonitoredTable ( MonitoredTableID INT PRIMARY KEY IDENTITY(1, 1) NOT NULL , DatabaseName NVARCHAR(128) NOT NULL , SchemaName NVARCHAR(128) NOT NULL , Tabl

我试图调用sp_spaceused来获取我想要监视的几个特定表中的行数,但我试图避免使用游标

我已经制作了一张桌子来容纳我想要监控的所有桌子:

CREATE TABLE MonitoredTable 
(
    MonitoredTableID INT PRIMARY KEY IDENTITY(1, 1) NOT NULL
  , DatabaseName NVARCHAR(128) NOT NULL
  , SchemaName NVARCHAR(128) NOT NULL
  , TableName NVARCHAR(128) NOT NULL
  , RowNumberThreshold INT NOT NULL
  , IsActive BIT NOT NULL
)
我的问题是:我想创建一个函数,该函数只为行数超过定义的RowNumberThreshold的表返回MonitoredTableId

下面是我想做的,但这是无效的SQL:

有没有一种方法可以在不使用光标的情况下检查MonitoredTable中的行数是否大于定义的RowNumberThreshold?

类似的内容

-- See the following pages for documentation on the tables used in this query:
--
-- sys.indexes              https://msdn.microsoft.com/en-us/library/ms173760.aspx
-- sys.partitions           https://msdn.microsoft.com/en-us/library/ms175012.aspx
-- sys.allocation_units     https://msdn.microsoft.com/en-us/library/ms189792.aspx
-- sys.tables               Only columns inherited from sys.object, see link below
-- sys.object               https://msdn.microsoft.com/en-us/library/ms190324.aspx
SELECT OBJECT_NAME(i.OBJECT_ID) AS [TableName]
     , p.[rows] AS [Num_Rows]
FROM sys.indexes AS i
    INNER JOIN sys.partitions AS p 
        ON p.OBJECT_ID = i.OBJECT_ID 
       AND p.index_id = i.index_id
    INNER JOIN sys.allocation_units AS a 
        ON a.container_id = p.partition_id
    INNER JOIN sys.tables AS t 
        ON i.OBJECT_ID = t.OBJECT_ID
WHERE i.type <= 1 -- Heap or clustered index
  AND a.type = 1 -- In-row data
  AND t.type = 'U' -- User-defined table
  AND t.is_ms_shipped = 0 -- sys.object was not created by an internal SQL Server component

这比我需要的稍多,我只需要sp_spaceused存储过程中的行数。我已经编辑了你的答案以匹配我使用的答案。当您有机会确保我的笔记有意义并且我正确使用了查询时,请快速查看一下。谢谢你的回答!!我会在今天晚些时候或明天看一看。很高兴我能帮忙。
-- See the following pages for documentation on the tables used in this query:
--
-- sys.indexes              https://msdn.microsoft.com/en-us/library/ms173760.aspx
-- sys.partitions           https://msdn.microsoft.com/en-us/library/ms175012.aspx
-- sys.allocation_units     https://msdn.microsoft.com/en-us/library/ms189792.aspx
-- sys.tables               Only columns inherited from sys.object, see link below
-- sys.object               https://msdn.microsoft.com/en-us/library/ms190324.aspx
SELECT OBJECT_NAME(i.OBJECT_ID) AS [TableName]
     , p.[rows] AS [Num_Rows]
FROM sys.indexes AS i
    INNER JOIN sys.partitions AS p 
        ON p.OBJECT_ID = i.OBJECT_ID 
       AND p.index_id = i.index_id
    INNER JOIN sys.allocation_units AS a 
        ON a.container_id = p.partition_id
    INNER JOIN sys.tables AS t 
        ON i.OBJECT_ID = t.OBJECT_ID
WHERE i.type <= 1 -- Heap or clustered index
  AND a.type = 1 -- In-row data
  AND t.type = 'U' -- User-defined table
  AND t.is_ms_shipped = 0 -- sys.object was not created by an internal SQL Server component