检查文件是否存在 - 在SQL Server 2005中 [英] Check whether file exists - in SQL Server 2005
问题描述
我试图写一个SQL用户定义的函数来检查传递给它的文件路径是否存在。
Hi,
I was trying to write a SQL user defined function to check whether a file path passed to it exists or not.
CREATE FUNCTION [dbo].[IsExists](@FilePath nvarchar(MAX))
RETURNS int
AS
BEGIN
DECLARE @File_Exists int;
EXEC Master.dbo.xp_fileexist @FilePath, @File_Exists OUT;
RETURN ISNULL(@File_Exists, 0);
END
无论文件是否存在,此函数始终返回零。任何人都可以帮我找到我错的地方! :(
现在,在检查这个时,我写了下面给出的脚本(代码片段)
This function is always returning Zero, regardless of whether the file exists or not. Could anyone help me to find where I went wrong! :(
Now, while checking this, I wrote the below given script (code snippet)
DECLARE @FilePath nvarchar(MAX);
SET @FilePath='C:\temp\ServiceLog.txt';
CREATE TABLE #FileExists (isExists int, isDirectory int, isParentDirExists int);
INSERT INTO #FileExists (isExists, isDirectory, isParentDirExists)
EXEC Master.dbo.xp_fileexist @FilePath;
DROP TABLE #FileExists;
,它抛出错误!
消息0,等级11,状态0,行0
当前命令发生严重错误。结果,如果有的话,应该被丢弃。任何线索... :(
问候
Sangeeth
and it is throwing error!
Msg 0, Level 11, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded. Any clue... :(
Regards
Sangeeth
推荐答案
它在我的位置正常工作
It works fine at my place
CREATE FUNCTION [dbo].[IsExists](@FilePath nvarchar(255))
RETURNS int
AS
BEGIN
DECLARE @File_Exists INT;
EXEC Master.dbo.xp_fileexist @FilePath, @File_Exists OUT;
RETURN @File_Exists
END
GO
DECLARE @FilePath VARCHAR(255)= 'F:\data.txt'
SELECT [dbo].[IsExists] (@FilePath) AS FileExists
GO
FileExists
1
不使用功能运行
FileExists
1
Running without using function
DECLARE @FilePath VARCHAR(255)= 'F:\data.txt'
CREATE TABLE #FileExists (isExists int, isDirectory int, isParentDirExists int);
INSERT INTO #FileExists (isExists, isDirectory, isParentDirExists)
EXEC Master.dbo.xp_fileexist @FilePath;
SELECT * FROM #FileExists
DROP TABLE #FileExists;
isExists isDirectory isParentDirExists
1 0 1
我刚刚改变了Nvarchar( Max)到Nvarchar(255)
I Just have changed Nvarchar(Max) to Nvarchar(255)
这篇关于检查文件是否存在 - 在SQL Server 2005中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!