SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


dynamic linked server query


dynamic linked server query

Author
Message
Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13148 Visits: 4077
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;-)
CrazyMan
CrazyMan
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2964 Visits: 1597
Try without AT @Linked_Server, it will work, its working for me, what version of SQL are u using mate???
TRACEY-320982
TRACEY-320982
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3098 Visits: 1014
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.
ashok84.kr
ashok84.kr
Right there with Babe
Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)Right there with Babe (796 reputation)

Group: General Forum Members
Points: 796 Visits: 677
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
TRACEY-320982
TRACEY-320982
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3098 Visits: 1014
Thanks for the script
RS7
RS7
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 14
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search