gpt4 book ai didi

sql-server - 在 Using SQL xp_create_subdir 中创建目录

转载 作者:行者123 更新时间:2023-12-04 02:01:18 25 4
gpt4 key购买 nike

CREATE PROCEDURE SPCheckDirectoryExists
(
@chkdirectory as nvarchar(4000)
)
AS
SET NOCOUNT ON
BEGIN
DECLARE @folder_exists as int
DECLARE @file_results table(file_exists int,file_is_a_directory int,parent_directory_exists int)

INSERT INTO @file_results
(file_exists, file_is_a_directory, parent_directory_exists)
EXEC MASTER.dbo.xp_fileexist @chkdirectory

SELECT @folder_exists = file_is_a_directory
FROM @file_results

--script to create directory
IF @folder_exists = 0
BEGIN
EXECUTE master.dbo.xp_create_subdir @chkdirectory
PRINT @chkdirectory + ' created on ' + @@servername
END
ELSE
PRINT 'Directory already exists'
END

通过使用上面的存储过程,
EXEC SPCheckDirectoryExists '\\SampleNetworkpath\Test\Test1'

它将在 Test Folder 中检查并创建 Test1 文件夹,

如果测试文件夹不存在于 \\SampleNetworkpath\Path它的抛出错误,

如何解决这个问题,
提前致谢,

最佳答案

您还需要检查该目录。像这样的事情,使用相同的逻辑......

CREATE PROCEDURE SPCheckDirectoryExists
(
@chkdirectory as nvarchar(4000)
)
AS
SET NOCOUNT ON
BEGIN
DECLARE @folder_exists as int
DECLARE @file_results table(file_exists int,file_is_a_directory int,parent_directory_exists int)

DECLARE @folder_results table(file_exists int,file_is_a_directory int,parent_directory_exists int)
DECLARE @chkdirectory2 nvarchar(4000) = reverse(right(reverse(@chkdirectory),len(@chkdirectory) - charindex('\',reverse(@chkdirectory))))
DECLARE @folder_exists2 int

INSERT INTO @file_results
(file_exists, file_is_a_directory, parent_directory_exists)
EXEC MASTER.dbo.xp_fileexist @chkdirectory

INSERT INTO @folder_results
(file_exists, file_is_a_directory, parent_directory_exists)
EXEC MASTER.dbo.xp_fileexist @chkdirectory2

SELECT @folder_exists = file_is_a_directory
FROM @file_results

SELECT @folder_exists2 = file_is_a_directory
FROM @folder_results

--script to create directory
IF @folder_exists = 0 and @folder_exists2 = 1
BEGIN
EXECUTE master.dbo.xp_create_subdir @chkdirectory
PRINT @chkdirectory + ' created on ' + @@servername
END
ELSE
PRINT 'Directory already exists or parent directory was invalid'
END

关于sql-server - 在 Using SQL xp_create_subdir 中创建目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47150459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com