-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicCrossTab.sql
84 lines (55 loc) · 1.9 KB
/
DynamicCrossTab.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/************************************************\
SP: sp_GetCouncilsStats
Description: return all councils and specific attributes that make up statistics.
Uses a cursor to create a dynamic cross tab query (whcih in turn is calling a the UDF fn_AttributeCrossTab)
HISTORY:
Date: Author: Description:
2006-07-17 Barry Halper Created
\************************************************/
ALTER PROCEDURE sp_GetCouncilsStats
-- todo: add input variables
--
-- (
-- @StatType varchar (50),
-- @CouncilTypeID varchar (50),
-- @CountryID varchar (50)
-- )
--
AS
SET CONCAT_NULL_YIELDS_NULL OFF
/* SET LOCAL VARS TO BE USED IN CURSOR */
DECLARE
@AttributeName varchar(50),
@SQLSelect varchar(75),
@SQLFrom varchar(75),
@SQLCrosstab varchar(1500),
@SQL varchar(2000)
/*Set SQL 'SELECT' and 'FROM' statments into string vars*/
SET @SQLSELECT = 'SELECT DISTINCT O.Organisation, O.OrganisationID '
SET @SQLFrom = ' FROM Organisation O WHERE O.OrganisationTypeID=5'
/*declare cursor to scroll through Attribute table */
DECLARE varCurs CURSOR FOR
SELECT Attribute
FROM Attribute
WHERE AttributeID IN(128,129,130) /*todo: change this to input variable*/
OPEN varCurs
FETCH varCurs INTO @AttributeName
--begin loop
WHILE (@@fetch_status=0)
BEGIN
/*create a column in sql stament for each iteration of cursor: each iteration calls udf to prduce cross tab*/
SET @SQLCrosstab = @SQLCrosstab + ', ' +
'dbo.fn_AttributeCrossTab(O.OrganisationID, ''' + @AttributeName + ''') AS ' + '[' + @AttributeName + ']'
FETCH varCurs INTO @AttributeName
END
/*Concatenate SQl strings into 1 string var*/
SET @SQL = @SQLSELECT + @SQLCrosstab + @SQLFrom
--PRINT (@SQL)
/* execute sql statement*/
EXEC (@SQL)
CLOSE varCurs
DEALLOCATE varCurs