How to run deployed R Code from SQL server 2016 - sql

How can i run an R Code which is deployed in Microsoft R Server using sp_execute_external_script.
I have an R Script, which is pretty lengthy. Now i want to deploy that script to R Server, so that any one with privileges can access that script. Also i want to execute that script from an SQL Server Stored Procedure by referring the script that is deployed in R Server.
Is it possible to do that ?

tl;dr yes, you can.
I would recommend starting with this introduction from Microsoft:
https://msdn.microsoft.com/en-us/library/mt604885.aspx
When you understand it (and have set up a SQL Server 2016 correctly) the following tutorials can be helpful: https://msdn.microsoft.com/en-us/library/mt591993.aspx

Yes you can
Note: To run R code in Transact-SQL, you must be connected to an instance of SQL Server 2016 where SQL Server R Services is already installed .
Exemple:
exec sp_execute_external_script #language =N'R',
#script=N'OutputDataSet<-InputDataSet',
#input_data_1 =N'select 1 as hello'
with result sets (([hello] int not null));
go
Good Lock

Related

Run MS SQL server script on startup

I am trying to run an SQL script when I start (or restart) my windows 2012 R2 server instance (Google Cloud Server). I am doing so using an SQL script, a Batch-file and the task-scheduler.
For the sake of testing I have created a simple SQL-script that adds a datestamp to a table:
USE <Databasename>
GO
INSERT INTO testingTable(time_Stamp)
VALUES (GETDATE())
SELECT * FROM testingTable
(where Databasename obviously contains the name of the specific database)
The batch-file looks as follows:
sqlcmd -S <servername> -i "C:\Temp\testQuery.sql" > C:\Temp\output.txt
I am outputting everything to a text-file. When I run the Batch-file the output looks fine: it prints a list with all the times I have run this SQL-query and saves it in the text-file.
I have scheduled this task to run on startup (following the steps here: https://www.sevenforums.com/tutorials/67503-task-create-run-program-startup-log.html). I have tried a whole range of settings here but nothing seems to work, including the exact settings as highlighted in the forum.
When I now restart the server the output file shows the following error message:
Msg 904, Level 16, State 3, Server <servername>, Line 1
Database 7 cannot be autostarted during server shutdown or startup.
Msg 208, Level 16, State 1, Server <servername>, Line 2
Invalid object name 'testingTable'.
It seems like MS SQL does not allow scripts to be run before you log-in to one of the user accounts.
The problem really is that the actual SQL tasks that I want to run have to be run very early in the morning such that they are done when everyone arrives at the office. I have managed to automate the startup of the server using VMPower, but I can not automate logging in to one of the accounts.
I was hoping someone could give me some feedback on how to resolve this issue. Preferably I would want my current setup to work, but if anyone has an idea on how to automate logging in to an account on an existing google cloud server instance that would be really helpful as well.
Thank you,
Joost
SQL Server offers the system stored procedure sp_procoption which can be used to designate one or more stored procedures to automatically execute when the SQL Server service is started.
For instance, you may have an expensive query in your database which takes some time to run at first execution. Using sp_procoption, you could run this query at server startup to pre-compile the execution plan so one of your users does not become the unfortunate soul of being first to run this particular query. I've used this feature to set up the automatic execution of a Profiler server side trace which I've scripted. The scripted trace was made part of a stored procedure that was set to auto execute at server start up.
exec sp_procoption #ProcName = ['stored procedure name'],
#OptionName = 'STARTUP',
#OptionValue = [on|off]
Read more: Automatically Running Stored Procedures at SQL Server Startup.
Docker
For solution to MSSQL Docker image, see: SQL Server Docker container is stopping after setup.

How do you pull data from a database to another one using a linked server?

I have a local server set up on my computer with a local database. I am trying to connect to a network server with a linked server (I'm having a lot of trouble with this) and then create tables and views in the local database from the data in the network database. I'm using Microsoft SQL Server Management Studio 2012.
Try something like :
SELECT * from openquery(LINKED_SERVER_NAME,'SELECT * FROM TABLE')
If your linked server is well configured, you should be able to query the tables in the linked server using openquery.
Try this. First you link the Server then run query against the linked Server.
EXEC Sp_addlinkedserver
#server="MY-PC\SQLServer1",
#srvproduct='SQL Server'
-- from SQL Server2 you run this query
SELECT *
FROM Openquery("my-pc\sqlserver1", 'select * from Students')
Reference: Running Query against linked Server
SELECT * FROM [LINKEDSERVERNAME].[DATABASENAME].[DBO].[TABLENAME]

Moving data from SQL Server 2008 to remote SQL Server 2000, using sqlcmd

The setup:
I have two different machines, one with MS SQL Server 2008 which is my main machine and a secondary with MS SQL Server 2000. Newly generated data are stored in a specific table on the main server(2008).
The problem:
My main machine has limited storage, whereas my secondary one with the older SQL version(2000), doesn't have such kind of limitations.
A possible solution:
At least as a temporary solution, i could try to move some data, on a daily schedule, from the main machine to the secondary, using sqlcmd, run by a Windows Task Scheduler.
The largest portion of my data are stored on a single table so the idea is to "cut" them from the table on the main server and append them on a "backup/depot/storage" table on my secondary server.
What i have tried so far:
So far, i haven't been able to simultaneously connect to both servers from the main one, at least using sqlcmd. Is there a limitation for the sqlcmd to the connections it can create simultaneously?
Other possible ways:
Is there a suggested practice for that case? Would it be a good idea to write a vbs script to export from the main server and import to the secondary?
All corrections and suggestions are welcome. And thanks for your time.
First, link the servers. From the server you want to INSERT into, run this SQL (using any tool you want to run SQL with... SQL Server Management Studio or SQLCMD or OSQL or whatever):
EXEC sp_addlinkedServer N'remoteSqlServer\Instance', N'SQL Server'
EXEC sp_addlinkedsrvlogin #rmtsrvname = N'remoteSqlServer\Instance', #useself = 'FALSE', #rmtuser = N'user', #rmtpassword = N'password'
Replace "remoteSqlServer\Instance" with the actual host-name\instance of the SQL Server you want to copy data FROM. Also replace the "user" and "password" with appropriate login credentials to that server.
After that is done, you can execute SQL like the following against this server (from anywhere, including SQLCMD):
INSERT INTO [LocalTable]
SELECT * FROM [remoteSqlServer\Instance].[DatabaseName].[schema].[TableName]
Again, this is just an example... you'd replace all those values with values appropriate to your source and destination databases (everything in the square brackets). For instance, it might look something like this:
INSERT INTO [HistoricalData]
SELECT * FROM [DBServer\SQL2008].[ProductionDatabase].[dbo].[CurrentData]
I hope this helps.

SQL Server: Query not linked server providing credencials. Is possible?

I tried to query another specific server many times, but i failed... I searched and i found out that the server must be linked with the other server in order to achieve what i want. Unfortunately is not in my hands to change that so my question is, if it is possible to query the other server by providing credentials.
The server i want to query has SQL Server 2005 Service Pack 4 (9.00.5000) and the server i'm working on has SQL Server 2005 Service Pack 3(9.00.4035).
Thanks in advance.
Use OPENROWSET or OPENDATASOURCE instead. Both allow you to pass credentials. This is your best option if you are unable to use a linked server.
This example works if you are using SQL Server logins.
SELECT a.*
FROM OPENROWSET('SQLNCLI', 'SERVER=<servername>;UID=<username>;PWD=<password>',
'SELECT FOO FROM FOO.BAR') AS a

Connect to another SQL Server via SQL Query?

Is it possible to connect to another SQL Server using a SQL query (so that one can also run queries on that server and use the result set in current server). If not, then why ?
Yes, you can accomplish connecting to another SQL Server by using a Linked Server, or you can query through servers by using openquery:
SELECT * FROM openquery([aa-db-dev01], 'Select * from TestDB.dbo.users')
Querying a linked sql server
Microsoft SQL Server only:
Yes, it is possible. You have to configure a linked Microsoft SQL Server. Here is the info: http://msdn.microsoft.com/en-us/library/ms188279.aspx
Once you have your servers configured, your query (on server1) would look like the following:
SELECT TOP 10 * FROM server2.yourdatabase.yourschema.yourtable
It is not simply possible to connect to 2 different SQL servers simultaneously with one query if you have a query that needs to run on 2 SQL servers' databases to get a required resultset (distributed query)
Then you must have to create a "Linked Server" in your local SQL server "linked to" the 2nd SQL server (the remote SQL server)
Once the Linked Server is created in your local server you may query both servers with one query from your local server's connection.
The linked Servers can be queried directly or by using OPENQUERY.
There is a difference of performance between 'Direct Linked Server Query' and 'Linked Server OPENQUERY' as in the direct query entire data will be flown back to local server from remote server and then the local server will process the filters locally while in OPENQUERY the process will be completed on the remote server and local server will get only the filtered dataset
This works in SQL 2012.
Shows up in a grey box
Has to be run prior to the SQL to be run on the other server, else the code runs on which ever server/database the query window is connected to. Local variables like ##Servername and SERVERPROPERTY return the same results as the server connected to. Which was darn unexpected!!!!
:Connect servername
Example run from SQLTEST
SELECT CONVERT(sysname, SERVERPROPERTY('servername'));
:Connect CSQL2008
SELECT CONVERT(sysname, SERVERPROPERTY('servername'));
SELECT CONVERT(sysname, SERVERPROPERTY('servername'));
Produces:
CSQL2008
CSQL2008
CSQL2008
While
select distinct( server_name) from msdb.dbo.backupset
go
:Connect CSQL2012
select distinct( server_name) from msdb.dbo.backupset ;
go
select distinct( server_name) from msdb.dbo.backupset ;
produces:
SQLTEST
CSQL2012
CSQL2012