问题
For one reason or other which are out of my control I am attempting to simply pull data over the past 12 months. However, essentially down to the size of data, I have to query each day into a temp table and go from there. Now I'm a newbie to scripting in DB2, but not SQL in general, so I've tried the code below (the logic seems fine to me).
Initially I'm was just interested in how many records will be generated, but ideally I'd want to run the second SELECT code. I've been using Data Studio, but I believe to export the data I would need to run this via CLP. Now my issues are that I'm missing something within syntax that I can't figure out and it's doing my head in. It's most likely something very basic or I'm just doing it totally wrong.
If I try the SELECT COUNT(*) code I'm getting these errors: - Multiple markers at this line - DB2 for Linux, UNIX, and Windows: "" was expected after "FROM". - DB2 for Linux, UNIX, and Windows: "." is invalid.
If I try the other SELECT code I'm getting these errors: - DB2 for Linux, UNIX, and Windows: "" was expected instead of "SELECT MARKET_ID, SUPER_REGION, REGION, MARKET, POSA, DEST_ID, DEST_NAME, DEST_TYPE, STAT... DB2 for Linux, UNIX, and Windows: "," was expected after "FROM". DB2 for Linux, UNIX, and Windows: "," was expected after "GROUP"
I just don't get it. Can someone please help? The SESSION.l12_Dest table also doesn't seem to be available afterwards for me to try looking at the table manually.
Code: -
--<ScriptOptions statementTerminator="@"/>
CREATE OR REPLACE PROCEDURE HWW.DM_CHECKLIST()
LANGUAGE SQL
BEGIN
DECLARE GLOBAL TEMPORARY TABLE SESSION.L12_DEST
(
ACTUAL_DATE DATE,
MARKET_ID INTEGER,
SUPER_REGION VARCHAR (100),
REGION VARCHAR (100),
MARKET VARCHAR (100),
POSA VARCHAR (100),
DEST_ID INTEGER,
DEST_NAME VARCHAR (100),
DEST_TYPE VARCHAR (30),
STATUS_CODE SMALLINT,
VISITORS INTEGER
)
ON COMMIT PRESERVE ROWS NOT LOGGED;
COMMIT;
FOR V_ROW AS
SELECT ACTUAL_DATE
FROM DM.DATE_DIM
WHERE ACTUAL_DATE
BETWEEN (CURRENT_DATE - 12 MONTHS) - DAY((CURRENT_DATE - 12 MONTHS)) DAYS + 1 DAYS
AND LAST_DAY((CURRENT_DATE - 1 MONTHS))
DO
INSERT INTO SESSION.L12_DEST
SELECT B.ACTUAL_DATE,
Z.HCOM_SRCH_DEST_PROPERTY_MKT_ID,
Z.HCOM_SRCH_DEST_PROPERTY_MKT_SUPER_REGN_NAME,
Z.HCOM_SRCH_DEST_PROPERTY_MKT_REGN_NAME,
Z.HCOM_SRCH_DEST_PROPERTY_MKT_NAME,
S.SITE_CNTRY_NAME,
Z.HCOM_SRCH_DEST_ID,
Z.HCOM_SRCH_DEST_NAME,
Z.HCOM_SRCH_DEST_TYP_NAME,
LZ.STATUS_CODE,
COUNT(DISTINCT(F.VISITOR_KEY))AS VISITORS
FROM DM.LODG_DEMAND_FACT F
INNER JOIN DM.V_HCOM_SRCH_DEST_DIM Z
ON F.HCOM_SRCH_DEST_KEY = Z.HCOM_SRCH_DEST_KEY
INNER JOIN DM.DATE_DIM B
ON F.LOCAL_DEMAND_DATE_KEY = B.DATE_KEY
INNER JOIN DM.SITE_DIM S
ON S.SITE_KEY = F.SITE_KEY
LEFT JOIN LZ.LZ_HCM_DESTINATION LZ
ON Z.HCOM_SRCH_DEST_ID = LZ.DESTINATION_INT_ID
WHERE F.BRAND_KEY = 101
AND B.ACTUAL_DATE = V_ROW.ACTUAL_DATE
GROUP BY B.ACTUAL_DATE,
Z.HCOM_SRCH_DEST_PROPERTY_MKT_ID,
Z.HCOM_SRCH_DEST_PROPERTY_MKT_SUPER_REGN_NAME,
Z.HCOM_SRCH_DEST_PROPERTY_MKT_REGN_NAME,
Z.HCOM_SRCH_DEST_PROPERTY_MKT_NAME,
S.SITE_CNTRY_NAME,
Z.HCOM_SRCH_DEST_ID,
Z.HCOM_SRCH_DEST_NAME,
Z.HCOM_SRCH_DEST_TYP_NAME,
LZ.STATUS_CODE;
END FOR;
--SELECT COUNT(*) FROM SESSION.L12_DEST;
--EXPORT TO C:\TEMP\MARKETS.TXT OF DEL
SELECT MARKET_ID,
SUPER_REGION,
REGION,
MARKET,
POSA,
DEST_ID,
DEST_NAME,
DEST_TYPE,
STATUS_CODE,
SUM(VISITORS)
FROM SESSION.L12_DEST
GROUP BY MARKET_ID,
SUPER_REGION,
REGION,
MARKET,
POSA,
DEST_ID,
DEST_NAME,
DEST_TYPE,
STATUS_CODE;
END @
回答1:
I don't think you can do a bare select statement in a stored proc (at least in DB2).
When I try this I get the same results: the most unhelpful error message ever. (thanks IBM)
But, if I put some valid statement after the END FOR;
there isn't a problem.
If you want a stored proc to do a select and show you the answer do something like this:
CREATE OR REPLACE PROCEDURE DM_CHECKLIST()
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
DECLARE RS CURSOR FOR SELECT COUNT(*) FROM sysibm.sysdummy1;
OPEN RS;
END@
The interesting thing is the DYNAMIC RESULT SETS 1
business. That tells the executor that we will have a result set returned and to show it in the command line interface (cli).
You can have as many result sets as you like. You will see the results on the cli, or you can get them pragmatically.
I did try the EXPORT
command and it didn't seem to work.
EXPORT TO "C:\TEMP\MARKETS.TXT" OF DEL SELECT COUNT(*) FROM sysibm.sysdummy1;
It looks like you aren't allowed to use EXPORT
in a stored proc, as it is a DB2 command and not a SQL statement. I wouldn't be surprised if there is some way to do it, but not this way.
回答2:
To use the EXPORT utility in a stored procedure, you have to wrap it in the ADMIN_CMD procedure:
create procedure dm_checklist()
language sql
begin
declare global temporary table results (...)
on commit preserve rows;
-- Steps to build / populate your temporary table
call sysproc.admin_cmd('export to C:\file.del of del select * from session.results');
end
来源:https://stackoverflow.com/questions/14964538/db2-9-7-sql-syntax-what-am-i-doing-wrong