Execute a Stored Procedure using OpenQuery

  • Hi All,

    I am trying to execute a "ServerB" Stored Procedure in "ServerA".This SP is in Multiple DB's in ServerB.

    I am trying to use Openquery and Dynamic SQL to do that.But I am having issues.

    Intially i am trying to pass just one DBname as parameter..if it returns values then i can use cursor or other options to retrieve for multiple DB's

    Please Help!!!

    Ex:

    DECLARE @TSQL varchar(8000), @DBNAME char(20)

    SELECT @DBNAME = 'DB1'

    SELECT @TSQL = 'SELECT * FROM OPENQUERY(serverB'+','+''exec '' + @DBNAME + ''.dbo.sp_StoredProcedure''+')'

    EXEC (@TSQL)

  • Hi,

    You can execute the stored procedure as following

    exec [Linkedserver].databasename.dbo.usp_testing

    I am getting problem inexecuting the query thro OPENQUERY

    SELECT *

    FROM OPENQUERY([Linkedserver],databasename.dbo.usp_testing)

    So yOu can write a SQL Function Object on the top of the procedure

    and call the function thro linked server.

    select * from OpenQuery(LinkeServername,'select * from batabase.dbo.funcname()')

    In this case the function will return a TABLE data type which you

    can access in your code.

    Thanks,

    Rahul

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply