dynamic linked server query

  • hi ,

    Below is the linked server query ,here i m passing the server and linked server name dynamically

    The problem is that its giving error for very last line (it is not taking linked server name dynamically)

    but if i hard coded the linked server in last line its working

    i need to have linked server dynamically

    ----------------------------------------------------------------------------

    declare @servername varchar(20),

    @Linked_server varchar(20),

    @SQLCmd varchar(200),

    @TableName varchar(50)

    set @TableName = 'sysobjects'

    SELECT @servername = @@servername

    IF LEFT(@servername,2) ='D2'

    SET @Linked_server = 'IN\DEV'

    ELSE

    SET @Linked_server = 'D2\DEV'

    print @servername

    print @Linked_server

    SET @SQLCmd = 'SELECT top 10 * from [' + @servername + '].PROD.dbo.' + @TableName + ' WITH (NOLOCK)'

    EXEC(@SQLCmd) AT @Linked_server

    ------------------------------------------------------------------------------

    Please help

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Try without AT @Linked_Server, it will work, its working for me, what version of SQL are u using mate???

  • Don't suppose you know how to see SYNONYMS in a linked server.

    I have SYNONYMS set up on server b and on server a set the user with view definition of SYNONYMS but cannot see them in linked server only see tables and views. Can't see SP either

    Right now i can ony set up a view to see the data on servera and want the user to be able to run select * from mysynonym

    Just curious if even possible.

  • Bhuvnesh (11/12/2008)


    hi ,

    Below is the linked server query ,here i m passing the server and linked server name dynamically

    The problem is that its giving error for very last line (it is not taking linked server name dynamically)

    but if i hard coded the linked server in last line its working

    i need to have linked server dynamically

    ----------------------------------------------------------------------------

    declare @servername varchar(20),

    @Linked_server varchar(20),

    @SQLCmd varchar(200),

    @TableName varchar(50)

    set @TableName = 'sysobjects'

    SELECT @servername = @@servername

    IF LEFT(@servername,2) ='D2'

    SET @Linked_server = 'IN\DEV'

    ELSE

    SET @Linked_server = 'D2\DEV'

    print @servername

    print @Linked_server

    SET @SQLCmd = 'SELECT top 10 * from [' + @servername + '].PROD.dbo.' + @TableName + ' WITH (NOLOCK)'

    EXEC(@SQLCmd) AT @Linked_server

    ------------------------------------------------------------------------------

    Please help

    Use it Like this

    ------------------------------------------

    declare @servername varchar(100),

    @Linked_server varchar(100),

    @SQLCmd varchar(200),

    @TableName varchar(100)

    set @TableName = 'sysobjects'

    SELECT @servername = @@servername

    --IF LEFT(@servername,2) ='D2'

    --SET @Linked_server = 'IN\DEV'

    --ELSE

    SET @Linked_server = '[xx.xxx.x.xxx]'

    print @servername

    print @Linked_server

    SET @SQLCmd = '(''SELECT top 10 * from abc.dbo.' + @TableName + ' WITH (NOLOCK)'') AT ' + @Linked_server

    select @SQLCmd = 'EXEC ' + (@SQLCmd)

    EXEC(@SQLCmd)

    Regards

    Ashok

  • Thanks for the script

  • This is not working for me, can you suggest what is the issue in below code:

    DECLARE @myCount int;

    DECLARE @sql1 nvarchar(MAX);

    DECLARE @DBAT [varchar](10) = 'LS_EG_DEV'

    DECLARE @sql nvarchar(max);

    set @sql =

    N'BEGIN

    SELECT count(*) into :mCount FROM OWNER_CDG.CLI_CLIENT;

    END;'

    SET @sql1 = N'(@sql, @myCount OUTPUT) at ' + @DBAT

    SELECT @sql1

    SELECT @sql1 = 'EXEC ' + ( @sql1)

    SELECT @sql1

    EXEC (@sql1)

Viewing 6 posts - 1 through 5 (of 5 total)

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