Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Refering to multiple databases

 
   Database Help (Home) -> General Discussions RSS
Next:  How to check interception time periods  
Author Message
K Viltersten

External


Since: Jun 10, 2008
Posts: 15



(Msg. 1) Posted: Tue Dec 16, 2008 8:25 am
Post subject: Refering to multiple databases
Archived from groups: comp>databases>ms-sqlserver, others (more info?)

I know i can switch the currently refered
database by choosing a different name in
the drop-down menu. However, i'd like to
programmatically pick a database.

I'd like to compare the same search from
two databases as follows.

select * from olds.stuff
select * from news.stuff

How can i do that?

--


Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.

 >> Stay informed about: Refering to multiple databases 
Back to top
Login to vote
Dan Guzman1

External


Since: Aug 22, 2004
Posts: 840



(Msg. 2) Posted: Tue Dec 16, 2008 8:25 am
Post subject: Re: Refering to multiple databases [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> I'd like to compare the same search from
> two databases as follows.
>
> select * from olds.stuff
> select * from news.stuff
>
> How can i do that?

Use 3-part names:

select * from olds.dbo.stuff
select * from news.dbo.stuff

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

 >> Stay informed about: Refering to multiple databases 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 3) Posted: Tue Dec 16, 2008 8:25 am
Post subject: Re: Refering to multiple databases [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi
One option is dynamic sql.
declare @dbanme sysname
set @dbname='db'
exec('select * from '+@dbname+'..tblname)


--in the case that the structure of the dbs are the same

or create a vew in master database like
create view myview
as
select 'db1' as dbname, * from
db1..tbl
union all
select 'db2 as dbname, * from
db1..tbl
......


usage
select * from view where dbname='db1'





"K Viltersten" wrote in message

>I know i can switch the currently refered
> database by choosing a different name in
> the drop-down menu. However, i'd like to
> programmatically pick a database.
>
> I'd like to compare the same search from
> two databases as follows.
>
> select * from olds.stuff
> select * from news.stuff
>
> How can i do that?
>
> --
>
>
> Regards
> Konrad Viltersten
> ----------------------------------------
> May all spammers die an agonizing death;
> have no burial places; their souls be
> chased by demons in Gehenna from one room
> to another for all eternity and beyond.
>
 >> Stay informed about: Refering to multiple databases 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 4) Posted: Tue Dec 16, 2008 8:25 am
Post subject: Re: Refering to multiple databases [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi
I'd like to notice you that by using dynamic sql you will have to deal
with SQL Injection .... use it carefully




"Uri Dimant" wrote in message

> Hi
> One option is dynamic sql.
> declare @dbanme sysname
> set @dbname='db'
> exec('select * from '+@dbname+'..tblname)
>
>
> --in the case that the structure of the dbs are the same
>
> or create a vew in master database like
> create view myview
> as
> select 'db1' as dbname, * from
> db1..tbl
> union all
> select 'db2 as dbname, * from
> db1..tbl
> .....
>
>
> usage
> select * from view where dbname='db1'
>
>
>
>
>
> "K Viltersten" wrote in message
>
>>I know i can switch the currently refered
>> database by choosing a different name in
>> the drop-down menu. However, i'd like to
>> programmatically pick a database.
>>
>> I'd like to compare the same search from
>> two databases as follows.
>>
>> select * from olds.stuff
>> select * from news.stuff
>>
>> How can i do that?
>>
>> --
>>
>>
>> Regards
>> Konrad Viltersten
>> ----------------------------------------
>> May all spammers die an agonizing death;
>> have no burial places; their souls be
>> chased by demons in Gehenna from one room
>> to another for all eternity and beyond.
>>
>
>
 >> Stay informed about: Refering to multiple databases 
Back to top
Login to vote
K Viltersten

External


Since: Jun 10, 2008
Posts: 15



(Msg. 5) Posted: Tue Dec 16, 2008 8:25 am
Post subject: Re: Refering to multiple databases [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> I'd like to compare the same search from
>> two databases as follows.
>>
>> select * from olds.stuff
>> select * from news.stuff
>>
>> How can i do that?
>
> Use 3-part names:
>
> select * from olds.dbo.stuff
> select * from news.dbo.stuff


Thanks to both!

--


Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
 >> Stay informed about: Refering to multiple databases 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Cursor among several databases - Hi all, I am wondering if anyone can help. I am doing a sp that search for any string in any row of any table in any database. The idea is to create the sp on the master database and from there call it passing as parameter the database to do the search....

free databases - http://123maza.com/35/child419/

Accessing SQL Server Databases from Networked PC - Hello all. I have just built another PC (New PC)and have it connected to my Original PC via a LAN. I have my SQL Server 2000 installed on the Original PC. Both PCs use WinXP Pro OS. I believe I now have to install the Client Tools on the New PC but..

Should you perform maintenance on System databases? - Just wondering what sorts of maintenance programs you should do on the system databases, msdb, master etc? Also, how do you stop SQL Server 2000 from logging all your DTS jobs? My msdb database is 17 gigs!

multiple numbers in one statement - Hi, Is there any way to insert into table 100 integers from 1 to 100 in one insert statement? Now I have to use loop to insert numbers. I was wondering if there is the simpler way.
   Database Help (Home) -> General Discussions All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]