SQL generation code for cache table