Searching in 2 databases

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 9
Reputation: muek is an unknown quantity at this point 
Solved Threads: 0
muek muek is offline Offline
Newbie Poster

Searching in 2 databases

 
0
  #1
Jan 5th, 2009
Hi,

I would like to know if it's possible to create a procedure, where we pass 2 arguments (the databases where the search must be done) and how to use those parameters in select clause

What I would like to know is if it's possible something like:

  1. CREATE proc dbo.uspMySearch
  2. (
  3. @database1 nvarchar(50),
  4. @database2 nvarchar(50)
  5. )
  6. AS
  7.  
  8. SET nocount ON
  9.  
  10. SELECT *
  11. FROM @database1.dbo.table1
  12. UNION
  13. SELECT *
  14. FROM @database2.dbo.table1

Without using dymanic SQL.

muek
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Searching in 2 databases

 
0
  #2
Jan 5th, 2009
Yes, I tried it, did you?
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 9
Reputation: muek is an unknown quantity at this point 
Solved Threads: 0
muek muek is offline Offline
Newbie Poster

Re: Searching in 2 databases

 
0
  #3
Jan 6th, 2009
Yes I have tried, but doesn't run.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Searching in 2 databases

 
0
  #4
Jan 6th, 2009
Look, it works from Master database, and give me the SQL script and the error you get when you run it
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 60
Reputation: pclfw is an unknown quantity at this point 
Solved Threads: 5
pclfw pclfw is offline Offline
Junior Poster in Training

Re: Searching in 2 databases

 
0
  #5
Jan 7th, 2009
Ramy,

I tried this and it does not run.

You cannot just concatonate local variables and literals and expect them to combine into another literal.

The best that you could do is to set a char / varchar variable to the whole sql string that you want to run and then use EXEC to run that code. But the poster said he didn't want dynamic SQL.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Searching in 2 databases

 
0
  #6
Jan 7th, 2009
Look
  1. USE Master
  2. GO
  3. CREATE proc GetResult
  4. @studentFirstName VARCHAR
  5. AS
  6. SELECT [ID]
  7. ,[FirstName]
  8. ,[LastName]
  9. ,[Grade]
  10. ,[dt]
  11. FROM [BlogEngine].[dbo].[Student]
  12. WHERE [FirstName] = @studentFirstName
  13. UNION
  14. SELECT [ID]
  15. ,[FirstName]
  16. ,[LastName]
  17. ,[Grade]
  18. ,[dt]
  19. FROM [MaxMinManufacturingDM].[dbo].[Student]
  20. WHERE [FirstName] = @studentFirstName
  21.  
  22. GetResult 'a'
Last edited by Ramy Mahrous; Jan 7th, 2009 at 11:27 am.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Searching in 2 databases

 
-1
  #7
Jan 7th, 2009
haa?
Attached Thumbnails
select from different database.jpg  
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 9
Reputation: muek is an unknown quantity at this point 
Solved Threads: 0
muek muek is offline Offline
Newbie Poster

Re: Searching in 2 databases

 
0
  #8
Jan 7th, 2009
RamyMahrous please SEE my example.
It's completely different from yours.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Searching in 2 databases

 
0
  #9
Jan 7th, 2009
oooh, sorry, really sorry I didn't notice that!!
You can't do such thing
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Searching in 2 databases

 
0
  #10
Jan 8th, 2009
Directly your stored procedure would not work, you will need to generate a dynamic SQL statement like this:-

  1. CREATE proc dbo.uspMySearch (
  2. @database1 nvarchar(50),
  3. @database2 nvarchar(50)
  4. )
  5. as
  6.  
  7. SET nocount on
  8.  
  9. execute 'select *
  10. from ' + @database1 + '.dbo.table1
  11. UNION
  12. select *
  13. from ' + @database2 + '.dbo.table1'

Note I haven't really tried the code out, but this is how I used execute dynamic SQL Queries.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC