Project

General

Profile

Bug #754 » NEWspUtilSplitToTable7Column.sql

M Azid Wahyudi, 01/18/2022 02:41 PM

 
1
??USE [Bintang_Prod]
2

3
GO
4

5
/****** Object:  StoredProcedure [dbo].[spUtilSplitToTable7Column]    Script Date: 1/18/2022 2:30:09 PM ******/
6

7
SET ANSI_NULLS ON
8

9
GO
10

11
SET QUOTED_IDENTIFIER ON
12

13
GO
14

15
ALTER PROCEDURE [dbo].[spUtilSplitToTable7Column]
16

17
	@sInputList VARCHAR(MAX)
18

19
	, @sDelimiterRow VARCHAR(MAX)
20

21
	, @sDelimiterColumn VARCHAR(MAX)
22

23
AS
24

25

26

27
--declare @sInputList VARCHAR(MAX)='|T14. Company General Process,5,Level 03,,-3,0,0||T153. Organizational Development,10,Level 03,,-3,0,0||C1. Customer Focus,20,Level 03,,-3,0,0||C2. Awareness,20,Level 03,,-3,0,0||C3. Trust & Respect,20,Level 03,,-3,0,0||C4. Spirit,20,Level 03,,-3,0,0||C5. Team Work,20,Level 03,,-3,0,0||S1. Working Style (Self Confident, Concern for Order, & Initiative),25,Level 03,,-3,0,0||S2. Analytical Thinking,25,Level 03,,-3,0,0||T2. Kemampuan umum perkantoran (bahasa inggris dan administrasi),5,Level 03,,-3,0,0||S3. Interpersonal Understanding,25,Level 03,,-3,0,0||S4. Leadership (Planning & Monitoring, Decision Making, People Development),25,Level 03,,-3,0,0||T13. Bintang Product Knowledge,5,Level 03,,-3,0,0||T7. Negotiation,10,Level 03,,-3,0,0||T169. Performance Management,10,Level 03,,-3,0,0||T146. Strategic Orientation,5,Level 03,,-3,0,0||T1. Communication (Presentation),10,Level 03,,-3,0,0||T64. Industrial Relation,15,Level 03,,-3,0,0||T38. Risk Management,5,Level 03,,-3,0,0||T172. Asuransi,5,Level 03,,-3,0,0||T175. Digital,5,Level 03,,-3,0,0||T173. Manajemen Talenta/Talent Management,5,Level 03,,-3,0,0||T174. Strategik & Kebijakan SDM,5,Level 03,,-3,0,0'
28

29
--declare @sDelimiterRow VARCHAR(MAX)='|'
30

31
--declare @sDelimiterColumn VARCHAR(MAX)=','
32

33

34

35
	--Variable Declaration
36

37
	DECLARE @row_temp TABLE (item varchar(MAX))
38

39
	DECLARE @col_temp TABLE (
40

41
		col1 varchar(MAX),
42

43
		col2 varchar(MAX),
44

45
		col3 varchar(MAX),
46

47
		col4 varchar(MAX),
48

49
		col5 varchar(MAX),
50

51
		col6 varchar(MAX),
52

53
		col7 varchar(MAX)
54

55
	)
56

57
	DECLARE @row_count int, @col_pos int
58

59
	DECLARE @col1 varchar(MAX),
60

61
		@col2 varchar(MAX),
62

63
		@col3 varchar(MAX),
64

65
		@col4 varchar(MAX),
66

67
		@col5 varchar(MAX),
68

69
		@col6 varchar(MAX),
70

71
		@col7 varchar(MAX)
72

73
	
74

75
	--Processing Row
76

77
	INSERT INTO @row_temp
78

79
	SELECT item FROM dbo.fnUtilSplit(@sInputList, @sDelimiterRow)
80

81
	SELECT @row_count = COUNT(*) FROM @row_temp
82

83
	
84

85
	--Processing Column
86

87
	IF (@row_count > 0)
88

89
	BEGIN
90

91
		DECLARE cur CURSOR
92

93
		FOR SELECT item FROM @row_temp 
94

95
		
96

97
		DECLARE @cur_status int, @col_txt varchar(MAX), @col_txt_reverse varchar(MAX)
98

99
		SET @cur_status = 0
100

101
		
102

103
		OPEN cur
104

105

106

107
		WHILE @cur_status = 0
108

109
		BEGIN
110

111
			SET @col_txt = ''
112

113
			SET @col_txt_reverse = ''
114

115
			FETCH NEXT FROM cur INTO @col_txt
116

117
			SET @cur_status = @@FETCH_STATUS
118

119
			IF @cur_status = 0
120

121
			BEGIN
122

123
				SET @col_txt_reverse = REVERSE(@col_txt)
124

125
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
126

127
				SET @col6=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
128

129
				SET @col6 =  REVERSE(@col6)
130

131
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
132

133
				
134

135
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
136

137
				SET @col5=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
138

139
				SET @col5 =  REVERSE(@col5)
140

141
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
142

143

144

145
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
146

147
				SET @col4=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
148

149
				SET @col4 =  REVERSE(@col4)
150

151
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
152

153

154

155
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
156

157
				SET @col3=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
158

159
				SET @col3 =  REVERSE(@col3)
160

161
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
162

163

164

165
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
166

167
				SET @col2=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
168

169
				SET @col2 =  REVERSE(@col2)
170

171
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
172

173

174

175
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
176

177
				SET @col1=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
178

179
				SET @col1 =  REVERSE(@col1)
180

181
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
182

183

184

185
				INSERT INTO @col_temp VALUES( REVERSE(@col_txt_reverse), @col1, @col2, @col3, @col4, @col5, @col6)
186

187
				--INSERT INTO @col_temp VALUES( REVERSE(@col_txt_reverse), @col6, @col5, @col4, @col3, @col2, @col1)
188

189
			END
190

191
		END
192

193
				
194

195
		CLOSE cur
196

197
		DEALLOCATE cur
198

199
	END
200

201
	
202

203
	SELECT * FROM @col_temp
204

205

(4-4/4)