December 28, 2006 at 7:33 am
does anyone know how or if I can accomplish the following?
SELECT s.ShowID, sd.ContactName, sd.ContactEmail, s.datasource
FROM show s
INNER JOIN [s.datasource].dbo.Showdetails sd
ON s.showid = sd.showid
where sd.SendNightlyEmail = 1
Basically I'm trying to join to a different sql database based on the datasource value that is returned in the select column... Is this possible?
December 28, 2006 at 4:19 pm
in general, yes.
December 28, 2006 at 6:21 pm
DECLARE
@strSQL nvarchar(1000)
,@dataSource varchar(100)
SET @dataSource = 'SomeDB'
SELECT @strSQL = 'SELECT s.ShowID, sd.ContactName, sd.ContactEmail, @dataSource
FROM show s
INNER JOIN [' + @dataSource + '].[dbo].[Showdetails] sd
ON s.showid = sd.showid
where sd.SendNightlyEmail = 1'
EXEC sp_executesql @strSQL
______________________________________________________________________
Personal Motto: Why push the envelope when you can just open it?
If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.
Jason L. SelburgDecember 28, 2006 at 6:35 pm
This might work better :
DECLARE
@strSQL nvarchar(1000)
,@dataSource varchar(100)
SET @dataSource = 'SomeDB'
SELECT @strSQL = 'SELECT s.ShowID, sd.ContactName, sd.ContactEmail, ' + @dataSource + '
FROM show s
INNER JOIN [' + @dataSource + '].[dbo].[Showdetails] sd
ON s.showid = sd.showid
where sd.SendNightlyEmail = 1'
EXEC sp_executesql @strSQL
December 28, 2006 at 6:38 pm
Yep, that will probably work better, now I'll go stand in the corner for a few minutes. *GRIN*
______________________________________________________________________
Personal Motto: Why push the envelope when you can just open it?
If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.
Jason L. SelburgDecember 28, 2006 at 6:41 pm
If you insist, but just remember that I didn't put you there!
December 28, 2006 at 8:57 pm
I'm well aware that it's all me... LOL
______________________________________________________________________
Personal Motto: Why push the envelope when you can just open it?
If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.
Jason L. SelburgJanuary 2, 2007 at 7:41 am
the solutions have me setting a datasource variable, what i'm trying to accomplish is that one of the selected columns (s.datasource) in the first table contains the database to be used for the join.
January 3, 2007 at 5:51 am
You would need to alter
SET @dataSource = 'SomeDB'
to
SET @datasource = (SELECT ColumnContainingDatabaseName
FROM TableContainingDatabaseName
WHERE CriteriaForDatabaseName
)
-------------------------------------------------------------------------
Normal chaos will be resumed as soon as possible. :crazy:
January 3, 2007 at 6:11 am
the problem is that this query returns multiple rows, the join needs to be dynamic based on what the current row is returning in the first table... maybe this isn't possible and I need to handle it inside my programming language of choice.
January 5, 2007 at 7:49 pm
Everything is possible in SQL. Only need some work.
Approach 1: Already U know how U can use Dynamic SQL for the purpose.
Approach 2: If U have to use another db frequently, why not U make a Linked Server and then use it as simple query.
January 8, 2007 at 6:08 am
I think I've already show how Dynamic SQL is not the answer in this case. This is a database on the same server, no need for linked servers. Once again, the query from the first table in the join returns multiple records, therefore i cannot just set a variable before i run the SQL.
January 8, 2007 at 7:07 am
Providing you do not exceed 4000 chars try
DECLARE @sql nvarchar(4000)
SET @strSQL = ''
SELECT @sql = @sql + 'SELECT s.ShowID,sd.ContactName,sd.ContactEmail,' + [dataSource] + ' FROM show s INNER JOIN [' + [dataSource] + '].[dbo].[Showdetails] sd ON s.showid=sd.showid WHERE sd.SendNightlyEmail=1;'
FROM show
EXEC sp_executesql @sql
Otherwise use a cursor
Far away is close at hand in the images of elsewhere.
Anon.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply