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

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

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>')

SQL Image Extraction

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
--========================================================================

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

Escaping command parameters passed to xp_cmdshell to dtexec

I am calling an SSIS package remotely using a stored procedure and a call to xp_cmdshell:
declare #cmd varchar(5000)
set #cmd = '"C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\dtexec.exe" /Rep E /Sql Package /SET \Package.Variables[User::ImportFileName].Value;c:\foo.xlsx'
print #cmd
exec xp_cmdshell #cmd
This works fine, however I can not guarantee the variable value (c:\foo.xslx) is not going to contain spaces so I would like to escape that with quotes like below:
set #cmd = '"C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\dtexec.exe" /Rep E /Sql Package /SET \Package.Variables[User::ImportFileName].Value;"c:\foo.xlsx"'
But by doing this I get the error
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Both of the above commands work fine if executed within cmd.exe so I am guessing that SQL Server is interpreting my double quotes and changing something, but I can't figure out what.
In a nutshell, put CMD /S /C " at the beginning, and " at the end. In between you can have as many quotes as you like.
Here is how you do it:
declare #cmd varchar(8000)
-- Note you can use CMD builtins and output redirection etc with this technique,
-- as we are going to pass the whole thing to CMD to execute
set #cmd = 'echo "Test" > "c:\my log directory\logfile.txt" 2> "c:\my other directory\err.log" '
declare #retVal int
declare #output table(
ix int identity primary key,
txt varchar(max)
)
-- Magic goes here:
set #cmd = 'CMD /S /C " ' + #cmd + ' " '
insert into #output(txt)
exec #retVal = xp_cmdshell #cmd
insert #output(txt) select '(Exit Code: ' + cast(#retVal as varchar(10))+')'
select * from #output
After looking into this, it appears you have to use the DOS 8.3 notation with xp_cmdshell e.g. c:\progra~1... and you can only have one set of quotes on the arguments.
To get around this limitation, either use the older DOS notation, or put your code in a batch file instead which will run fine.
Source: http://social.msdn.microsoft.com/forums/en-US/sqlintegrationservices/thread/4e7024bb-9362-49ca-99d7-1b74f7178a65

Transfering the content of a variable to file in SQL Server 2k5

How do we transfer the content of a string variable to a file on a SP? I need to do these:
Create an empty text file from an SP
Push the content of a variable (whose len is 25487) to the newly created file
The variable size is Varchar(Max), and this is the code I am trying to make work (but it ain't [Sad] ) ---
Declare #cmd sysname
Declare #ReqContent Varchar(max)
SET #cmd = 'echo ' + #ReqContent + ' > C:\DD4TRD.txt'
EXEC master..xp_cmdshell #cmd ,NO_OUTPUT
Thanks,
Sayan
Check out this sproc from Reading and Writing Files in SQL Server using T-SQL.