我有以下代码检查物理文件计数以确认先前的进程已生成正确数量的文件(在不同的文件夹中)
set NOCOUNT ON
declare @DateOffset int
set @DateOffset=0
-- Create a table variable to store user data
DECLARE @myTable TABLE
(
docID INT IDENTITY(1,1),
docRef VARCHAR(10),
YPTMPID varchar(3),
saveDir VARCHAR(500),
totalLettersExpected int,
actualLetters int
);
insert @myTable SELECT docRef, YPTMPID,
saveDir=max(Save_Directory) + cast(YEAR(GETDATE()-@DateOffset) as varchar(4)) + '\' + datename(month, GETDATE()-@DateOffset) + '\'+SUBSTRING(CONVERT(CHAR(20), GETDATE()-@DateOffset, 101),4, 2) + '.' + LEFT(CONVERT(CHAR(20), GETDATE()-@DateOffset, 101), 2)
+ '.' + SUBSTRING(CONVERT(CHAR(20), GETDATE()-@DateOffset, 101),7, 4),
COUNT(*) as 'Total Letters', null
FROM [alpsMaster].[dbo].[uExtractMonitor]
group by docRef, YPTMPID
order by 1,2
-- Get the number of rows in the looping table
DECLARE @RowCount INT, @SQL nvarchar(4000), @LoopSQL nvarchar(4000), @Date varchar(20)
set @Date=rtrim(CONVERT( CHAR(12), getDate()-@DateOffset, 106)) --'29 Oct 2013'
SET @RowCount = (SELECT COUNT(docID) FROM @myTable)
-- Declare an iterator
DECLARE @I INT
-- Initialize the iterator
SET @I = 1
-- Loop through the rows of table @myTable, and for each docRef, check the file directory for the correct number of files
WHILE (@I <= @RowCount)
BEGIN
DECLARE @docRef VARCHAR(10), @saveDir VARCHAR(500), @TemplateID varchar(3), @letterCount int
DECLARE @statement nvarchar(200), @EQRecordCout int
-- Get the data from table and set to variables
SELECT @docRef = docref FROM @myTable WHERE docID = @I
SELECT @saveDir = saveDir FROM @myTable WHERE docID = @I
SELECT @TemplateID = YPTMPID FROM @myTable WHERE docID = @I
update @myTable set actualLetters = 0 where docRef=@docRef
create table #files (subdirectory varchar(100), depth int, [file] int)
insert into #files EXEC master.sys.xp_dirtree @saveDir,0,1;
-- *** PROBLEM HERE ***
set @statement= 'SELECT count(*) FROM BOCTEST.S653C36C.LIVEBOC_A.' + @docRef
print @statement
exec @EQRecordCout=sp_executesql @statement
print @EQRecordCout
select @letterCount = COUNT(*) from #files
print cast(@letterCount as char(3)) + ' files for ' + @docRef
drop table #files
update @myTable set actualLetters = @letterCount where docRef=@docRef-- and YPTMPID=@TemplateID
-- Increment the iterator
SET @I = @I + 1
END
select * from @myTable
set NOCOUNT OFF
在我的最终WHILE LOOP
内部我希望使用动态SQL计算iSeries远程服务器上每个文件中的记录数(在SQL字符串末尾的表名为docRef
为动态)
但即使它执行得很好,我进入变量 @EQRecordCout 的所有内容都是零。这是不正确的,虽然有些表是空的,但大多数都有记录
写这个以返回值的正确方法是什么?
set @statement= 'SELECT count(*) FROM BOCTEST.S653C36C.LIVEBOC_A.' + @docRef
print @statement
exec @EQRecordCout=sp_executesql @statement
print @EQRecordCout
感谢 菲利普
答案 0 :(得分:1)
答案 1 :(得分:1)
您可以将sp_executesql
与output
参数一起使用。请务必指定output
两次:
set @statement= 'SELECT @rc = count(*) FROM BOCTEST.S653C36C.LIVEBOC_A.' + @docRef
declare @rc int
exec sp_executesql @statement, N'@rc int output', @rc output;
答案 2 :(得分:0)
我在Microsoft Technet: sp_executesql (Transact-SQL)
MS Technet Library 找到了答案这是有效的WHILE LOOP
代码:
WHILE (@I <= @RowCount)
BEGIN
DECLARE @docRef VARCHAR(10), @saveDir VARCHAR(500), @TemplateID varchar(3), @letterCount int
DECLARE @statement nvarchar(200), @EQRecordCount int
DECLARE @ParmDefinition nvarchar(500);
-- Get the data from table and set to variables
SELECT @docRef = docref FROM @myTable WHERE docID = @I
SELECT @saveDir = saveDir FROM @myTable WHERE docID = @I
SELECT @TemplateID = YPTMPID FROM @myTable WHERE docID = @I
update @myTable set actualLetters = 0 where docRef=@docRef
create table #files (subdirectory varchar(100), depth int, [file] int)
insert into #files EXEC master.sys.xp_dirtree @saveDir,0,1;
SET @ParmDefinition = N'@EQRecordCountOUT int OUTPUT';
set @statement= 'SELECT @EQRecordCountOUT=count(*) FROM BOCTEST.S653C36C.LIVEBOC_A.' + @docRef
--print @statement
execute sp_executesql @statement,@ParmDefinition, @EQRecordCountOUT=@EQRecordCount OUTPUT;
print @EQRecordCount
select @letterCount = COUNT(*) from #files
drop table #files
update @myTable set actualLetters = @letterCount, EQRecordCount=@EQRecordCount where docRef=@docRef-- and YPTMPID=@TemplateID
-- Increment the iterator
SET @I = @I + 1
END
select * from @myTable
所以有必要传入要填充到变量中的OUTPUT参数。