0
我想知道爲什麼變量在「if語句」內的子查詢中傳遞時被區別對待,但在獨立執行時工作正常。在db_id()中傳遞變量
成功的一個
select db_id((Select name from sys.databases where database_id > 4))
結果:
DBSIZE
--------
5
當它返回null:
Declare @dbcount int;
set @dbcount = (Select count(*) from sys.databases where database_id > 4);
If @dbcount = 1
Begin
select usedspace as 'DBSize'
from
(
SELECT (s.size * CONVERT(float,8))/1024 AS [Size],
(CAST(CASE s.type WHEN 2 THEN 0 ELSE CAST(FILEPROPERTY(s.name, 'SpaceUsed') AS float)* CONVERT(float,8) END AS float))/1024 AS [UsedSpace],
s.file_id AS [ID]
FROM
sys.filegroups AS g
INNER JOIN sys.master_files AS s ON ((s.type = 2 or s.type = 0) and s.database_id = db_id((Select name from sys.databases where database_id > 4)) and (s.drop_lsn IS NULL)) AND (s.data_space_id=g.data_space_id)
) DBFileSizeInfo
End
else
Print 'The instance has more than 1 database'
結果:
DBSize
---------
NULL
我有點朝着那個方向走去。謝謝你的幫助 –