SQL Image Extraction - sql

I'm trying to extract images from a restored database. The script below ostensibly runs fine: meaning it extracts the images and saves the number of files I expect to see (and the size I would expect to see) into a network folder as .pdf files. However, when I try to open the files this is the error message I receive:
Adobe Reader could not open 'filename.pdf' because it is either not a supported file type or because the file
has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
I have run this script before in other environments (2 to be exact) and it's worked fine.
I have verified that the actual images are good. You can access the files via the front end.
It doesn't appear that the values are encrypted. Although I'm not completely sure.
The Data Type natively is: image
I've been using SQL for a while but would consider myself a noob, whereas, I'm basically self-taught -so please go easy.
Any guidance / suggestions / insight will be very much welcome.
Thank you.
--========================================================================
USE <<ENTER_DATABASENAME>>
GO
sp_configure 'show advanced options', 1
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1
GO
RECONFIGURE
GO
DECLARE #FILEID VARCHAR(12),
#IMAGE VARBINARY(MAX),
#OBJSTREAM INT,
#FILENAME VARCHAR(MAX),
#BASEDIR VARCHAR(MAX)
SET #BASEDIR = '<<ENTER_NETWORKPATH>>'
PRINT 'BASEDIR: ' + #BASEDIR
DECLARE IMGEXTRACT CURSOR FAST_FORWARD FOR
select <<ENTER_COLUMNNAME01>>, <<ENTER_COLUMNNAME02>>
from <<ENTER_TABLENAME>>
OPEN IMGEXTRACT
FETCH NEXT FROM IMGEXTRACT INTO #FILEID, #IMAGE
WHILE ##FETCH_STATUS = 0
BEGIN
SET #FILENAME = #BASEDIR + '\' + #FILEID + '.pdf'
PRINT #FILENAME
EXEC sp_OACreate 'ADODB.Stream', #OBJSTREAM OUTPUT
EXEC sp_OASetProperty #OBJSTREAM, 'Type', 1
EXEC sp_OAMethod #OBJSTREAM, 'Open'
EXEC sp_OAMethod #OBJSTREAM, 'Write', NULL, #IMAGE
EXEC sp_OAMethod #OBJSTREAM, 'SaveToFile', NULL, #FILENAME, 2
EXEC sp_OAMethod #OBJSTREAM, 'Close'
EXEC sp_OADestroy #OBJSTREAM
FETCH NEXT FROM IMGEXTRACT INTO #FILEID, #IMAGE
END
CLOSE IMGEXTRACT
DEALLOCATE IMGEXTRACT
GO
--========================================================================

Related

Is it possible to get Response Headers via sp_oamethod?

I have a web service in SQL Server 2014. I'm using sp_oamethod for request to API. Now I'm getting response-code and response-body. And I need to get response header. Can anyone help me?
The following works for me to retrieve Rate-Limit-Remaining from the response header
DECLARE #responseHeader varchar(4000)
EXEC sp_OAMethod #res, 'getResponseHeader', #responseHeader OUT, 'Rate-Limit-Remaining'
PRINT 'Rate-Limit-Remaining: ' + coalesce(#responseHeader,'<null>')
EXEC sp_OAMethod #res, 'getResponseHeader', #responseHeader OUT, 'Rate-Limit-Reset'
PRINT 'Rate-Limit-Reset: ' + coalesce(#responseHeader,'<null>')
EXEC sp_OAMethod #res, 'getAllResponseHeaders', #responseHeader OUT
PRINT 'Response Headers: ' + coalesce(#responseHeader,'<null>')

Move text file from one folder to another folder using sql script

I have been using the following sql script to move file from one location to another .
EXEC
master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC
master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE
DECLARE #cmd nvarchar(500), #folderName varchar(100),#move varchar(100),#destinationpath varchar(50)
SET #folderName = 'Newfolder_' + REPLACE(CONVERT(varchar(10), GETDATE(), 101), '/', '')
SET #cmd = 'mkdir C:\FileDestination\' + #folderName
EXEC master..xp_cmdshell #cmd --- this will create folder(newfolder_mmddyyy)
set #destinationpath='C:\FileDestination\'+#folderName
set #move ='move c:\filesource\* '+ #destinationpath
exec master.dbo.xp_cmdshell #move ---this will move files to newly created folder
But it's showing the error as follows
The system cannot find the file specified.
Can any one let me know the solution for this. Thanks Alot for your help !
If "filesource" and "FileDestination" are example folder locations, then it could be that the variable for the move command is too small. Try making it the same as #cmd, i.e. #move nvarchar(500)
It seems to be the access issue on the filesource folder, try giving full access to everyone on the folder.
I tried the exact code on my system and it works fine for me (moved test file).

Getting 0x800C0005 System error in SQL Server

The following code is used to call a web service from SQL Server:
Declare #Object as Int;
Declare #ResponseText as VARCHAR(MAX);
Exec sp_OACreate 'MSXML2.XMLHTTP', #Object OUT;
Exec sp_OAMethod #Object, 'open', NULL, 'GET','http://localhost/Service1.asmx/HelloWorld?name=xyz', false;
Exec sp_OAMethod #Object, 'send';
EXEC sp_OAGetErrorInfo #Object
Create table #tmp(dt varchar(max))
insert into #tmp
exec sp_OAGetProperty #Object, 'ResponseText' --,#strLine OUtPUT
Select dt from #tmp -- single column/single row.
Drop Table #tmp -- clean up
Exec sp_OADestroy #Object;
But I am getting below error:
0x800C0005 msxml3.dll System error: -2146697211
Any pointers ...?
There are two (2) ways to fix Msxml3 Dll 0x800c0005 System Error Error:
Advanced Computer User Solution (manual update):
1) Start your computer and log on as an administrator.
2) Click the Start button then select All Programs, Accessories, System Tools, and then click System Restore.
3) In the new window, select "Restore my computer to an earlier time" option and then click Next.
4) Select the most recent system restore point from the "On this list, click a restore point" list, and then click Next.
5) Click Next on the confirmation window.
6) Restarts the computer when the restoration is finished.
Novice Computer User Solution (completely automated):
1) Download (Msxml3 Dll 0x800c0005 System Error) repair utility.
2) Install program and click Scan button.
3) Click the Fix Errors button when scan is completed.
4) Restart your computer.
More details

Call web service from stored procedure - special characters aren't shown correctly

I'm trying to call a web service from a stored procedure in SQL Server 2012:
DECLARE #ResponseText AS VARCHAR(8000)
DECLARE #Object AS INT;
DECLARE #idoc INT;
Exec sp_OACreate 'MSXML2.ServerXMLHTTP', #Object OUT;
Exec sp_OAMethod #Object, 'open', NULL, 'get', #Webservice, 'false'
Exec sp_OAMethod #Object, 'send'
Exec sp_OAMethod #Object, 'responseText', #ResponseText OUTPUT
Exec sp_OADestroy #Object
EXEC sp_xml_preparedocument #idoc OUTPUT, #ResponseText
The call works but the result string #ResponseText won't convert some special characters. An ë (with umlaut) turns into ? and the next character is cut of. The ë is also shown as ë in the source of the xml, so it isn't translated to ë
The < and &characters as are translated well (into < and &) and these give no problems.
The returning XML is from the <?xml version="1.0" encoding="ISO-8859-1"?> format and it displays correctly when I open the service in a browser.
Can I do anything to make the ë work as well or do I need to change my returning xml and change the ë to ë?
Use NVARCHAR type to resolve this issue.
An ë (with umlaut) turns into ?
You might need a user function like this to encode the resulting string to HTML for this one
the next character is cut of, < turns into <, & into &... and
so on

Exec master..xp_cmdshell 'Del...' is not working

I'm trying to delete a excel file using xp_cmdshell, but is not working... I check the other questions of here but I can't solve my issue, I activated the advanced options of SQL SERVER to allow this sintax, but it show's me a error message like this:
No se encuentra C:\xampp\htdocs\Web_Seguimiento\Body\Upload\Files\Carga
No se encuentra C:\WINDOWS\system32\Masiva
NULL
My code:
---- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
declare #sqlquery varchar(255)
declare #name varchar(255)
set #name = 'Carga Masiva Usuarios General.xlsx'
set #sqlquery= 'Del C:\xampp\htdocs\Web_Seguimiento\Body\Upload\Files\'+#name +''
exec master..xp_cmdshell #sqlquery
Can anybody tell me what I do wrong, or how can I solve this issue...???
Try putting quotes around the file name, like this:
set #sqlquery= 'Del "C:\xampp\htdocs\Web_Seguimiento\Body\Upload\Files\'+#name +'"'