[英]SQL Server: using table or @table in stored procedure
I have a stored procedure (see below) which inserts data into a physical table and then joins information with sys.databases. 我有一个存储过程(请参见下文),该过程将数据插入物理表中,然后将信息与sys.databases连接起来。 I was thinking that would it be better to not have a physical table for data insertion?
我当时在想,没有物理表进行数据插入会更好吗? Would it be better to fetch these results into a table variable within this procedure?
在此过程中将这些结果提取到表变量中会更好吗? If so, how to do that?
如果是这样,该怎么做?
CREATE PROCEDURE dbo.PROC_getDbInfo
AS
SET NOCOUNT ON
GO
TRUNCATE TABLE dbo.dbinfo
GO
EXECUTE sp_msforeachdb 'insert into dbo.dbinfo
select ''?'' as name,
type_desc,
physical_name,
state_desc,
size * 1.0/128 as size_in_mb,
max_size,
growth * 1.0/128 as growth_in_mb,
is_percent_growth,
is_read_only
from [?].sys.database_files'
GO
SELECT @@SERVERNAME as instance_name,
f.name,
d.create_date,
d.compatibility_level,
d.collation_name,
d.user_access_desc,
d.state_desc,
d.recovery_model_desc,
d.page_verify_option_desc,
d.log_reuse_wait_desc,
f.type_desc,
f.physical_name,
f.state_desc,
f.size_in_mb,
f.max_size,
f.growth_in_mb,
f.is_percent_growth,
f.is_read_only
FROM dbo.dbinfo AS f INNER JOIN
sys.databases AS d
ON f.name = d.name
ORDER BY f.name
GO
You'll have to use a table. 您必须使用表格。 Either global temp (##) or a normal table.
全局临时(##)或普通表。
A table variable will not be in scope for the sp_msforeachdb
call if declared for the stored proc, and not visible to the stored proc if declared in sp_msforeachdb
表变量不会在范围为
sp_msforeachdb
如果申报的存储过程,并在存储过程中不可见的呼叫如果在宣告sp_msforeachdb
@table更好-桌子很小,I / O成本会使它变慢。
Table variable usage is explained here: 表变量的用法在这里说明:
http://msdn.microsoft.com/en-us/library/ms175010.aspx http://msdn.microsoft.com/en-us/library/ms175010.aspx
It basically behaves like a table when it comes to how your script looks - but has very different behaviour under the hood, and if it's small enough should not result in any disc IO. 就脚本的外观而言,它的行为基本上像一张表-但是在幕后有非常不同的行为,并且如果它足够小,则不会导致任何磁盘IO。
Also, if the table is only used and then removed during the course of a procedure, this scope limitation becomes a argument for using it. 同样,如果仅在过程过程中使用该表,然后将该表删除,则该范围限制将成为使用该表的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.