我有一个复杂的存储过程,它查询特定的网络文件夹,收集它在那里找到的 SQL Server 备份文件的名称,然后处理每个文件以获取详细信息。如果我从 SSMS 版本 18.4 运行该过程,则会给出错误的结果。但如果我在 SSMS 版本 17.9 中运行它,该过程会为我提供我希望看到的数据。
我可以用一个更简单的例子来重现这个问题:
CREATE OR ALTER PROCEDURE TestOutput (
@HostName nvarchar(255),
@DbName nvarchar(255)
)
AS
BEGIN
Declare @cmd nvarchar(500);
Declare @basePath nvarchar(500) = '\\Server1\DB_Backups\SQL\' + @HostName + '\' + @DbName + '\';
Create table #FullFileList (backupPath nvarchar(255), backupFile NVARCHAR(255)) ;
SET @cmd = 'DIR /b "' + @basePath + 'Full\"';
INSERT INTO #FullFileList (backupFile) EXEC master.sys.xp_cmdshell @cmd;
update #FullFileList set backupPath = @basePath + 'Full\';
SET @cmd = 'DIR /b "' + @basePath + 'Diff\"';
INSERT INTO #FullFileList (backupFile) EXEC master.sys.xp_cmdshell @cmd
update #FullFileList set backupPath = @basePath + 'Diff\' where backupPath is null;
SET @cmd = 'DIR /b "' + @basePath + 'Log\"'
INSERT INTO #FullFileList (backupFile) EXEC master.sys.xp_cmdshell @cmd;
update #FullFileList set backupPath = @basePath + 'Log\' where backupPath is null;
select * from #FullFileList;
select count(*) from #FullFileList;
END
GO
如果我从 SSMS 版本 17.9 对包含三个子文件夹中的 1,950 个文件的父文件夹(基本路径)执行此过程,则输出为 1,953 行(列表中包含三个具有空文件名的行,每个文件夹一个)。计数输出为 1,953。
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Full\ Host_Name_DB_Name_FULL_20191230_040407.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Full\ Host_Name_DB_Name_FULL_20200106_040407.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Full\ Host_Name_DB_Name_FULL_20200113_040407.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Full\ NULL
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200103_050507.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200104_050506.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200105_050506.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200107_050506.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200108_050506.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200109_050506.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200110_050506.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200111_050507.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200112_050506.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200114_050506.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200115_050506.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ Host_Name_DB_Name_DIFF_20200116_050506.bak
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Diff\ NULL
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Log\ Host_Name_DB_Name_LOG_20200112_104703.trn
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Log\ Host_Name_DB_Name_LOG_20200112_105003.trn
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Log\ Host_Name_DB_Name_LOG_20200112_105302.trn
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Log\ Host_Name_DB_Name_LOG_20200112_105602.trn
[... Snip ...]
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Log\ Host_Name_DB_Name_LOG_20200116_144402.trn
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Log\ Host_Name_DB_Name_LOG_20200116_144703.trn
\\Server1\DB_Backups\SQL\Host_Name\DB_Name\Log\ NULL
1953
但是,如果我在 SSMS 版本 18.4 中执行相同的代码,则该过程会产生 9 行输出。它还告诉我计数是22 行,这也是错误的:刚刚给出了输出;只返回了九行。
\\wtrnas\DB_Backups\SQL\wtr320msdncm\DBMonitoring\Full\ WTR320MSDNCM_DBMonitoring_FULL_20191230_040407.bak
\\wtrnas\DB_Backups\SQL\wtr320msdncm\DBMonitoring\Full\ WTR320MSDNCM_DBMonitoring_FULL_20200106_040407.bak
\\wtrnas\DB_Backups\SQL\wtr320msdncm\DBMonitoring\Full\ WTR320MSDNCM_DBMonitoring_FULL_20200113_040407.bak
\\wtrnas\DB_Backups\SQL\wtr320msdncm\DBMonitoring\Full\ NULL
\\wtrnas\DB_Backups\SQL\wtr320msdncm\DBMonitoring\Log\ WTR320MSDNCM_DBMonitoring_LOG_20200112_104703.trn
\\wtrnas\DB_Backups\SQL\wtr320msdncm\DBMonitoring\Log\ WTR320MSDNCM_DBMonitoring_LOG_20200112_105003.trn
\\wtrnas\DB_Backups\SQL\wtr320msdncm\DBMonitoring\Log\ WTR320MSDNCM_DBMonitoring_LOG_20200112_105302.trn
\\wtrnas\DB_Backups\SQL\wtr320msdncm\DBMonitoring\Log\ WTR320MSDNCM_DBMonitoring_LOG_20200112_105602.trn
\\wtrnas\DB_Backups\SQL\wtr320msdncm\DBMonitoring\Log\ WTR320MSDNCM_DBMonitoring_LOG_20200112_105902.trn
22
(这是从 18.4 运行时返回的所有行)
同样,后端完全相同的代码、相同的数据库引擎(Windows Server 2012 R2 Standard 上的 Microsoft SQL Server 2017 CU18 Enterprise Edition)指向 Server1 上的相同文件夹结构。唯一的区别是执行 create procedure 命令的 SSMS 版本。
请注意,SQLCMD 也给出了正确的输出。
我已尝试卸载并重新安装 SSMS 18.4。我也尝试将其切换到非临时表。SSMS 18.4 版本创建 9 行数据,而不是 1953 行,然后我可以从程序外部查看这些数据。我尝试切换文件夹的处理顺序;这不会改变结果。
SQL Server 拥有浏览相关三个文件夹所需的所有权限;它实际上写了这些文件。
但是为什么 SSMS 的版本是一个因素呢?这一切都应该从服务器执行,这里没有改变。我怎样才能使这个程序在 18.4 中正常工作,或者为什么它不能正常工作?
我看到你的评论
您可以通过在 SSMS 中配置菜单选项来实现相同的目的: