Can I run SQL Server Job through Linked Server? - sql-server

I have two SQL Server machines:
SqlServer-1
SqlServer-2
I have a job created on SqlServer-1 named RunExeFile.
SqlServer-2 can access SqlServer-1 through a linked server.
Can I start the job RunExeFile from a stored procedure on SqlServer-2 ?

Yes, you can.
You can run this in your stored procedure on SqlServer-2 against linked Server LinkedServer1:
EXEC LinkedServer1.msdb.dbo.sp_start_job #job_name = N'RunExeFile';
You need to configure RPC and ensure the calling security context is set:

Related

How do I automatically fire a (remote) stored procedure in SQL Server when connecting to a remote server?

The specific issue is that I want a SQL Server to connect to an Oracle database and the Oracle database has a Virtual Private Database configured. I need to execute a static stored procedure to enable the VPD to see data. How can I configure SQL Server to fire a stored procedure upon connecting to a remote database? I figure if I can fire a local stored procedure, I can put the remote stored procedure call inside of that. The key is, I need the SQL Server to fire this as soon as it is done connecting to the remote database and before it tries to pass any other queries to it. I'm trying to avoid making the applications do it explicitly.
SQL Server does not offer a solution to my problem.
However, you can setup the service account to have a logon trigger to execute what is needed.
Create Trigger My_User.Logon_Trigger_Name
AFTER LOGON
ON SCHEMA
WHEN (USER = 'MY_USER')
BEGIN
--Do Stuff Here.
NULL;
EXCEPTION
WHEN OTHERS THEN
NULL; --Consume errors so you can still log on.
END;

Possible to set SQL Server Remote Query Timeout per Query for Linked Server calls?

For linked servers, I see how it is possible to change the "remote query timeout" configuration to hint a call to a linked server should complete or rollback within the specific timeout value. This appears to work across the SQL Server engine--is it possible to change the hint within a stored procedure, so that a specific stored procedure could run longer if needs to, but all other non-hinted SPROCs would timeout quicker if they run long?
Linked Query Timeout is discussed here:
http://support.microsoft.com/kb/314530
Example code to set it to timeout in 3 seconds is here:
sp_configure 'remote query timeout', 3
go
reconfigure with override
go
Not really advisable to change it within a stored procedure. remote query timeout is a global server setting when altered with sp_configure, so changing it in a stored procedure affects all remote queries for all linked servers on the server.
Additionally, executing sp_configure requires the ALTER SETTINGS server permission, which typically only sysadmin and serveradmin have. Granting these permissions to a data access account would be a security concern since they could potentially take your server down with sp_configure commands.
What I would suggest is creating a second linked server with a different name that you would use with just this one stored procedure. You can, in SSMS, configure a query timeout for each individual linked server. Adding a second linked server would enable you to query the same server with different linked server client settings. You might need to create a DNS CNAME to accomplish this if you're using plain SQL Server Linked Servers.

How to backup linked servers with SQL Server?

I want to backup/restore my data which is distributed in some linked servers, which are connected or linked in a central server. How can I do this?
I'm using SQL Server 2012 in all my nodes.
you can use the remote machine to run sp_executesql command within the dynamic SQL.
exec server.master.dbo.sp_executesql 'BACKUP DATABASE DBNAME1 to disk='\\Server\Share\backupfilename.bak''
Or you can do this
exec server.master.dbo.sp_executesql 'BACKUP DATABASE [DBNAME1] to disk='\\Server\Share\backupfilename.bak''
Both line are almost identical , except the square bracket [] on the second syntax.

SQL server - job calling procedure and procedure difference

I'm trying to create table from view from remote server (calling procedure UPDATE_PROC), like this:
SELECT *
INTO table
FROM [remote_server\database].DATABASE.dbo.view
And it works perfectly.
Problem is, when I try to create a new job, which is calling the previously worked stored procedure UPDATE_PROC
exec UPDATE_PROC
It doesn't work and reports:
Executed as user: NT AUTHORITY\SYSTEM. The OLE DB provider "SQLNCLI10" for linked server "remote_server\database" does not contain the table ""DATABASE"."dbo"."view"". The table either does not exist or the current user does not have permissions on that table. [SQLSTATE 42000] (Error 7314). The step failed
So it seems to be same, but it isn't. What I'm doing wrong?
UPDATE:
Remote server is SQL Server 2005, job is on SQL Server 2008
I'm logged as sa user. Procedure is working always, job calling stored procedure never
Job isn't run as sa user, but I don't know which user and where should be defined to execute the job propery....
It seems to me that's a security issue. User under which the job is running doesn't have permissions to access table on the remote server.

Execute an Oracle stored procedure from a SQL Server stored procedure

I have a SQL Server stored proc that needs to send information to an Oracle stored proc.
How do I set this up? Should it be a linked server? And how do I write the code to pass and execute the stored proc?
You could also use:
DECLARE #outputParameter int
EXEC ('BEGIN <procedureName>(?,?); END;', #inputParameter , #outputParameter OUTPUT) at <linkedServerName>
This allows capturing output parameters
a procedure "TestingProcedure" in Oracle server.
a linked Server "LinkedServer" in SQL Server.
call TestingProcedure in SQL server example:
EXECUTE ('begin TestingProcedure; end;') AT LinkedServer;
This is the syntax that I would probably try from the start:
SELECT * FROM OPENQUERY(<linked server name>, '{CALL <oracle sp>}')
What I've seen (I have not tried this myself) is that you can also use OPENQUERY to call an Oracle function which can then call the stored procedure, or you can turn the stored procedure into a function. Your connection from SQL Server would then look something like this:
SELECT * FROM OPENQUERY(<linked server name>, 'SELECT <oracle function> FROM dual')
Turns out I ended up having to use Microsofts CLR and write my own little C# application in order to handle the variables being pushed between the servers. Here's what I did:
C# Application starts on a scheduled task in windows
Application executes the SQL Server stored procedure and gets the return values
Application then executes the Oracle stored procedure with appropriate values

Resources