-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetPKJoin.sql
46 lines (38 loc) · 1.03 KB
/
GetPKJoin.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
if object_id('dbo.GetPKJoin') is not null
begin
exec ('drop function dbo.GetPKJoin')
end
go
create function dbo.GetPKJoin (@ColumnList xml, @FirstAlias varchar(6), @SecondAlias varchar(6))
returns varchar(8000)
as
begin
declare @ret varchar(8000)
; with src as
(
select c.i.value('@name','varchar(30)') as ColumnName
from @ColumnList.nodes('columns/column') c(i)
)
select @ret = (SELECT ' and '+case when @FirstAlias is null or len(@FirstAlias) < 1 then ''
else @FirstAlias+'.' end
+'['+ColumnName+'] = '+@SecondAlias+'.['+ColumnName+']'
from src
for xml path (''))
set @ret = substring(@ret,6,len(@ret)-4)
return @ret
end
go
-- Test the function
select dbo.GetPKJoin(N'<columns>
<column name="PKColumn" />
<column name="SecondPKColumn" />
</columns>'
,'s'
,'t')
select dbo.GetPKJoin(N'<columns>
<column name="PKColumn" />
<column name="SecondPKColumn" />
</columns>'
,''
,'t')
go