SQL Server Syntax Cheat Sheet

Key Reserved SQL word Optional your text Datatypes int | nvarchar (LENGTH) | ntext | bit Date types day | month | year | quarter | hour | minute | second | millisecond | dayofyear dy,y | mm, m | yy, yyyy | qq, q | hh | mi, n | ss, s | ms | dy, y Stored procedure Basic stored procedure create procedure [dbo].[PROCEDURE_NAME] @PARAMETER1_NAME PARAMETER_TYPE, @PARAMETER2_NAME PARAMETER_TYPE NULL as .......... go; Variables declare @VARIABLE1_NAME VARIABLE1_TYPE; declare @VARIABLE2_NAME VARIABLE2_TYPE; Conditions if(condition = thing | condition <> thing | condition is null )begin ...... end; Table Create create table #LOCALTABLENAME | ##GLOBALTABLENAME ( FIELD1_NAME FIELD1_TYPE, FIELD2_NAME FIELD2_TYPE NULL ); Read select * from #TABLENAME; Close and clean up drop table #TABLENAME; Cursors Declare declare CURSOR_NAME cursor local fast_forward for select statement goes here!

Open open CURSOR_NAME; Fetch fetch next from CURSOR_NAME into @VARIABLE1, @VARIABLE2; Loop through while @@fetch_status = 0 fetch next from CURSOR_NAME into @VARIABLE1, @VARIABLE2; begin end; Close and clean up close CURSOR_NAME; deallocate CURSOR_NAME; Dates Add dateadd ( DATEPART, INTERVAL, DATE); Difference datediff ( DATEPART, DATE1, DATE2);

Add a comment