I have a sql query that returns some values and I need to use those values as inputs to another SQL select statement.
The first sql query runs on DB1 that exists on server1. I want my web page to run the sqlquery1 and take the results of the query.
For example my sql query returns some values as
1 2 3 4 Then i need to use those values as inputs to another sql DB that exist on different server. And the final result displayed in my webpage.
What would be the easiest/effective way to do this. Imagine the queries are running on views and very limited access. No relations to DBservers either.
how2do
0 Points
1 Post
Using results of sql query as parameters to another sql query
Nov 21, 2012 07:31 PM|LINK
I have a sql query that returns some values and I need to use those values as inputs to another SQL select statement.
The first sql query runs on DB1 that exists on server1. I want my web page to run the sqlquery1 and take the results of the query.
For example my sql query returns some values as
1 2 3 4 Then i need to use those values as inputs to another sql DB that exist on different server. And the final result displayed in my webpage.
What would be the easiest/effective way to do this. Imagine the queries are running on views and very limited access. No relations to DBservers either.
wmec
Contributor
6228 Points
3226 Posts
Re: Using results of sql query as parameters to another sql query
Nov 22, 2012 01:02 AM|LINK
You can have a Stored proc to do this, and inside that, you can capture the variable from 1st query like
declare @var1 nvarchar(80)
select @var1=...
from ...
and further use such variable for other query.
HuaMin Chen
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: Using results of sql query as parameters to another sql query
Nov 22, 2012 01:11 AM|LINK
I think the most effective way would be to set up a linked server on one of the servers.
eralper
Contributor
6048 Points
971 Posts
Re: Using results of sql query as parameters to another sql query
Nov 22, 2012 06:33 AM|LINK
To execute SQL query on an other server as Dan offered a linked server is must
Within .NET code or in T-SQL procedure code, you can read values into parameters
Then you can query second server data over a linked server connection.
SQL Server 2012