1
我創建了一個存儲過程,該存儲過程需要一個UNC路徑,根據日期追加一個子文件夾字符串,然後執行xp_cmdshell 'Dir '
來檢查目錄中有多少個文件。存儲過程不返回值
不幸的是,它似乎沒有返回一個值給我叫做sql。
這裏是存儲過程:
create procedure getDirFileCount (
@sUNCPath Varchar(500),
@countFiles int OUTPUT
)
AS
BEGIN
declare @files table (ID int IDENTITY, FileName varchar(100))
declare @saveDir varchar(250), @Cmd varchar(500)
declare @Count int
-- get the correct path for today's generated files in format \Year\Month\dd.mm.yyyy...
set @saveDir [email protected]
+ cast(YEAR(GETDATE()) as varchar(4))
+ '\' + datename(month, GETDATE())
+ '\'+SUBSTRING(CONVERT(CHAR(20), GETDATE(), 101),4, 2)
+ '.' + LEFT(CONVERT(CHAR(20), GETDATE(), 101), 2)
+ '.' + SUBSTRING(CONVERT(CHAR(20), GETDATE(), 101),7, 4)
-- using quoted identifier for the long string UNC path
set @Cmd= 'dir "' + @saveDir + '" /b'
insert into @files execute xp_cmdshell @cmd
select @Count= COUNT(*) from @files
SET @[email protected]
RETURN @countFiles
--print @countFiles
END
這裏是我使用調用存儲過程的代碼:
declare @DaysOffset int=0, @I int =0
DECLARE @tmpLetterTbl TABLE (
[docID] [int] IDENTITY(1,1) NOT NULL,
[docRef] [varchar](50) NULL,
[saveDir] [varchar](4000) NULL,
[totalLettersExpected] [int] NULL,
[actualLetters] [int] NULL,
[EQRecordCount] [int] NULL
)
insert @tmpLetterTbl SELECT distinct docRef,
saveDir=max(Save_Directory),
COUNT(*) as 'Total Letters', null, null
FROM [alpsMaster].[dbo].[uExtractMonitor]
where YPTMPID<>0
group by docRef
order by 1,2
-- Get the number of rows in the looping table
DECLARE @RowCount INT,@Date varchar(20)
set @Date=rtrim(CONVERT(CHAR(12), getDate()[email protected], 106)) --'29 Oct 2013'
SET @RowCount = (SELECT COUNT(docID) FROM @tmpLetterTbl)
WHILE (@I <= @RowCount)
BEGIN
DECLARE @docRef VARCHAR(10)
DECLARE @saveDir VARCHAR(500)
DECLARE @letterCount int
-- Get the data from table and set to variables
SELECT @docRef = docref FROM @tmpLetterTbl WHERE docID = @I
SELECT @saveDir = saveDir FROM @tmpLetterTbl WHERE docID = @I
update @tmpLetterTbl set actualLetters = 0 where [email protected]
exec getDirFileCount @SaveDir, @letterCount
print cast(@letterCount as varchar(3)) + ' letters in directory: ' + @saveDir
update @tmpLetterTbl set actualLetters = @letterCount where docID = @I
set @[email protected]+1
END
我想表變量@tmpLetterTbl與數量進行更新由stroed proc返回的目錄中的文件getDirFileCount
我沒有得到任何錯誤,但是@letterCount是沒有被填充。
我在這裏做錯了什麼?
感謝 菲利普
它看起來像變量@saveDir VARCHAR(250)在存儲過程getDirFileCount,可能太小。您正在追加一個比varchar(250)更長的字符串。 –