Category=Database Maintenance - CAUTION
Name=08b. Generate- Pages for Empty Documents - Link to Shelf Instance
Description=Link Generated pages to a shelf instance if possible

Heading=Put Generated Pages Into SHELFINSTANCES
OutputLevel=999
ImportSQL=SELECT	\
	\
CAST(SUBSTR(USERDATA,1,5) AS SIGNED) AS DOCUMENTID,		\
SHELFID,		\
(LEAVES * 2) - (select count(*) FROM SHELFINSTANCEPAGES SIP WHERE SIP.SHELFID=T1.SHELFID)		\
	\
FROM SHELFINSTANCE T1 WHERE NOT EXISTS (SELECT * FROM SHELFINSTANCEPAGES T2 WHERE T2.SHELFID = T1.SHELFID)|	\
[1..{2}]INSERT INTO SHELFINSTANCEPAGES (	\
		\
DOCUMENTID,	\
PAGEID,		\
SHELFID,	\
FOLIONUMBER,	\
SORTORDER)	\
		\
SELECT		\
		\
{1},		\
COALESCE(MIN(PAGEID), 0),	\
{2},		\
'',		\
cast(lpad(COALESCE(MIN(PAGEID), 0), 5, '0') as char)		\
		\
FROM PAGE P WHERE DOCUMENTID={1} AND NOT EXISTS (SELECT 1 FROM SHELFINSTANCEPAGES SIP WHERE SIP.DOCUMENTID=P.DOCUMENTID AND SIP.PAGEID=P.PAGEID)

IgnoreException=Error Code: 1146
