這是我的全部程序:如何解決在Microsoft SQL中使用xp_cmdshell時出現的「Access is denied」錯誤?
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語句被添加,文件名稱返回爲「訪問被拒絕」。
任何幫助,將不勝感激!
我不是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.
我也走進了小平面>外圍應用配置,並確保xp_cmdshell的啓用/允許(true)。在Facets> Server Security下也已經標記爲true。
是否可以在#tmp中有記錄,其中eFileName是一個空字符串?我會改變它在「While(Select Count(*)From #tmp where isnull(eFileName,'')<>'')> 0」 – GuidoG
@GuidoG我試着用你提到的替換while語句。不幸的是,我仍然收到錯誤。 –
如果只運行EXEC xp_cmdshell'dir/B C:\ Users \ ***** \ Desktop \ Test_Images',返回什麼? ?請記住,此路徑與SQL服務器相關,而不是您從中運行的客戶端。 –