-----------------T_sql--------------------------------1. Global variable-------------------------------------Print @ @identity --last-inserted identity valuePrint @ @language --language currently in usePrint @ @version --Version numberPrint @ @servername--Service Name--2. Custom Variables ----1. Declaration with declare Declare @i int,@j int Declare @sum int ----2. Assignment Set,select Set @i=Ten Select @j= - --Add the values of I and J and print Set @sum=@i+@j Print @sum --find out the lowest score, print------While loop statementDeclare @stat int,@end int,@count intSet @end=TenSet @count=0Set @stat=0 while(@stat<=@end)begin --you can execute any statement inside . Set @count=@stat+@count Set @stat=@stat+1 Insert intoT_buyer (Baid)Values(@count)EndPrint @count------------timed Command------------------waitforDelay'00:00:3'--3 seconds Delay executionPrint '3..2..1 go!'Select CONVERT(varchar( -),GETDATE(), -)waitforTime'11:40:56' --10:57:47--time to executePrint 'hell0!'-------Case Statement--Usage 1Select Case when Len(bquertion)>0 Then 'has a value' Else 'No value' End,* fromT_buyerSelect *Grade= Case whenScore> the Then 'Excellent' whenScore>= - Then 'Good' Else 'inferior lattice' End fromGrade--Usage TwoSelect *Call= CaseSex when 'male' Then 'handsome man.' Else 'young lady' End---FunctionCreate functionFn_sum (---parameter list @aa int , @bb int)returns int --specifying the return type as begin--method Body Declare @s int Set @s=@aa+@bb return @sEndDeclare @x int Set @x =Dbo.fn_sum (Ten, -)Print @x-------------------Dynamic Execution-------------------Declare @sql varchar( -)Set @sql='SELECT * FROM Student'-----to execute the string as a statement in SQL Serverexec(@sql)-------As long as the statements in @sql conform to the SQL syntax, you can--Stored Procedures--no parameter stored procedureCreate procUp_print asPrint 1Go--with parameter functionCreate procUp_print1--(Stored procedures cannot have the same name in SQL)@str nvarchar( -) as Print @strGoexecUp_printexecUp_print1'Oh, hello.'--stored procedure with output parametersCreate procedureUp_print2@xing nvarchar(1),@ming nvarchar(5),@rtn nvarchar(6) Output asSet @rtn=@xing+@mingGoDeclare @rtn nvarchar(6)execUp_print2'Li','Tao',@rtnOutputPrint @rtn----Write a new student's deposit process Up_insertstuif(object_id('Up_insertstu','P')) is not NULLDrop procUp_insertstuGoCreate procUp_insertstu@stuname varchar( -), @sex varchar(2), @stuno varchar( +), @birthday datetime, @remark text asbeginTryDeclare @id int --1. Inserting Data Insert intoStudentValues(@stuname,@sex,@stuno,@birthday,@remark) --2. Add an English random score for the student just now Set @id =@ @IDENTITY Insert intoGradeValues('English',RAND()* -,@id)EndTrybeginCatchPrint 'execution error, constraint violation, error number:'+Convert(varchar,@ @error)EndCatchGo---------------------------------Call----------------------------------execUp_insertstu'Tao Tao','male','2014140208','1991-1-1','No'
SQL Server Getting started with mastering----T-SQL BASIC statements + functions and stored procedures