1、查看主機(jī)名稱 select host_name() go 2、日期函數(shù) getdate()返回服務(wù)器當(dāng)前的系統(tǒng)日期和時(shí)間 datename(日期元素,日期)返回指定日期的名字,返回字符串 datepart(日期元素,日期)返回指定日期的一部分,用整數(shù)返回 datediff(日期元素,日期1,日期2)返回兩個日期間的差距并轉(zhuǎn)換成日期元素的形式 dateadd(日期元素,日期)將日期元素加上日期產(chǎn)生新的日期 year(日期)返回年份 month(日期)返回月份 day(日期)返回某月幾號的整數(shù)值 getutcdate()返回表示當(dāng)前utc時(shí)間的日期值 3、字符函數(shù) stuff(字符表達(dá)式1,start,length,字符表達(dá)式2) 字符表達(dá)式1中從start開始的length個字符換成字符表達(dá)式2 4、數(shù)字函數(shù) round(數(shù)值表達(dá)式,整數(shù)表達(dá)式)將設(shè)置表達(dá)式四舍五入為整型表達(dá)式所給的精度。 5、批處理命令 批處理是包含一個或多個Transact—SQL語句的組,它將一次性地發(fā)送到SQL Server中執(zhí)行,應(yīng)用程序?qū)⑦@些語句作為一個單元一次性地提交給SQL Server,并由SQL Server編譯成一個執(zhí)行計(jì)劃,然后作為一個整體去執(zhí)行。如果批處理中的某一條語句發(fā)生錯誤,執(zhí)行計(jì)劃就無法編譯,從而導(dǎo)致批處理中的任何語句都無法執(zhí)行。用GO命令來通知SQL Sever和Transact—SQL語句的結(jié)束。 大多數(shù)的create命令要在單個批處理命令中執(zhí)行,但create database,create table,crate index例外。 6、流程控制 (1)if……else……語句 (2)begin……end……語句 create proc get_girl as begin if exists(select * from student where ssex='女') begin declare @sno nchar(10),@sname nchar(10) select @sno=sno,@sname=sname from student print @sno+@sname end end exec get_girl (3)while語句 (4)case語句 select sno as '學(xué)號',sname as '姓名',ssex as '性別',年齡情況= case sage--此處的sage為字符型數(shù)據(jù)或者為數(shù)值型數(shù)據(jù) when '21' then '年齡為21' when '22' then '年齡為22' when '23' then '年齡為23' else '不明' end from student 總結(jié): case XXX when 值1 then 結(jié)果1 when 值2 then 結(jié)果2 ... else 結(jié)果else end or case when 判斷表達(dá)式1 then 結(jié)果1 when 判斷表達(dá)式2 then 結(jié)果2 ... else 結(jié)果else end (5)waitfor語句 waitfor命令用來暫時(shí)停止程序執(zhí)行,直到所設(shè)定的等待時(shí)間已過或所設(shè)定的時(shí)間已到才繼續(xù)往下執(zhí)行。 實(shí)例:等待2小時(shí)2分鐘2秒才執(zhí)行select語句 waitfor delay '02:02:02' select * from book1 (6)goto語句 declare @sum smallint,@i smallint set @i=1 set @sum=0 beg: if(@i<100) begin set @sum=@sum+@i set @i=@i+1 goto beg end print @sum 注意:goto命令用來改變程序執(zhí)行的流程,使程序跳到標(biāo)識符指定的程序行再繼續(xù)往下執(zhí)行,作為跳轉(zhuǎn)目標(biāo)的標(biāo)識符可為數(shù)字與字符的組合,但必須以“:”結(jié)尾。在goto命令行,標(biāo)識符后不必跟 |
|