001A.CURSOR FETCH
Template: USE database_name GO -- Declare the variables to store the values returned by FETCH. DECLARE @Var1 VARCHAR(50), @Var2 VARCHAR(50), Var3 VARCHAR(50) DECLARE cursor_name CURSOR FOR SELECT Column1, Column2, Column3 FROM table_name WHERE condition OPEN cursor_name -- Perform the first fetch and store the values in variables. -- Note: The variables are in the same order as the columns -- in the SELECT statement. FETCH NEXT FROM cursor_name INTO @Var1, @Var2, Var3 -- Check @@FETCH_STATUS to see if there are any more rows to fetch. WHILE @@FETCH_STATUS = 0 BEGIN -- Write some SQL statements here -- This is executed as long as the previous fetch succeeds. FETCH NEXT FROM cursor_name INTO @Var1, @Var2, @Var3 END CLOSE cursor_name DEALLOCATE cursor_name GO Other FETCH Options: -- Fetch the last row in the cursor. FETCH LAST FROM cursor_name -- Fetch the row immediately prior to the current row in the cursor. FETCH PRIOR FROM cursor_name -- Fetch the second row in the cursor. FETCH ABSOLUTE 2 FROM cursor_name -- Fetch the row that is three rows after the current row. FETCH RELATIVE 3 FROM cursor_name -- Fetch the row that is two rows prior to the current row. FETCH RELATIVE -2 FROM cursor_name |
001B. Simple cursor in
SQL server to update rows Template:
Example Link USE database_name GO DECLARE cursor_name CURSOR FOR SELECT column1, column2, column3, ... FROM table_name WHERE condition -- open cursor and fetch first row into variables OPEN cursor_name FETCH FROM cursor_name -- check for a new row WHILE @@FETCH_STATUS = 0 BEGIN -- do update operation UPDATE table_name SET columnx = Value WHERE CURRENT OF cursor_name -- get next available row into variables FETCH NEXT FROM cursor_name END close cursor_name Deallocate cursor_name |
002.SELECT INTO
Template: SELECT column1, column2, column3, ... INTO newtable FROM oldtable WHERE condition |
003.INSERT INTO
Template: INSERT INTO table2 (column1, column2, column3, ...) SELECT column1, column2, column3, ... FROM table1 WHERE condition |
004.CASE WHEN
Template: CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 WHEN conditionN THEN resultN ELSE result END |
005. /* SQL to Pull a List of InSchool Students as of @AsOfDate */ USE CampusVue GO DECLARE @AsOfDate DATE; SET @AsOfDate = '2020-12-31'; -- Enter a Valid Date in the Past or Today WITH temp AS ( SELECT *, CASE WHEN StatusID in (17,18) THEN Convert(Date, GradDate) WHEN StatusID in (20) THEN Convert(Date, DropDate) WHEN StatusID in (13,14,15,93,137,140,141,142,163,173,174,175,176) THEN '3099-12-31' END AS XDate FROM MSLSV WHERE StatusID in (13,14,15,17,18,20,93,137,140,141,142,163,173,174,175,176) ) SELECT DISTINCT temp.* FROM temp WHERE ExpStartDate <= @AsOfDate AND XDate > @AsOfDate |
005A.
/* SQL to Pull a List of InSchool Students as of @AsOfDate (Simplified)*/ USE campusVue GO DECLARE @AsOfDate DATE; SET @AsOfDate = '2020-12-31'; -- Enter a Valid Date in the Past or Today SELECT * FROM EnrollmentV WHERE ExpStartDate <= @AsOfDate AND XDate > @AsOfDate |
006. /* SQL to Pull a List of Students Enrolled Between @EnrolledBeginDate and @EnrolledEndDate */ USE CampusVue GO DECLARE @EnrolledBeginDate DATE, @EnrolledEndDate DATE; SET @EnrolledBeginDate = '2020-07-01'; -- Enter Enrolled Begin Date in the Past SET @EnrolledEndDate = '2021-06-30'; -- Enter Enrolled End Date in the Past or Today WITH temp AS ( SELECT *, CASE WHEN StatusID in (17,18) THEN Convert(Date, GradDate) WHEN StatusID in (20) THEN Convert(Date, DropDate) WHEN StatusID in (13,14,15,93,137,140,141,142,163,173,174,175,176) THEN '3099-12-31' END AS XDate FROM MSLSV WHERE StatusID in (13,14,15,17,18,20,93,137,140,141,142,163,173,174,175,176) ) SELECT DISTINCT temp.* FROM temp WHERE ExpStartDate <= @EnrolledEndDate and XDate >= @EnrolledBeginDate |
006A. /* SQL to Pull a List of Students Enrolled Between @EnrolledBeginDate and @EnrolledEndDate (Simplified)*/ USE CampusVue GO DECLARE @EnrolledBeginDate DATE, @EnrolledEndDate DATE; SET @EnrolledBeginDate = '2020-07-01'; -- Enter a Valid Enrolled Begin Date in the Past SET @EnrolledEndDate = '2021-06-30'; -- Enter a Valid Enrolled End Date in the Past or Today SELECT * FROM EnrollmentV WHERE ExpStartDate <= @EnrolledEndDate and XDate > = @EnrolledBeginDate |
007. SQL Running Total. Link1 | Link2 | Link3 | Link4 |
008. CampusVue Crystal Report <SELECTION>
Examples: <SELECTION> <Stored Procedure=dbo.cstSP_FCExitReport_rpt_rk> [none1.none1](datetimerange:)"Date Range" [none2.none2](bit:)#DEFAULT:1#"Drop Status" [none3.none3](bit:)"Grad Status" <SELECTION> <Stored Procedure=dbo.cstSP_FCPastDueReport_rpt_rk> [none0.none0](datetimesingle:)"As of Date" <SELECTION> <Stored Procedure=dbo.cstSP_FCTuitioScheduleAndAgingReport_rpt_rk> [none0.none0](datetimesingle:)"Exp Start Date >=" |
009. |
010. |