| | |
Searching in 2 databases
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 9
Reputation:
Solved Threads: 0
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:
Without using dymanic SQL.
muek
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)
CREATE proc dbo.uspMySearch ( @database1 nvarchar(50), @database2 nvarchar(50) ) AS SET nocount ON SELECT * FROM @database1.dbo.table1 UNION SELECT * FROM @database2.dbo.table1
Without using dymanic SQL.
muek
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Jun 2005
Posts: 60
Reputation:
Solved Threads: 5
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.
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.
Look 

sql Syntax (Toggle Plain Text)
USE Master GO CREATE proc GetResult @studentFirstName VARCHAR AS SELECT [ID] ,[FirstName] ,[LastName] ,[Grade] ,[dt] FROM [BlogEngine].[dbo].[Student] WHERE [FirstName] = @studentFirstName UNION SELECT [ID] ,[FirstName] ,[LastName] ,[Grade] ,[dt] FROM [MaxMinManufacturingDM].[dbo].[Student] WHERE [FirstName] = @studentFirstName 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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
haa?
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
oooh, sorry, really sorry I didn't notice that!!
You can't do such thing
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Directly your stored procedure would not work, you will need to generate a dynamic SQL statement like this:-
Note I haven't really tried the code out, but this is how I used execute dynamic SQL Queries.
sql Syntax (Toggle Plain Text)
CREATE proc dbo.uspMySearch ( @database1 nvarchar(50), @database2 nvarchar(50) ) as SET nocount on execute 'select * from ' + @database1 + '.dbo.table1 UNION select * 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 ?"
"How to ask questions the smart way ?"
![]() |
Similar Threads
- SQL Injection Attack (Database Design)
- transfering data between databases (MS Access and FileMaker Pro)
- Searching multiple MS SQL Tables (VB.NET)
- PHP vs ASP... the big ShOwdOwN (IT Professionals' Lounge)
- Can we access Databases through C or C++ (C++)
- Switching from CS to being a Statistician? (IT Professionals' Lounge)
- want to write latin translator what language should i choose (Computer Science)
- databases thatcan be used with php!!! (PHP)
- 35 processes, need to trim the fat (Viruses, Spyware and other Nasties)
- ColdFusion 5 Web Application Construction Kit (ColdFusion)
Other Threads in the MS SQL Forum
- Previous Thread: Connection is Busy with results for another hstmt ERROR
- Next Thread: selecting from other tables with information from the main table
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last autogrowth business connectingtodatabaseinuse count cursor data database dateadd datepart day" dbsize deadlock delete_trigger getdate highperformancecomputing hpc hpcserver2008 ibm iis limit loop maximum microsoft ms mssql multiple multithreading news number permission position query reporting result server services sets single source sql sqlserver sqlserver2005 subtype supercomputing supertype tables uniqueid update view weekday






