我有一个T-SQL脚本,执行后会显示以下内容:
(1 row(s) affected)
(1 row(s) affected)
我认为这意味着2行,每行都在不同的表中受影响。
有没有一种方法可以使SQL Server也显示受影响的表的名称? (除了研究SQL脚本之外?
答案 0 :(得分:0)
我想不出一种简单的方法,例如让SQL自动显示表名 但是您可以使用“使用情况统计信息”来实现。 尝试以下代码:
SELECT OBJECT_NAME(ius.object_id) as TableName,
si.name as IndexName,
si.index_id as IndexID,
ius.user_seeks,
ius.user_scans,
ius.user_lookups,
ius.user_updates,
ius.last_user_seek,
ius.last_user_scan,
ius.last_user_lookup,
ius.last_user_update
FROM sys.dm_db_index_usage_stats ius
JOIN sys.indexes si
ON ius.object_id = si.object_id
AND ius.index_id = si.index_id
WHERE database_id = db_id()
答案 1 :(得分:0)
有一种方法可以通过将统计信息设置为“开”来获取受影响的行和表:
SET STATISTICS TIME ON;
SET STATISTICS IO ON;
将显示以下内容:
SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms. SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 0 ms. Table 'Your_Table'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms.
(3 row(s) affected) SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 0 ms. Table 'Your_Table'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
因此,它不仅向您显示受影响的表,而且还向您显示系统处理每个子查询所花费的时间。
当您不再需要它们时,请运行:
SET STATISTICS TIME OFF;
SET STATISTICS IO OFF;
答案 2 :(得分:-2)
据我所知,SQL无法为您显示表,但可以显示正在插入,更新(旧值和新值)和DELETED的行的内容,但是它可能太多了。 例如:
UPDATE Products
SET ProductName = 'Pears'
OUTPUT Deleted.ProductID, Deleted.ProductName AS OldProductName, Inserted.ProductName AS NewProductName
WHERE ProductName = 'Peras';
显示的内容: 产品ID旧产品名称新产品名称 1个梨子
如果对INSERT使用相同的逻辑,则不会看到虚构的DELETED表,而如果使用DELETE,则不会看到INSERTED表。