How can I resolve the "could not find server" error when using execute(@sql) at [linked_server] with a stored procedure?

Timothy Dooling 25 Reputation points
2024-05-08T15:59:54.14+00:00

I have created a stored procedure in SQL Server to execute a PL/SQL script to a linked Oracle server using either sp_executesql or execute(@sql).

The former method works while the latter generates a "cannot find linked server" or "Server is not configured for RPC" error.

I need to execute a PL/SQL script to return an IEnumerable<T> using Dapper on the Oracle server, but I must use an SP in SQL Server to call the linked server.

I suspect a configuration issue, but I'm new to working with linked servers in SSMS.

The stored procedure is:

ALTER PROCEDURE [dbo].[Oracle_s] 
(
	@sql varchar(4000)
)
AS
BEGIN
	declare @result bit = 0;
	set nocount on;
	declare @temp nvarchar(4000) = 'SELECT * FROM OPENQUERY(LK_VPROD, ''' + REPLACE(@sql,'''', '''''') + ''')';
	declare @tsql varchar(4000) = REPLACE(@sql,'''', '''''');
	begin try
		--exec sp_executesql @temp;
		execute(@sql ) at [LK_VPROD];
		set @result = 1;
	end try
	begin catch
		SELECT ERROR_MESSAGE() AS ErrorMessage; 
		set @result = 0;
	end catch
	set nocount off;
	return @result;
END

How can I resolve this error and execute the PL/SQL script successfully through the stored procedure using the execute(@sql) at linked_server command?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,853 questions
SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
48 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 102.2K Reputation points MVP
    2024-05-08T21:08:05.84+00:00

    As Shivam says, you need to register the linked server. Or, rather, you have to ask your DBA to do this.

    Furthermore, for a linked server to Oracle, you need an OLE DB provider for Oracle. Microsoft has no OLE DB provider that supports modern versions of Oracle, but you need to get them from Oracle. I don't know if they are free downloads, or they come with a fee.

    Once you have the linked server in place, you can use either OPENQUERY or EXECUTE AT. The latter is easier, since it accepts parameters. With OPENQUERY you need to build a query string for the Oracle query and then you need to nest that query in a T-SQL query to run with dynamic SQL. It can easily drive you insane.

    With EXECUTE AT, you can run things like:

    EXECUTE ('SELECT a, b, c FROM tbl WHERE col1 = ? AND col2 = ?', @val1, @val2) AT ORACLESRV
    

    Note that the query you pass to EXECUTE AT is executed on the Oracle server, and thus needs to use Oracle syntax.

    To use EXECUTE AT, the server needs to be configured for RPC:

    EXEC sp_serveroption 'ORACLESRV', 'rpc out', 'true'
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Shivam Kumar 541 Reputation points
    2024-05-08T20:34:56.6666667+00:00

    Hi @Timothy Dooling

    Have you created/registered the linked server on sql server on which you are trying to run the procedure?

    execute(@sql ) at [LK_VPROD];

    Basically, means to run whatever is in @sql parameter on the linked server LK_VPROD and by linked server it means a linked server registered on sql server not the remote oracle server.

    Probably that is why you are getting server not found error as you mentioned "cannot find linked server"

    The reason sp_executesql is working because it's executing query locally on sql server.

    You need to add a linked server from object viewer by expanding Server objects and right clicking on Linked Server for being able to run queries on that Linked server using execute at command.

    User's image

    You can read more about it here:

    https://learn.microsoft.com/en-us/sql/t-sql/language-elements/execute-transact-sql?view=sql-server-ver16#g-using-execute-with-at-linked_server_name

    Also make sure the spns for the server are registered.

    1 person found this answer helpful.