Way to access a REST Service via a SQL Server stored procedure? - sql

We are looking for a way to call a REST service from a SQL Server 2008 & newer stored procedure. We do not need to get any information from this service. We just need to force the call to run in the middle of a stored procedure.
I have found some 3rd party options or CLR; but I refuse to think there isn't an easier way since we just need to fire a call and don't need anything in return.
Hopefully someone can shed some light. Thanks!

You could make a SQL Agent job to run a PowerShell script, then run the job via msdb.dbo.sp_start_job. That way you wouldn't need any external scripts or utilites, nor any SQL-CLR.
Your job step would be something like:
$request = [System.Net.WebRequest]::Create('http://stackoverflow.com/')
$result = $request.GetResponse()

I can think at least of 2 ways of doing that:
1) Probably prefered way:
Make a C# dll and call its function from the store procedure example:
How to call C# function in stored procedure
2) Call the rest service with the help of CURL utility.
DECLARE #PassedVariable VARCHAR(100)
DECLARE #CMDSQL VARCHAR(1000)
SET #PassedVariable = 'SqlAuthority.com'
SET #CMDSQL = 'c:findword.bat' + #PassedVariable
EXEC master..xp_CMDShell #CMDSQL
have the call to the ws inside the bat file

Related

Catching OUT parameter value of a bigquery Procedure from composer

I am calling a bigquery procedure using BigQueryInsertJob operator using "declare out_var String; call bq_sp("input params", out_var)". This is getting called properly and executing the stored procedure properly also. But what I need to do along with that, is to print the value of out_var to the composer log, to catch the value of out_var, so that I can do some more things depending on the value of that variable. Could you please help me, how can I catch the value of the OUT parameter value from the cloud composer. Thank you.
I have a stored procedure with input and output parameters. and calling that from composer.
This is working absolutely fine.
I am expecting to fetch the OUT parameter value in the cloud composer job

Can R access internet whilst running as an external script in SQL Server?

I run the R portion of the code in Rstudio, and it works fine.
But when I call it inside SQL Server, it runs successfully, but cant return any results, only "I've got nothin'" which is the default error of the wikifacts package when it cant find a result to return.
EXEC sp_execute_external_script
#language = N'R',
#script = N'
library(wikifacts)
query <- "Microsoft"
answer <- as.data.frame(wiki_define(query, sentence = 1))
print(answer)
'
Im just wondering if its being blocked along the way, or not possible to call external data from SQL server? And what a possible workaround could be?
Solved..!
For anyone who wants to know, I had to disable outbound firewall rules for SQL server.

Dynamic Procedure BigQuery

Does Bigquery support dynamic procedure calls? I am looking to generate a procedure name as string based on variables and call the string.
EXECUTE IMMEDIATE returns this error clearly it doesn't support CALL. Is there any other way?
SQL created by EXECUTE IMMEDIATE contains unsupported statement type: CallStatement.
Thank you.
I make this answer for visibility, the approach you can use is as follow (as mention in the comments by mikhail):
CREATE OR REPLACE PROCEDURE `project-id.dataset`.maker(option INT64)
BEGIN
IF option=1 THEN
CALL `project-id.dataset`.create_user(); #call to procedure
ELSEIF option=2 THEN
CALL `project-id.dataset`.create_customer(); #call to procedure
ELSE
SELECT "END"; #default
END IF;
END
to use
CALL `project-id.dataset`.maker(2)
As stated in the comments, execute immediate do not support at the moment the usage of call.
I also found a feature request about supporting using call with execute immediate on google issue tracker you can leave a comment to support the feature request.

SQL Powershell to Create Backup of Objects

I would like to create a powershell script that I can run to backup objects to file before updating them. My goal is to backup objects before changing them in case something breaks. I would like to pass in parameters to run like the following:
backupobjects.ps1 -servername -databasename -schemaname -objectname -outputdirectory
So if I call this powershell script and pass in parameters the script will connect to the database and find the object and save the CREATE script and save the object to the outputdirectory passed in and put BEFORE_objectname.sql as the filename.
I am just starting in powershell so accepting parameters I have not learned yet.
Any guidance or suggestions would be helpful.
Rather than write it for you, here are a couple of nudges:
1) param is how you pass in parameters in powershell. I like to do it like so:
param (
[string] $server = (Read-Host "Enter a server name"),
[string] $db = (Read-Host "Enter a database name")
)
you then reference $server and $db later in your script as though you'd explicitly initialized them.
2) Most (if not all) objects in SQL server have a Script() method attached to them. For instance take a look at the Table class.
3) You can control how objects are scripted using the ScriptingOptions class. When you invoke the Script() method on an object, pass a ScriptingOptions object as an argument and the scripting behavior will be governed it.

How do you retrieve the return value of a DB2 SQL sproc using Perl DBI?

I need to retrieve the value returned by a DB2 sproc that I have written. The sproc returns the number of rows in a table and is used by the calling process to decide whether or not to update other data.
I have looked at several similar questions on SO but they refer to the use of out parameters instead of using the sproc's return value, for example:
Perl Dbi and stored procedures
I am using a standard DBI connection to the database with both RaiseError and PrintError enabled.
$sql_stmt = "call MY_TABLE_SPACE.MY_SPROC('2011-10-31')";
$sth = $dbh->prepare($sql_stmt)
or die "Unable to prepare SQL '$sql_stmt': $rps_met_dbh->errstr";
$rsp = 0;
$rsp = $sth->execute();
unless($rsp) {
print(STDERR "Unable to execute sproc: $rps_met_dbh->errstr\n");
}
print(STDERR "$?\n");
I have tried looking at $h->err for both the statement handle and the db handle.
I would really prefer communicating the number of rows via a return code rather than using SQLSTATE mechanism if I can.
Edit:
I have finished up using a dedicated out parameter to communicate the number of rows updated as follows:
$sql_stmt = "call MY_TABLE_SPACE.MY_SPROC('2011-10-31')";
$sth = $dbh->prepare($sql_stmt)
or die "Unable to prepare SQL '$sql_stmt': $rps_met_dbh->errstr";
$sth = $dbh->bind_param_inout(1, $rows_updated, 128)
or die "Unable to prepare SQL '$sql_stmt': $rps_met_dbh->errstr";
$rows_updated = 0;
$rsp = 0;
$rsp = $sth->execute();
unless($rsp) {
print(STDERR "Unable to execute sproc: $rps_met_dbh->errstr\n");
}
print(STDERR "$rows_updated\n");
Edit 2:
And now thinking about this further I have realised that I should apply the PragProg principle of "Tell. Don't Ask." That is, I shouldn't call the sproc. then have it give me back a number before I decide whether or not to call the anopther sproc, i.e. "Ask".
I should just call the first sproc. and have it decide whether it should call the other sproc or not, i.e. "Tell" and let it decide.
What is wrong with using an output parameter in your procedure. I've not got a working DB2 lying around right now or I'd provide an example but when I was using it I'm sure you can define output parameters in procedures and bind them with bind_param_inout. I cannot remember if a DB2 procedure can return a value (like a function) but if it can them using "? = call MY_TABLE_SPACE.MY_SPROC('2011-10-31')" would allow you to bind the output return value. If this doesn't work you could use a DB2 function which definitely can return a value. However, at the end of the day the way you get data out of a procedure/function is to bind output parameters - that is just the way it is.
I've no idea what you mean by "using SQLSTATE". I've also no idea what you mean by looking at $h->err as that is only set if the procedure fails or you cannot call the procedure (SQL error etc).