Is there any way to execute a SQL Server procedure from an Oracle stored procedure? - sql-server

I have a SQL Server procedure which I want to call from an Oracle stored procedure and get the result. Then I need to do some processing on the data.
Can I create something like DB link to call the procedure? Or is there any possible way to do that?

I had this issue solved by contacting DBA and creating gateway then using DBMS_HS_PASSTHROUGH with db link assigned with username, password and database

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;

Using stored procedure in openrowset throws login_mapping error

Because I need to fill a view with dynamic data, I wrote a stored procedure to collect the data and use select from OPENROWSET('SQLNCLI','server=....;trusted_connection=yes','EXEC Stored_Procedure') as the view definition.
As far as I know you can't use variables in your view definition so that's why I choose this route.
The stored procedure collects data from a linked server source combined with native SQL Server data. The linked_server has the appropriate permissions setup for the remote user and can be successfully queried from outside SQL Server.
That works very well in a local context but when I try to use the view in a PostGreSQL foreign-table I get an ODBC error. The first errors involved the permission to execute the stored procedure by the caller's user account. After that I get stuck. It seems the connection with the server has sufficient access permissions but the OPENROWSET call to execute the stored procedure does not (no login_mapping exists).
However, if I change the SQL in the OPENROWSET command to a simple select statement (select * from db.schema.table) it works fine.
I tried to change the server in de OPENROWSET to a datasource to the local server itself, with the remote user to impersonate, but with no luck either. The same login_mapping error.
My guess is that this has something to do with the fact that the users security context is not the one used by the OPENROWSET command?
I have looked around a lot but was not able to find a solution. Can you help me?

Retrieve data from remote server in a stored procedure

Say I have a stored procedure that gets data from another database on another server like this:
SELECT * FROM [DatabaseServer].[DatabaseName].dbo.dbPerson
I can setup a linked server and it works as I would expect. Is there a way to connect to a remote server in a stored procedure without creating a linked server i.e. by specifying the connection string along with the username and password to connect with?
I know that I can do this with SSIS. I wandered if it is possible in a stored procedure.
As Zohar said you can use opendatasrouce like this
Select
*
from
OPENDATASOURCE(
'SQLOLEDB',
'Data Source=_SQL_SERVER_NAME_;User ID=_LOGIN_;Password=_PASSWORD_'
).[DatabaseName].dbo.dbPerson
WHERE ...
or you can use different providers and put different connection strings

Is there a way to check if SQL SP is running or completed?

I use Misrosoft SQL Server Management Studio to work with databases. I manually started SP execution on PC One. Is there any way to check if SP is still running or not from a different PC?
The system stored procedure sp_who might be of use: http://technet.microsoft.com/en-us/library/ms174313.aspx
If you know the SPID then you can use
EXEC sp_who <spid>;
Alternatively you can use the login name of the login that executed the command
EXEC sp_who 'george\gvee';
If the cmd column states AWAITING COMMAND then this indicates that that session is not performing a query or other action i.e. it is idle
You can use SQL Server Profiler
http://msdn.microsoft.com/en-us/library/ms187929(v=sql.90).aspx
Use a free tool like Idera SQL Check. Shows graphically a lot of statatiscs about the SQL Server current state.

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