Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Create View that gets different sql servers

Author  Topic 

jandh98
Starting Member

10 Posts

Posted - 2015-01-29 : 15:35:42
I'm trying to create a view that gets the sql server from another table. So I'd like to create something like:

Create View as
Select
name

from
THISSERVER.system.dbo.employee


THISSERVER can change - and I can create dynamic sql that will execute but it will always put THISSERVER. What I want is to not have that hard coded in the view.

Hopefully that makes sense.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-01-29 : 15:39:55
You can't do that with a view as dynamic sql is not allowed in there. You could use a stored procedure, but I question this design. Why are you using the four-part name for the object if the object is local?

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

viggneshwar
Yak Posting Veteran

86 Posts

Posted - 2015-01-30 : 10:19:22
Four part is required right SERVERNAME.DATABASENAME.SCHEMANAME.TABLENAME.
He is trying to do the dynamic part. So that even that is local server he has to specify the servername.

Regards
Viggneshwar A
Go to Top of Page
   

- Advertisement -