Guest User

Untitled

a guest
Nov 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. final SessionImpl sess = (SessionImpl) this.getSessionFactory().openSession();
  2. try (Connection conn = sess.connection(); Statement st = conn.createStatement();) {
  3. final NamedParameterPreparedStatement ps = NamedParameterPreparedStatement.createNamedParameterPreparedStatement(conn, aStringWithTheRequestBelow);
  4. // ... set parameters here ...
  5. ps.setQueryTimeout(15);
  6. final ResultSet rs = ps.executeQuery();
  7. } catch (final SQLException ex) {
  8. LOG.error("parameter 1 :" + parameter1 + "etc.");
  9. } finally {
  10. sess.close();
  11. }
  12.  
  13. --DECLARE @date as DATE = '2017-09-03'; -- one of the input parameter set up from java.
  14.  
  15.  
  16. IF OBJECT_ID('tempdb.dbo.#MATCHINGDAYS718567154','U') is not null
  17. DROP TABLE #MATCHINGDAYS718567154; -- RANDOM TOKEN HERE TO AVOID CONCURRENCY
  18.  
  19. WITH startDateTable AS (
  20. SELECT TOP 1 a.dateStart
  21. FROM someSelection
  22. ),
  23. endDateTable AS (
  24. SELECT TOP 1 endDate
  25. FROM anotherSelection
  26. ),
  27. AllDays AS (
  28. SELECT myFunc_getMaxDate(DATEADD(DAY,1,startDateTable.dateStart), DATEADD(dd, -30,@date))AS [Date]
  29. FROM startDateTable
  30. UNION ALL
  31. SELECT
  32. DATEADD(DAY, 1, [Date])
  33. from AllDays
  34. join endDateTable on 1=1
  35. WHERE [Date] < myFunc_getMinDate(DATEADD(dd, +7, @date),endDateTable.endDate))
  36.  
  37.  
  38. -- build a temporary table with all days from startDate to endDate the recursive way
  39. -- with a min -30 days before input date and 7 days after output date
  40. SELECT [Date]
  41. INTO #MATCHINGDAYS718567154
  42. FROM AllDays
  43. OPTION (MAXRECURSION 37)
  44.  
  45. SELECT
  46. manyFields
  47.  
  48. from MainTable
  49. -- various joins
  50. join #MATCHINGDAYS718567154 as MD on 1 = 1
  51. where 1 = 1
  52. and -- etc... many clauses including some computed over MATCHINGDAYS718567154
  53.  
  54. order by someField
  55.  
  56. DROP TABLE #MATCHINGDAYS718567154
Add Comment
Please, Sign In to add comment