Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 i want to create dynamic query

Author  Topic 

inbs
Aged Yak Warrior

860 Posts

Posted - 2010-06-28 : 03:49:06
i have 3 table

Table 1.Customer IN Stage_Area DB
Table 2.Customer IN DWH
Table 3.TblPK hold the primary key (PK)

1.Customer :A(PK),B(PK),C,D
2.Customer :A(PK),B(PK),C,D
3.TblPK :PK,TableName

i want to JOIN between 1.Customer join 2.Customer ON TblPK
how can i do it?

like that:
SELECT * FROM Stage_Area.dbo.Customer a
JOIN DWH.dbo.Customer b ON a.(SELECT PK FROM TblPK )=b.(SELECT PK FROM TblPK )

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-06-28 : 04:45:22
Can you provide some sample data ?
Go to Top of Page

inbs
Aged Yak Warrior

860 Posts

Posted - 2010-06-28 : 04:57:12
[code]CUSTOMER
A B C D
1 10 AA X
2 20 BB Y

TblPK
PK TableName
A CUSTOMER
B CUSTOMER[/code]

i want that in ON will dynamic

SELECT * FROM Stage_Area.dbo.Customer a
JOIN DWH.dbo.Customer b ON a.A=b.A AND a.B=b.B
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-28 : 05:50:46
quote:
Originally posted by inbs

i want that in ON will dynamic



Why?
Does there exists multiple relatioships between the two tables?


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

inbs
Aged Yak Warrior

860 Posts

Posted - 2010-06-28 : 06:51:33
yes ,the pk composite from multiple columns
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-28 : 07:05:48
Declare @var as varchar(max)='your on clause'

Exec(SELECT * FROM Stage_Area.dbo.Customer a
JOIN DWH.dbo.Customer b ' + @var)

Make sure you have proper and matching aliases set for both the query and the dynamic ON clause


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page
   

- Advertisement -