Example of Date Range Selection: WITH q AS ( SELECT 1 AS num UNION ALL SELECT num + 1 FROM q WHERE num < 10 ) SELECT DATEADD(DAY,0,cast(cast(DATEADD(DAY,-num,SYSDATETIME()) as date) as datetime)) FROM q Simple version of the example of selecting a range of numbers:
Example of updating data in one table with simultaneous comparison and condition check based on the second table using JOIN: update cc set cc.ActivateState=2 from ClientsCards cc left join ClientsCardsHeads cch on cch.CardUid=cc.Uid wher…
This example can be useful when you need to choose a certain number of numbered records from somewhere and insert them into a temporary table or table variable. Example: select number as Section from master..spt_values where type='L'…
Example of how to catch errors in MSSQL. BEGIN TRY -- Some code that will return an error for example. SELECT 1/0; END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber ,ERROR_SEVERITY() AS ErrorSeverity ,ERROR_STATE() AS ErrorState ,ERROR_PROCEDURE() AS ErrorProcedure ,ERROR_LINE() AS ErrorLine ,ERROR_MESSAGE() AS ErrorMessage; END CATCH