1
UNC 경로를 사용하여 날짜를 기반으로 하위 폴더 문자열을 추가 한 다음 저장 한 proc를 만든 다음 xp_cmdshell 'Dir '
을 실행하여 디렉터리에있는 파일 수를 확인합니다.저장 프로 시저가 값을 반환하지 않음
불행히도 내 sql에 값을 반환하지 않는 것 같지 않습니다. 숫자로 업데이트 할 내가 원하는
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 :
여기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
내가 저장된 프로 시저를 호출하기 위해 사용하고있는 코드는 : 여기
는 저장 프로 시저입니다 스트로크 된 proc 파일에서 반환 된 파일 중getDirFileCount
오류가 발생하지 않지만 @letterCount은 채워지지 않습니다.
내가 뭘 잘못하고있어?
감사 필립
그것은 저장된 프로 시저의 getDirFileCount에서 변수 @saveDir의 VARCHAR (250)과 같은, 수도 너무 작다. varchar (250)보다 훨씬 긴 문자열이 추가 될 수 있습니다. –