我認爲一個動態的過程/宏將最適合您的需求。 編譯以下程序:
REPLACE PROCEDURE your_proc (IN in_mydate DATE)
READS SQL DATA
DYNAMIC RESULT SETS 1
BEGIN
DECLARE stmt_str VARCHAR(60000);
DECLARE RESPONSE CURSOR WITH RETURN ONLY TO CLIENT FOR STMT;
-----------------------------------------------------------------------------------------------
-------------------------------- PROGRAM LOGIC ------------------------------------------------
-----------------------------------------------------------------------------------------------
SET stmt_str = 'SELECT ''A'' AS "Table", CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END AS "True?"'
||'WHERE EXISTS (SELECT * FROM A WHERE theDate='''||in_mydate||''')'
||'UNION ALL'
||'SELECT ''B'', CASE WHEN COUNT(*) = 1 THEN ''Y'' ELSE ''N'' END'
||'WHERE EXISTS (SELECT * FROM B WHERE theDate='''||in_mydate||''') '
||'UNION ALL'
||'SELECT ''C'', CASE WHEN COUNT(*) = 1 THEN ''Y'' ELSE ''N'' END'
||'WHERE EXISTS (SELECT * FROM C WHERE theDate='''||in_mydate||''');';
PREPARE STMT FROM stmt_str;
OPEN RESPONSE;
DEALLOCATE PREPARE STMT;
END;
在這之後你調用語句只是把你的約會(作爲輸入參數)。輸出將是您所需的選擇查詢的結果。
如果您使用的是宏下面的代碼會給你的輸出:
REPLACE MACRO your_macro (mydate DATE)
AS (
SELECT 'A' AS "Table", CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END AS "True?"
WHERE EXISTS (SELECT * FROM A WHERE theDate=:myDate)
UNION ALL
SELECT 'B', CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END
WHERE EXISTS (SELECT * FROM B WHERE theDate=:myDate)
UNION ALL
SELECT 'C', CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END
WHERE EXISTS (SELECT * FROM C WHERE theDate=:myDate);
);
感謝