943,617 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 1193
  • MS SQL RSS
Jan 5th, 2009
0

Searching in 2 databases

Expand Post »
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:

MS SQL Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
muek is offline Offline
9 posts
since Dec 2008
Jan 5th, 2009
0

Re: Searching in 2 databases

Yes, I tried it, did you?
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 6th, 2009
0

Re: Searching in 2 databases

Yes I have tried, but doesn't run.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
muek is offline Offline
9 posts
since Dec 2008
Jan 6th, 2009
0

Re: Searching in 2 databases

Look, it works from Master database, and give me the SQL script and the error you get when you run it
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 7th, 2009
0

Re: Searching in 2 databases

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.
Reputation Points: 33
Solved Threads: 9
Junior Poster
pclfw is offline Offline
132 posts
since Jun 2005
Jan 7th, 2009
0

Re: Searching in 2 databases

Look
sql Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 7th, 2009
-1

Re: Searching in 2 databases

haa?
Attached Thumbnails
Click image for larger version

Name:	select from different database.jpg
Views:	14
Size:	49.9 KB
ID:	8784  
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 7th, 2009
0

Re: Searching in 2 databases

RamyMahrous please SEE my example.
It's completely different from yours.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
muek is offline Offline
9 posts
since Dec 2008
Jan 7th, 2009
0

Re: Searching in 2 databases

oooh, sorry, really sorry I didn't notice that!!
You can't do such thing
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 8th, 2009
0

Re: Searching in 2 databases

Directly your stored procedure would not work, you will need to generate a dynamic SQL statement like this:-

sql Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Connection is Busy with results for another hstmt ERROR
Next Thread in MS SQL Forum Timeline: selecting from other tables with information from the main table





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC