介紹一個滿特別的SQL 語法
(此為鼎新 TipTop的4GL程式碼)
這個SQL主要是在數張核價單資訊中,取得今日之前的最後核價單據(依生效日pmj09來判斷)
若有同一天核價的單據,即取最大的核價單單號(pmi01)
1 LET l_sql = " SELECT pmj01,pmj02,pmi06 ",
2 " FROM pmi_file,pmj_file ",
3 " WHERE pmi01=pmj01 AND (pmi01,pmi03,pmj03,pmj05,pmj09) IN ( ",
4 " SELECT max(pmi01),pmi03,pmj03,pmj05,pmj09 FROM pmi_file,pmj_file ",
5 " WHERE pmi01=pmj01 AND (pmi03,pmj03,pmj05,pmj09) IN ( ",
6 " SELECT pmi03,pmj03,pmj05,max(pmj09) from pmi_file,pmj_file ",
7 " WHERE pmi01=pmj01 ",
8 " AND pmiacti='Y' ",
9 " AND pmj09< ? "
10 IF g_sma.sma116='N' THEN LET l_sql=l_sql CLIPPED," AND pmi06='N' " END IF
11 LET l_sql=l_sql CLIPPED, " GROUP BY pmi03,pmj03,pmj05 ) ",
12 " AND pmiacti='Y' ",
13 " GROUP BY pmi03,pmj03,pmj05,pmj09 ) ",
14 " AND pmi03= ? AND pmj03=? AND pmj05 = ? "
15 PREPARE pmi_pre FROM l_sql
16 DECLARE pmi_f CURSOR FOR pmi_pre
17 OPEN pmi_f USING p_date,p_vender,p_part,p_curr
18 FETCH pmi_f INTO l_pmj01,l_pmj02,l_pmi06
19 IF l_pmi06='Y' THEN
20 LET l_price=null
21 ELSE
22 SELECT pmj07 INTO l_price
23 FROM pmj_file
24 WHERE pmj01=l_pmj01 AND pmj02=l_pmj02
25 END IF
2 " FROM pmi_file,pmj_file ",
3 " WHERE pmi01=pmj01 AND (pmi01,pmi03,pmj03,pmj05,pmj09) IN ( ",
4 " SELECT max(pmi01),pmi03,pmj03,pmj05,pmj09 FROM pmi_file,pmj_file ",
5 " WHERE pmi01=pmj01 AND (pmi03,pmj03,pmj05,pmj09) IN ( ",
6 " SELECT pmi03,pmj03,pmj05,max(pmj09) from pmi_file,pmj_file ",
7 " WHERE pmi01=pmj01 ",
8 " AND pmiacti='Y' ",
9 " AND pmj09< ? "
10 IF g_sma.sma116='N' THEN LET l_sql=l_sql CLIPPED," AND pmi06='N' " END IF
11 LET l_sql=l_sql CLIPPED, " GROUP BY pmi03,pmj03,pmj05 ) ",
12 " AND pmiacti='Y' ",
13 " GROUP BY pmi03,pmj03,pmj05,pmj09 ) ",
14 " AND pmi03= ? AND pmj03=? AND pmj05 = ? "
15 PREPARE pmi_pre FROM l_sql
16 DECLARE pmi_f CURSOR FOR pmi_pre
17 OPEN pmi_f USING p_date,p_vender,p_part,p_curr
18 FETCH pmi_f INTO l_pmj01,l_pmj02,l_pmi06
19 IF l_pmi06='Y' THEN
20 LET l_price=null
21 ELSE
22 SELECT pmj07 INTO l_price
23 FROM pmj_file
24 WHERE pmj01=l_pmj01 AND pmj02=l_pmj02
25 END IF
剛接觸到這個系統時,對於其資料庫檔案命名的方式很不能理解
(都是以文字 XXX_FILE來做為檔案名稱,所有欄位皆以XXX為首,再以數字序號編列)
從欄位名稱中無法得知欄位的性質及內容,亦無法在資料庫中進行PK及NULL的設置
一進到database 會有一種迷路的感覺...
而且user.tablename 都是public的
因此,若想要從資料庫去整理這個系統的架構,是一件很痛苦的事情~
但是這種命名方式在進行程式設計時有很大的幫助。
不僅是欄位與畫面的MAPPing以及各程式間的資料流向,都能夠很快地找到。
從我入行到現在,接觸過很多很多不同的系統,以及編程方式。
即使是相同的語言,在不同的程式設計及架構下,有不一樣的個性及寫法。
我還滿喜歡去解析別人的系統。
任何設計,都有程式設計師的個性。不同時代的影子。
而架構師的功力,也在這兒窺見。