常用SQL语句3(MSSQL)

  1 --IF EXISTS FUNCTION fn_GET_PRESENT_STS THEN DORP IT OR CREATE IT
  2 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fn_GET_PRESENT_STS‘)
  3     DROP FUNCTION [dbo].[fn_GET_PRESENT_STS]
  4 GO
  5
  6 CREATE FUNCTION fn_GET_PRESENT_STS(@p_status INT)
  7 RETURNS VARCHAR(60)
  8 AS
  9 BEGIN
 10 DECLARE @l_rtn varchar(60)
 11 SET
 12   @l_rtn=CASE
 13       WHEN @p_status=0 THEN ‘Rejected‘
 14       WHEN @p_status=1 THEN ‘Accepted‘
 15       WHEN @p_status=2 THEN ‘Pending‘
 16       WHEN @p_status=3 THEN ‘Submitting‘
 17       WHEN @p_status=4 THEN ‘Error‘
 18       WHEN @p_status=5 THEN ‘Cleared‘
 19       WHEN @p_status=6 THEN ‘Clearing‘
 20       ELSE ‘N/A‘
 21       END
 22   RETURN @l_rtn
 23 END
 24 GO
 25
 26 --IF EXISTS FUNCTION fn_GET_ACC_VERIFY_STS THEN DORP IT OR CREATE IT
 27 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fn_GET_ACC_VERIFY_STS‘)
 28     DROP FUNCTION [dbo].[fn_GET_ACC_VERIFY_STS]
 29 GO
 30
 31 CREATE FUNCTION fn_GET_ACC_VERIFY_STS(@acc_status INT)
 32 RETURNS VARCHAR(60)
 33 AS
 34 BEGIN
 35 DECLARE @l_rtn varchar(60)
 36 SET
 37   @l_rtn=CASE
 38       WHEN @acc_status=0 THEN ‘Mismatched‘
 39       WHEN @acc_status=1 THEN ‘Matched‘
 40       WHEN @acc_status=2 THEN ‘Undetermined‘
 41       WHEN @acc_status=3 THEN ‘Rejected‘
 42       WHEN @acc_status=4 THEN ‘Pending‘
 43       ELSE ‘N/A‘
 44       END
 45   RETURN @l_rtn
 46 END
 47 GO
 48
 49 --IF EXISTS FUNCTION fn_GET_CRD_STS THEN DORP IT OR CREATE IT
 50 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fn_GET_CRD_STS‘)
 51     DROP FUNCTION [dbo].[fn_GET_CRD_STS]
 52 GO
 53
 54 CREATE FUNCTION fn_GET_CRD_STS(@crd_status INT)
 55 RETURNS VARCHAR(60)
 56 AS
 57 BEGIN
 58 DECLARE @l_rtn varchar(60)
 59 SET
 60   @l_rtn=CASE
 61       WHEN @crd_status=0 THEN ‘Not Granted‘
 62       WHEN @crd_status=1 THEN ‘Granted‘
 63       WHEN @crd_status=2 THEN ‘Reverted‘
 64       WHEN @crd_status=3 THEN ‘Error‘
 65       WHEN @crd_status=4 THEN ‘Submitting‘
 66       ELSE ‘N/A‘
 67       END
 68   RETURN @l_rtn
 69 END
 70 GO
 71
 72 --IF EXISTS FUNCTION fn_GET_CHANNEL THEN DORP IT OR CREATE IT
 73 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fn_GET_CHANNEL‘)
 74     DROP FUNCTION [dbo].[fn_GET_CHANNEL]
 75 GO
 76
 77 CREATE FUNCTION fn_GET_CHANNEL(@cust_1 VARCHAR(10), @p_tp INT )
 78 RETURNS VARCHAR(60)
 79 AS
 80 BEGIN
 81 DECLARE @l_rtn varchar(60)
 82 SET
 83   @l_rtn=CASE
 84       WHEN @cust_1=‘IB‘ THEN ‘iBanking‘
 85       WHEN @cust_1=‘FC‘ THEN ‘FCDB‘
 86       WHEN @cust_1=‘CW‘ THEN ‘Corp Website‘
 87       WHEN @cust_1 IS NULL AND @p_tp=2 THEN ‘HKICL Portal‘
 88       ELSE @cust_1
 89       END
 90   RETURN @l_rtn
 91 END
 92 GO
 93
 94 --IF EXISTS FUNCTION fn_GET_GROUP THEN DORP IT OR CREATE IT
 95 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fn_GET_GROUP‘)
 96     DROP FUNCTION [dbo].[fn_GET_GROUP]
 97 GO
 98
 99 CREATE FUNCTION fn_GET_GROUP(@cust_2 VARCHAR(20))
100 RETURNS VARCHAR(60)
101 AS
102 BEGIN
103 DECLARE @l_rtn varchar(60)
104 SET
105   @l_rtn=CASE
106       WHEN @cust_2=‘PR‘ THEN ‘Personal‘
107       WHEN @cust_2=‘BB‘ THEN ‘Business Banking‘
108       WHEN @cust_2=‘CP‘ THEN ‘Corporate‘
109       ELSE @cust_2
110       END
111   RETURN @l_rtn
112 END
113 GO
114
115 --IF EXISTS FUNCTION fn_GET_PRODUCT THEN DORP IT OR CREATE IT
116 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fn_GET_PRODUCT‘)
117     DROP FUNCTION [dbo].[fn_GET_PRODUCT]
118 GO
119
120 CREATE FUNCTION fn_GET_PRODUCT(@cust_3 VARCHAR(20))
121 RETURNS VARCHAR(60)
122 AS
123 BEGIN
124 DECLARE @l_rtn varchar(60)
125 SET
126   @l_rtn=CASE
127       WHEN @cust_3=‘CASA‘ THEN ‘CASA‘
128       WHEN @cust_3=‘CC‘ THEN ‘Credit Card‘
129       WHEN @cust_3=‘LOAN‘ THEN ‘Loans‘
130       WHEN @cust_3=‘INSU‘ THEN ‘Insurance‘
131       ELSE @cust_3
132       END
133   RETURN @l_rtn
134 END
135 GO
136
137 --IF EXISTS FUNCTION fn_GET_RETURN_REASON DORP IT OR CREATE IT
138 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fn_GET_RETURN_REASON‘)
139     DROP FUNCTION [dbo].[fn_GET_RETURN_REASON]
140 GO
141
142 CREATE FUNCTION fn_GET_RETURN_REASON(@present_val INT)
143 RETURNS VARCHAR(100)
144 AS
145 BEGIN
146 DECLARE @l_rtn varchar(100)
147 SET
148   @l_rtn=CASE
149       WHEN @present_val=1 THEN ‘Refer To Drawer (‘ + cast(@present_val as varchar(50)) + ‘)‘
150       WHEN @present_val=3 THEN ‘Drawn against uncollected funds (‘ +cast(@present_val as varchar(50)) + ‘)‘
151       WHEN @present_val=4 THEN ‘Payer Account Closed (‘ + cast(@present_val as varchar(50)) + ‘)‘
152       WHEN @present_val=6 THEN ‘e-Cheque Is Out-of-date (‘ + cast(@present_val as varchar(50)) + ‘)‘
153       WHEN @present_val=7 THEN ‘Post-dated e-Cheque (‘ + cast(@present_val as varchar(50)) + ‘)‘
154       WHEN @present_val=8 THEN ‘Payment Countermanded By The Drawer (‘ + cast(@present_val as varchar(50)) + ‘)‘
155       WHEN @present_val=26 THEN ‘Not Drawn On This Bank (‘ + cast(@present_val as varchar(50)) + ‘)‘
156       WHEN @present_val=27 THEN ‘Others (‘ + cast(@present_val as varchar(50)) + ‘)‘
157       WHEN @present_val=28 THEN ‘Duplicated Presentment (‘ + cast(@present_val as varchar(50)) + ‘)‘
158       WHEN @present_val=29 THEN ‘Invalid e-Cheque (‘ + cast(@present_val as varchar(50)) + ‘)‘
159       WHEN @present_val=30 THEN ‘e-Cheque Already Drawn (‘ + cast(@present_val as varchar(50)) + ‘)‘
160       WHEN @present_val=51 THEN ‘Invalid Payee Account Or Unsupported Currency (‘ +cast(@present_val as varchar(50)) + ‘)‘
161       WHEN @present_val=52 THEN ‘Payee Account Not Exists (‘ + cast(@present_val as varchar(50)) + ‘)‘
162       WHEN @present_val=53 THEN ‘Invalid Payee (‘ + cast(@present_val as varchar(50)) + ‘)‘
163       WHEN @present_val=54 THEN ‘Invalid Payer Branch (‘ + cast(@present_val as varchar(50)) + ‘)‘
164       WHEN @present_val=55 THEN ‘Invalid Payer Account (‘ + cast(@present_val as varchar(50)) + ‘)‘
165       WHEN @present_val=56 THEN ‘Invalid Payee Bank (‘ + cast(@present_val as varchar(50)) + ‘)‘
166       WHEN @present_val=57 THEN ‘Invalid Payer Bank (‘ + cast(@present_val as varchar(50)) + ‘)‘
167       WHEN @present_val=58 THEN ‘e-Cheque Issued In This Currency Is Not Accepted By Payee Account (‘ + cast(@present_val as varchar(50)) + ‘)‘
168       WHEN @present_val=61 THEN ‘Invalid File Type (‘ + cast(@present_val as varchar(50)) + ‘)‘
169       WHEN @present_val=62 THEN ‘File Size Exceeded The Limits (‘ + cast(@present_val as varchar(50)) + ‘)‘
170       WHEN @present_val=63 THEN ‘Might Be A Fake e-Cheque (‘ + cast(@present_val as varchar(50)) + ‘)‘
171       WHEN @present_val=64 THEN ‘Invalid e-Cheque (‘ + cast(@present_val as varchar(50)) + ‘)‘
172       WHEN @present_val=65 THEN ‘Unsupported Currency (‘ + cast(@present_val as varchar(50)) + ‘)‘
173       WHEN @present_val=71 THEN ‘Presentment Already Accepted (‘ + cast(@present_val as varchar(50)) + ‘)‘
174       WHEN @present_val=72 THEN ‘Duplicated Presentment Reference (‘ + cast(@present_val as varchar(50)) + ‘)‘
175       WHEN @present_val=73 THEN ‘Invalid Presentment Reference (‘ + cast(@present_val as varchar(50)) + ‘)‘
176       WHEN @present_val=74 THEN ‘Presentment Return Already Accepted (‘ + cast(@present_val as varchar(50)) + ‘)‘
177       WHEN @present_val=75 THEN ‘Presentment Not Exists Or Not Accepted Previously (‘ + cast(@present_val as varchar(50)) + ‘)‘
178       WHEN @present_val=76 THEN ‘Presentment Already Submitted To Clearing (‘ + cast(@present_val as varchar(50)) + ‘)‘
179       WHEN @present_val=99 THEN ‘Please Contact Bank (‘ + cast(@present_val as varchar(50)) + ‘)‘
180       ELSE ‘null‘
181       END
182   RETURN @l_rtn
183 END
184 GO
185
186 --IF EXISTS FUNCTION fn_GET_ACTIVITY_NAME DORP IT OR CREATE IT
187 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fn_GET_ACTIVITY_NAME‘)
188     DROP FUNCTION [dbo].[fn_GET_ACTIVITY_NAME]
189 GO
190
191 CREATE FUNCTION fn_GET_ACTIVITY_NAME(@mod_act INT, @mod_id INT)
192 RETURNS VARCHAR(30)
193 AS
194 BEGIN
195 DECLARE @l_rtn varchar(30)
196 SET
197   @l_rtn=CASE
198       WHEN @mod_act=5 AND @mod_id <> 107 THEN ‘Edit‘
199       WHEN @mod_act=5 AND @mod_id=107 THEN ‘Presentment Update‘
200       WHEN @mod_act=6 THEN ‘Remove‘
201       WHEN @mod_act=12 THEN ‘Handle Case‘
202       WHEN @mod_act=18 THEN ‘Add/Edit‘
203       WHEN @mod_act=170 THEN ‘Handle Case‘
204       WHEN @mod_act=180 THEN ‘Presentment Update‘
205       ELSE STR(@mod_act)
206       END
207   RETURN @l_rtn
208 END
209 GO
210
211 --IF EXISTS FUNCTION fn_GET_ACT_STATUS DORP IT OR CREATE IT
212 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fn_GET_ACT_STATUS‘)
213     DROP FUNCTION [dbo].[fn_GET_ACT_STATUS]
214 GO
215
216 CREATE FUNCTION fn_GET_ACT_STATUS(@act_status INT)
217 RETURNS VARCHAR(30)
218 AS
219 BEGIN
220 DECLARE @l_rtn varchar(30)
221 SET
222   @l_rtn=CASE
223       WHEN @act_status=0 THEN ‘Fail‘
224       WHEN @act_status=1 THEN ‘Success‘
225       WHEN @act_status=2 THEN ‘Reject‘
226       WHEN @act_status=3 THEN ‘Approve‘
227       ELSE STR(@act_status)
228       END
229   RETURN @l_rtn
230 END
231 GO
232
233 --IF EXISTS FUNCTION fn_GET_MOD_NAME DORP IT OR CREATE IT
234 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fn_GET_MOD_NAME‘)
235     DROP FUNCTION [dbo].[fn_GET_MOD_NAME]
236 GO
237
238 CREATE FUNCTION fn_GET_MOD_NAME(@module INT)
239 RETURNS VARCHAR(30)
240 AS
241 BEGIN
242 DECLARE @l_rtn varchar(30)
243 SET
244   @l_rtn=CASE
245       WHEN @module=0 THEN ‘Name Matching Management‘
246       WHEN @module=1 THEN ‘Presentment Management‘
247       ELSE STR(@module)
248       END
249   RETURN @l_rtn
250 END
251 GO
252
253 --IF EXISTS FUNCTION fn_GET_STATUS DORP IT OR CREATE IT
254 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fn_GET_STATUS‘)
255     DROP FUNCTION [dbo].[fn_GET_STATUS]
256 GO
257
258 CREATE FUNCTION fn_GET_STATUS(@status INT)
259 RETURNS VARCHAR(30)
260 AS
261 BEGIN
262 DECLARE @l_rtn varchar(30)
263 SET
264   @l_rtn=CASE
265       WHEN @status=0 THEN ‘Suspend‘
266       WHEN @status=1 THEN ‘Active‘
267       WHEN @status=2 THEN ‘Locked‘
268       ELSE STR(@status)
269       END
270   RETURN @l_rtn
271 END
272 GO
273
274
275 --IF EXISTS VIEW PRE_ITM_DETAIL_VW THEN DORP IT OR CREATE IT
276 IF EXISTS(SELECT 1 FROM sys.views WHERE name=‘PRE_ITM_DETAIL_VW‘)
277 DROP VIEW PRE_ITM_DETAIL_VW
278 GO
279
280 CREATE VIEW PRE_ITM_DETAIL_VW
281 AS
282 SELECT
283     pi.P_PAYER_BANK_COD AS PAYER_BANK_CODE,
284     pi.P_PAYEE_BRANCH AS PAYER_BRANCH_CODE,
285     REPLACE(pi.P_PAYEE_ACC,‘-‘,‘‘) as PAYEE_ACC_NUM,
286     pi.P_CHQ_NUM AS CHQ_NUM,
287     pi.P_CUR AS CHQ_CURRENCY,
288     pi.P_NEXT_DAY AS NEXT_DAY,
289     pi.P_AMT AS AMOUNT,
290     pi.P_PRESENT_AT AS PERSENT_DATE,
291     pi.P_REF AS PRE_REF,
292     pi.P_BANK_REF AS BANK_REF,
293     [dbo].[fn_GET_PRESENT_STS](pi.P_PRESENT_STS) AS PRE_STATUS,
294     [dbo].[fn_GET_ACC_VERIFY_STS](pi.P_ACC_VERIFY_STS) AS ACC_VERIFY_STATUS,
295     [dbo].[fn_GET_GROUP](pi.P_CUST_2) AS CUSTOMER_GROUP,
296     [dbo].[fn_GET_PRODUCT](pi.P_CUST_3) AS PRODUCT,
297     [dbo].[fn_GET_CHANNEL](pi.P_CUST_1, pi.P_TP) AS CHANNEL,
298     [dbo].[fn_GET_RETURN_REASON](pi.P_PRESENT_VAL) AS EXCEPTION_REASON,
299     [dbo].[fn_GET_CRD_STS](pi.P_CRD_STS) AS TRANS_STATUS,
300     pi.P_CLEAR_DT AS CLEAR_PROCESS_DT,
301     pi.P_CREATE_AT AS DEPOSIT_TIME,
302     pi.P_EXP_CLEAR_DT AS EXP_CLEAR_DT
303 FROM
304     CHQ_PRE_ITM_TBL pi
305 GO
306
307 --IF EXISTS VIEW PRE_GROUP_SUMMARY_VW THEN DORP IT OR CREATE IT
308 IF EXISTS(SELECT 1 FROM sys.views WHERE name=‘PRE_GROUP_SUMMARY_VW‘)
309 DROP VIEW PRE_GROUP_SUMMARY_VW
310 GO
311
312 CREATE VIEW PRE_GROUP_SUMMARY_VW
313 AS
314 SELECT
315     v.CUSTOMER_GROUP, v.CHANNEL, v.PRESENTED, v.PENDING, v.ERROR, v.CLEARED, v.ACCEPTED, v.REJECTED, v.RETURNED, ACCEPTED+REJECTED+RETURNED as COUNTTOTAL,
316     v.ACCEPTEDAMOUNT, v.REJECTEDAMOUNT, v.RETURNEDAMOUNT, ACCEPTEDAMOUNT+REJECTEDAMOUNT+RETURNEDAMOUNT as AMOUNTTOTAL
317 FROM (
318     SELECT
319         pi.CUSTOMER_GROUP AS CUSTOMER_GROUP, pi.CHANNEL AS CHANNEL,
320         SUM(CASE WHEN pi.PRE_STATUS IN (‘Accepted‘, ‘Cleared‘, ‘Clearing‘) OR pi.PRE_STATUS = ‘Rejected‘ AND pi.PERSENT_DATE IS NOT NULL THEN 1 ELSE 0 end) AS PRESENTED,
321         SUM(CASE WHEN pi.PRE_STATUS = ‘Pending‘ THEN 1 ELSE 0 END) AS PENDING,
322         SUM(CASE WHEN pi.PRE_STATUS = ‘Accepted‘ THEN 1 ELSE 0 END) AS ACCEPTED,
323         SUM(CASE WHEN pi.PRE_STATUS = ‘Rejected‘ THEN 1 ELSE 0 END) AS REJECTED,
324         SUM(CASE WHEN pi.PRE_STATUS = ‘Error‘ THEN 1 ELSE 0 end) AS ERROR,
325         SUM(CASE WHEN pi.PRE_STATUS = ‘Cleared‘ THEN 1 ELSE 0 END) AS CLEARED,
326         SUM(CASE WHEN pi.PRE_STATUS = ‘Rejected‘ AND pi.CLEAR_PROCESS_DT IS NOT NULL THEN 1 ELSE 0 END) AS RETURNED,
327         SUM(CASE WHEN pi.PRE_STATUS = ‘Accepted‘ THEN pi.AMOUNT ELSE 0 END) AS ACCEPTEDAMOUNT,
328         SUM(CASE WHEN pi.PRE_STATUS = ‘Rejected‘ THEN pi.AMOUNT ELSE 0 END) AS REJECTEDAMOUNT,
329         SUM(CASE WHEN pi.PRE_STATUS = ‘Rejected‘ AND pi.CLEAR_PROCESS_DT IS NOT NULL THEN pi.AMOUNT ELSE 0 END) AS RETURNEDAMOUNT
330     FROM PRE_ITM_DETAIL_VW pi
331     GROUP BY
332          pi.CUSTOMER_GROUP, pi.CHANNEL
333 ) v
334 GO
335
336 --IF EXISTS VIEW PRE_PRODUCT_SUMMARY_VW THEN DORP IT OR CREATE IT
337 IF EXISTS(SELECT 1 FROM sys.views WHERE name=‘PRE_PRODUCT_SUMMARY_VW‘)
338 DROP VIEW PRE_PRODUCT_SUMMARY_VW
339 GO
340
341 CREATE VIEW PRE_PRODUCT_SUMMARY_VW
342 AS
343 SELECT
344     pi.PRODUCT AS PRODUCT,
345     pi.CUSTOMER_GROUP AS CUSTOMER_GROUP,
346     SUM(CASE WHEN pi.PRE_STATUS IN (‘Accepted‘, ‘Cleared‘, ‘Clearing‘) OR pi.PRE_STATUS = ‘Rejected‘ AND pi.PERSENT_DATE IS NOT NULL THEN 1 ELSE 0 END) AS PRESENTED,
347     SUM(CASE WHEN pi.PRE_STATUS = ‘Pending‘ THEN 1 ELSE 0 END) AS PENDING,
348     SUM(CASE WHEN pi.PRE_STATUS = ‘Accepted‘ THEN 1 ELSE 0 END) AS ACCEPTED,
349     SUM(CASE WHEN pi.PRE_STATUS = ‘Rejected‘ THEN 1 ELSE 0 END) AS REJECTED,
350     SUM(CASE WHEN pi.PRE_STATUS = ‘Error‘ THEN 1 ELSE 0 END) AS ERROR,
351     SUM(CASE WHEN pi.PRE_STATUS = ‘Cleared‘ THEN 1 ELSE 0 END) AS CLEARED,
352     SUM(CASE WHEN pi.PRE_STATUS = ‘Rejected‘ AND pi.CLEAR_PROCESS_DT IS NOT NULL THEN 1 ELSE 0 END) AS RETURNED
353 FROM PRE_ITM_DETAIL_VW pi
354 GROUP BY
355     pi.CUSTOMER_GROUP, pi.PRODUCT
356 GO
357
358 --IF EXISTS VIEW AUDIT_TRIAL_VW THEN DORP IT OR CREATE IT
359 IF EXISTS(SELECT 1 FROM sys.views WHERE name=‘AUDIT_TRIAL_VW‘)
360 DROP VIEW AUDIT_TRIAL_VW
361 GO
362
363 CREATE VIEW AUDIT_TRIAL_VW
364 AS
365 SELECT
366     REPLACE(lg.ACCOUNT_NO, ‘-‘, ‘‘) as ACCOUNT_NO,
367     [dbo].[fn_GET_MOD_NAME](lg.MODULE) AS MODULE,
368     [dbo].[fn_GET_ACTIVITY_NAME](lg.ACTIVITY, lg.MODULE) AS ACTIVITY,
369     [dbo].[fn_GET_ACT_STATUS](lg.STATUS) AS STATUS,
370     lg.DETAILS, lg.MAKER_ID, lg.CHECKER_ID,
371     lg.MAINTENANCE_DATE AS MAINTENANCE_DATE
372 FROM
373 (
374     SELECT
375       P_PAYEE_ACC AS ACCOUNT_NO,
376       MOD_ID AS MODULE,
377       ACT_COD AS ACTIVITY,
378       ACT_RESULT AS STATUS,
379       LOG_DTL AS DETAILS,
380       ADM_NM AS MAKER_ID,
381       NULL AS CHECKER_ID,
382       LOG_CREATE_AT AS MAINTENANCE_DATE
383     FROM
384     (
385         (SELECT * FROM BOA_ADM_LOG_TBL WHERE MOD_ID = 108 AND ACT_COD = 5) boa_admin JOIN CHQ_PRE_ITM_TBL chq ON (chq.P_REF = boa_admin.OBJ_ID)
386     )
387     UNION ALL
388     (
389         SELECT
390             OBJ_ID AS ACCOUNT_NO,
391             MOD_ID AS MODULE,
392             ACT_COD AS ACTIVITY,
393             ACT_RESULT AS STATUS,
394             LOG_DTL AS DETAILS,
395             ADM_NM AS MAKER_ID,
396             NULL AS CHECKER_ID,
397             LOG_CREATE_AT AS MAINTENANCE_DATE
398         FROM
399             BOA_ADM_LOG_TBL
400         WHERE
401             MOD_ID = 107 AND ACT_COD IN (6, 12, 18)
402     )
403     UNION ALL
404         SELECT
405             P_PAYEE_ACC AS ACCOUNT_NO,
406             MOD_ID AS MODULE,
407             MOD_ACT AS ACTIVITY,
408             case when CHK_ACT = 0 then 2 when CHK_ACT = 1 then 3 else 4 end AS STATUS,
409             MAK_DESC AS DETAILS,
410             MAK_ADM_NM AS Maker_ID,
411             CHK_ADM_NM AS Checker_ID,
412             CHK_HND_AT AS MAINTENANCE_DATE
413         FROM
414           (
415               (SELECT * FROM BOA_MAK_CHK_ARC_TBL WHERE MOD_ID = 108 AND MOD_ACT = 180) maker_checker join CHQ_PRE_ITM_TBL chq ON (chq.P_REF = maker_checker.OBJ_ID)
416           )
417     UNION ALL
418     (
419         SELECT
420             OBJ_ID as ACCOUNT_NO,
421             MOD_ID as MODULE,
422             MOD_ACT as ACTIVITY,
423             case when CHK_ACT = 0 then 2 when CHK_ACT = 1 then 3 else 4 end as STATUS,
424             MAK_DESC as DETAILS,
425             MAK_ADM_NM as Maker_ID,
426             CHK_ADM_NM as Checker_ID,
427             CHK_HND_AT as MAINTENANCE_DATE
428         FROM
429             BOA_MAK_CHK_ARC_TBL
430         WHERE
431             MOD_ID = 107 AND MOD_ACT in (170, 171, 172)
432     )
433 ) lg
434 GO
435
436 --IF EXISTS VIEW BOA_USR_TEMP_VW THEN DORP IT OR CREATE IT
437 IF EXISTS(SELECT 1 FROM sys.views WHERE name=‘BOA_USR_TEMP_VW‘)
438 DROP VIEW BOA_USR_TEMP_VW
439 GO
440
441 CREATE VIEW BOA_USR_TEMP_VW
442 AS
443 SELECT
444     adm.ADM_ID USR_ID,
445     adm.ADM_NM USER_NAME,
446     adm.ADM_EMAIL EMAIL,
447     adm.ADM_STS STATUS,
448     grp.GRP_NM AUTHORITY,
449     adm.ADM_CREATE_AT CREATE_DATE,
450     adm.ADM_UPD_AT UPDATE_DATE
451 FROM
452     BOA_ADM_TBL adm
453 JOIN
454     BOA_ADM_GRP_MAP_TBL mapt
455 ON
456     (adm.ADM_ID=mapt.ADM_ID)
457 JOIN
458     BOA_GRP_TBL grp
459 ON
460     (mapt.GRP_ID=grp.GRP_ID)
461 WHERE
462     adm.ADM_STS <> 9
463 GO
464
465 --SELECT * FROM BOA_USR_TEMP_VW GO
466 --IF EXISTS VIEW BOA_USR_VW THEN DORP IT OR CREATE IT
467 IF EXISTS(SELECT 1 FROM sys.views WHERE name=‘BOA_USR_VW‘)
468     DROP VIEW BOA_USR_VW
469 GO
470
471 CREATE VIEW BOA_USR_VW
472 AS
473 SELECT
474      B.USR_ID,
475     B.USER_NAME,
476     B.EMAIL,
477     [dbo].[fn_GET_STATUS](B.STATUS) STATUS,
478     B.CREATE_DATE,
479     B.UPDATE_DATE,
480     LEFT(AuthList,LEN(AuthList)-1) as AUTHORITY
481 FROM (
482     SELECT
483         USR_ID,
484         USER_NAME,
485         EMAIL,
486         STATUS,
487         CREATE_DATE,
488         UPDATE_DATE,
489         (SELECT AUTHORITY+‘,‘ FROM BOA_USR_TEMP_VW WHERE USER_NAME=A.USER_NAME FOR XML PATH(‘‘)) AS AuthList
490     FROM
491         BOA_USR_TEMP_VW A
492     GROUP BY
493         USR_ID,USER_NAME,EMAIL,STATUS,CREATE_DATE,UPDATE_DATE
494 ) B
495 GO
496
497 --SELECT * FROM BOA_USR_VW GO
498
499 --IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N‘[dbo].[fnTest]‘) AND xtype IN (N‘FN‘, N‘IF‘, N‘TF‘))
500 IF EXISTS (SELECT * FROM sysobjects WHERE xtype=‘fn‘ AND NAME=‘fnTest‘)
501     DROP FUNCTION [dbo].[fnTest]
502 GO
503
504 CREATE FUNCTION fnTest(@u int )
505 RETURNS varchar(60)
506 AS
507 BEGIN
508 DECLARE @uu varchar(60)
509 SET
510   @uu=CASE
511       WHEN @u=1 THEN ‘中国历史111‘
512       WHEN @u=0 THEN ‘中国历史000‘
513       ELSE ‘中国历史‘
514       END RETURN @uu
515 END
516 GO
517
518 SELECT
519     [dbo].[fnTest](stuId) as a,
520     stuName as b
521 FROM
522     STUDENT
523 GO
524
525 INSERT INTO student VALUES (1, 1, ‘张三‘, ‘爬山‘) GO
526 INSERT INTO student VALUES (2, 1, ‘张三‘, ‘爬山‘) GO
527 INSERT INTO student VALUES (3, 2, ‘李四‘, ‘打球‘) GO
528 INSERT INTO student VALUES (4, 2, ‘李四‘, ‘打球‘) GO
529 INSERT INTO student VALUES (5, 3, ‘王五‘, ‘游泳‘) GO
530 INSERT INTO student VALUES (6, 3, ‘王五‘, ‘游泳‘) GO
531
532 SELECT * FROM student GO
533
534 SELECT
535     B.stuName,
536     LEFT(StuList,LEN(StuList)-1) as hobby
537 FROM (
538     SELECT
539         stuName,
540         (SELECT hobby+‘,‘ FROM student WHERE stuName=A.stuName FOR XML PATH(‘‘)) AS StuList
541     FROM
542        student A
543     GROUP BY
544         stuName
545 ) B
546 GO
时间: 2024-10-15 01:51:34

常用SQL语句3(MSSQL)的相关文章

经典MSSQL语句大全和常用SQL语句命令的作用

下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL类型包括数据库.表的创建,修改,删除,声明—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML类型包括数据表中记录的查询,删除,修改,插入—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL类型包括数据库用户赋权,废除用户访问权限, 提交当前事务,中止当前事务—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,简要介绍基础语句: 1.说

Ubuntu 下安装sqlite3 及常用SQL 语句

安装sqlite3命令如下: 1 sudo apt-get install sqlite3 创建或者打开已有的数据库文件: 1 sqlite3 test.db 进入数据库后,可以进行以下常用SQL语句操作: CREATE TABLE ONT_USER_TABLE( ONT_USER_NAME text PRIMARY KEY, ONT_USER_PWD text NOT NULL, ONT_CREATE_TIME text ); INSERT INTO ONT_USER_TABLE values

常用sql语句及案例

目录 1)基本 2)数学函数 3)rownum 4)分页 5)时间处理 6)字符函数 7)to_number 8)聚合函数 9)学生选课 10)图书馆借阅 基本 --新建表: create table table1( id varchar(300) primary key, name varchar(200) not null); --插入数据 insert into table1 (id,name) values ('aa','bb'); --更新数据 update table1 set id

Oracle数据库常用Sql语句大全

一,数据控制语句 (DML) 部分 1.INSERT  (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, --) VALUES ( 值1, 值2, --); INSERT INTO 表名(字段名1, 字段名2, --)  SELECT (字段名1, 字段名2, --) FROM 另外的表名; 字符串类型的字段值必须用单引号括起来, 例如: 'GOOD DAY' 如果字段值里包含单引号' 需要进行字符串转换, 我们把它替换成两个单引号''. 字符串类型的字段值超过

oracle常用SQL语句(汇总版)

Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象 一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSERT INTO 表名(字段名1, 字段名2, ……) SELECT (字段名1, 字段名2, ……) FROM 另外的表名; 字符串类型的字段值必须用单引号括起来, 例如: ’GOOD DAY’ 如果字段值里包含单引号’ 需要

MySQL常用SQL语句(Python实现学生、课程、选课表增删改查)

以基本的学生选课为例,建立选课数据库,学生.班级.选课信息三张表,并分别对表进行插删改操作: import MySQLdb try: conn = MySQLdb.connect(host = 'localhost', user = 'root', passwd = 'root', db = 'xuanke', port = 3306) cur = conn.cursor() cur.execute("CREATE DATABASE xuanke") cur.execute("

mysql基础(二) 常用SQL语句

SQL语句类型:     DDL:数据库定义语言 create,drop,alter     DML:数据操作语言 insert,delete,update,select     DCL:数据控制语言 grant,revoke 常用SQL语句:     CREATE DATABASE #创建数据库 CREATE TABLE         #创建表 CREATE TABLE table_name(字段名,字段数据类型,约束条件) #创建表 CREATE INDEX            #创建索

学生表 课程表 成绩表 教师表常用SQL语句

学生表 课程表 成绩表 教师表 50个常用sql语句 建表 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 ---- If database exists the same name datatable deletes it. IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHE

Oracle 数据库常用sql语句及知识

<Oracle 数据库> 一.常用sql语句: 1.连接系统管理员账号:conn system/system(安装时口令); 2.创建新账号:create user Leo(账号名) identified by Leo(密码): 3.给新账号授权:grant connect,resource to Leo; 4.从某个账户收权:revoke resource from Leo; 5.连接新账号:conn Leo/Leo; 6.显示当前客户:show user; 7.查看表结构:desc s_e

学生表 课程表 成绩表 教师表 50个常用sql语句[转]

Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表   create table Student(S# varchar(20),Sname varchar(10),Sage int,Ssex varchar(2)) 前面加一列序号: if exists(select table_name from information_schema.tables wh