这是我的整个例程:
Declare @AttFileType as char(5), @HQCo as int, @FormName as Varchar(15), @KeyID as VarChar(10), @UniqueID as uniqueidentifier, @FilePath as Varchar(100), @StringCommand as Varchar(200)
Declare @AttID as int
DECLARE @cmd as VARCHAR(500)
DECLARE @cmd2 as VARCHAR(500)
CREATE TABLE #tmp(eFileName VARCHAR(100));
INSERT INTO #tmp
EXEC xp_cmdshell 'dir /B C:\Users\*****\Desktop\Test_Images';
Declare @FileName varchar(100)
Set @UniqueID = NewID()
While (Select Count(*) From #tmp where eFileName is not null) > 0
Begin
Select Top 1 @FileName = eFileName From #tmp
Set @FilePath = 'C:\Users\*****\Desktop\Test_Images\' + @FileName
Set @AttID = (Select TOP 1 AttachmentID FROM dbo.bHQAF ORDER BY AttachmentID DESC) + 1
Set @AttFileType = '.jpg'
Insert Into dbo.bHQAF (AttachmentID, AttachmentFileType)
Select @AttID, @AttFileType
SET @cmd = '
Declare @AttID2 as int, @AttFileType2 as char(5), @FilePath2 as Varchar(100)
Set @AttFileType2 = ''.jpg''
Set @AttID2 = (Select TOP 1 AttachmentID FROM dbo.bHQAF ORDER BY AttachmentID DESC)
Update dbo.bHQAF
Set AttachmentData = (SELECT * From OPENROWSET (Bulk ''' + @FilePath + ''', Single_Blob) rs)
Where AttachmentID = @AttID2 and AttachmentFileType = @AttFileType2'
Exec (@cmd)
Set @HQCo = 101
Set @FormName = 'HRCompAssets'
Set @KeyID = 'KeyID=2'
Insert Into dbo.bHQAT (HQCo, AttachmentID, FormName, KeyField, UniqueAttchID)
Select @HQCo, @AttID, @FormName, @KeyID, @UniqueID
Insert Into dbo.bHQAI (AttachmentID, HRCo)
Select @AttID, @HQCo
Update dbo.bHQAT
Set Description = 'TEST3', AddDate = GETDATE(), AddedBy = '****', DocAttchYN = 'N', DocName = 'Database', OrigFileName = @FileName, TableName = 'HRCA'
Where AttachmentID = @AttID and HQCo = @HQCo
Insert Into dbo.bHQAI (AttachmentID, HRCo)
Select @AttID, 101
Update dbo.bHRCA
Set UniqueAttchID = @UniqueID
Where HRCo = 101 and Asset = '00001'
Delete from #tmp Where eFileName = @FileName
End
我已经验证了代码的工作原理,用于将单个图像加载到服务器中,没有这一点:
-- Declarations here
CREATE TABLE #tmp(eFileName VARCHAR(100));
INSERT INTO #tmp
EXEC xp_cmdshell 'dir /B C:\Users\*****\Desktop\Test_Images';
While (Select Count(*) From #tmp where eFileName is not null) > 0
Begin
Select Top 1 @FileName = eFileName From #tmp
-- Rest of code here
Delete from #tmp Where eFileName = @FileName
End
但是一旦添加了while循环和xp_cmdshell语句,文件名就会返回为“Access is denied”。
任何帮助将不胜感激!
我不是SQL的专家,但我被要求将大约1000个PDF和JPEG文件加载到数据库中,并且脚本似乎是最合乎逻辑的方法。
完成所有操作后,我希望脚本从文件夹中获取每个图像并将其加载到数据库中。
如果有必要,我愿意使用不同的循环方法。
编辑: 我还尝试将以下内容添加到代码的开头,但没有解决问题:
--Allow for SQL to use cmd shell
EXEC sp_configure 'show advanced options', 1 -- To allow advanced options to be changed.
RECONFIGURE -- To update the currently configured value for advanced options.
EXEC sp_configure 'xp_cmdshell', 1 -- To enable the feature.
RECONFIGURE -- To update the currently configured value for this feature.
我也进入了Facets>表面区域配置并确保启用/允许xp_cmdshell(true)。它在Facets>下也已经标记为真。服务器安全。
答案 0 :(得分:4)
这里有几个可能的问题。
xp_cmdShell在服务器上运行。如果该计算机没有名为C:\Users\*****\Desktop\Test_Images
的文件夹,则无效。
xp_CmdShell使用service account运行。如果该帐户对目标文件夹没有权限,则会失败。
必须启用xp_CmdShell。来自MSDN。xp_cmdshell选项是SQL Server服务器配置选项 这使系统管理员能够控制是否xp_cmdshell 扩展存储过程可以在系统上执行。默认情况下 xp_cmdshell选项在新安装时禁用,可以启用 使用基于策略的管理或运行sp_configure 系统存储过程。
答案 1 :(得分:1)
我明白了!
谢谢@ destination-data& @GarethLyons提供所有帮助!
你们是对的,文件夹权限存在问题我没有意识到我必须进入该文件夹并手动更新权限以包含“服务”。一旦我这样做,一切都很完美!
再次感谢你们,对不起,感到困惑。
对于将来遇到此问题的其他人,请先执行以下操作:
1)转到文件夹
2)右键单击
3)选择属性
4)选择“安全”选项卡
5)单击“高级”
6)点击添加
7)点击选择原则
8)输入“服务”和检查姓名
9)选择“服务”,然后单击“确定”
10)在“基本权限”
下选择适当的权限11)选择“仅将这些权限应用于此容器中的对象”
12)应用更改并尝试再次运行xp_cmdshell