I've never created a stored procedure with temp tables in the past. I need to do this now because my query contains a temp table and must be in a stored procedure. Please help. Query is below. I have another query I also need to place in a stored procedure that has multiple temp tables. I don't want to do this until I know how to handle the temp tables. I will eventually use the stored procedure in the Infomaker reporting tool which will not accept my temp tables in the syntax. Thank you.
DECLARE @LASTATTEND TABLE
(
ID_NUM INT,
LAST_ATTEND_DTE DATE
)
INSERT INTO @LASTATTEND
SELECT ID_NUM,
MAX(LAST_DTE_OF_ATTEND)
FROM STUDENT_CRS_HIST
GROUP BY ID_NUM
SELECT C.ID_NUM,
N.LAST_NAME,
N.FIRST_NAME,
N.EMAIL_ADDRESS,
C.YR_CDE,
C.TRM_CDE,
C.PROG_CDE,
C.STAGE,
CAST (C.HIST_STAGE_DTE AS DATE) 'HIST_STAGE_DTE',
L.LAST_ATTEND_DTE,
C.CUR_CANDIDACY,
C.DIV_CDE,
C.LOCA_CDE,
C.CANDIDACY_TYPE
FROM CANDIDACY C, NAME_MASTER N, @LASTATTEND L
WHERE (C.YR_CDE = 2017 OR C.HIST_STAGE_DTE >= '01/01/17')
AND C.STAGE IN ('NTACC', 'ACCPT')
AND C.PROG_CDE = 'UND'
AND C.ID_NUM = N.ID_NUM
AND C.ID_NUM = L.ID_NUM
ORDER BY C.YR_CDE, C.TRM_CDE, N.LAST_NAME, N.FIRST_NAME
DECLARE @LASTATTEND TABLE
(
ID_NUM INT,
LAST_ATTEND_DTE DATE
)
INSERT INTO @LASTATTEND
SELECT ID_NUM,
MAX(LAST_DTE_OF_ATTEND)
FROM STUDENT_CRS_HIST
GROUP BY ID_NUM
SELECT C.ID_NUM,
N.LAST_NAME,
N.FIRST_NAME,
N.EMAIL_ADDRESS,
C.YR_CDE,
C.TRM_CDE,
C.PROG_CDE,
C.STAGE,
CAST (C.HIST_STAGE_DTE AS DATE) 'HIST_STAGE_DTE',
L.LAST_ATTEND_DTE,
C.CUR_CANDIDACY,
C.DIV_CDE,
C.LOCA_CDE,
C.CANDIDACY_TYPE
FROM CANDIDACY C, NAME_MASTER N, @LASTATTEND L
WHERE (C.YR_CDE = 2017 OR C.HIST_STAGE_DTE >= '01/01/17')
AND C.STAGE IN ('NTACC', 'ACCPT')
AND C.PROG_CDE = 'UND'
AND C.ID_NUM = N.ID_NUM
AND C.ID_NUM = L.ID_NUM
ORDER BY C.YR_CDE, C.TRM_CDE, N.LAST_NAME, N.FIRST_NAME