Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 01, 2012 04:24 AM by D J
Member
375 Points
1033 Posts
Mar 01, 2012 01:47 AM|LINK
i would like to join two tables and they are in different database in sp by passing @table_name. there is error , why??
select a.id,b.id from databaseA.'+@table_name+' a inner join databaseB.'+@table_name b on a.name=b.name'
there is error about database name, how to join.
Participant
856 Points
293 Posts
Mar 01, 2012 04:16 AM|LINK
Hi,
Try like this,
Declare @sql Varchar(500)
SET @sql='select a.id,b.id from databaseA.'+@table_name+' a inner join databaseB.'+@table_name+' b on a.name=b.name'
EXEC (@sql)
Contributor
5362 Points
941 Posts
Mar 01, 2012 04:24 AM|LINK
Use like databaseA.DBO.'+@table_name
-_-
Member
375 Points
1033 Posts
join different database to get table sp (sql server)
Mar 01, 2012 01:47 AM|LINK
i would like to join two tables and they are in different database in sp by passing @table_name. there is error , why??
select a.id,b.id from databaseA.'+@table_name+' a inner join databaseB.'+@table_name b on a.name=b.name'
there is error about database name, how to join.
vendan
Participant
856 Points
293 Posts
Re: join different database to get table sp (sql server)
Mar 01, 2012 04:16 AM|LINK
Hi,
Try like this,
Declare @sql Varchar(500)
SET @sql='select a.id,b.id from databaseA.'+@table_name+' a inner join databaseB.'+@table_name+' b on a.name=b.name'
EXEC (@sql)
Kalaivendan
Please Mark as Answer if this post helps you!
D J
Contributor
5362 Points
941 Posts
Re: join different database to get table sp (sql server)
Mar 01, 2012 04:24 AM|LINK
Use like databaseA.DBO.'+@table_name