(1)全局變量。
輸入以下sql語句,根據(jù)查詢結(jié)果,了解sqlserver全局變量的含義
select @@VERSION
select @@CONNECTIONS
select @@CURSOR_ROWS
select @@error
select @@language
select @@options
select @@PROCID
select @@ROWCOUNT
select @@SERVERNAME
select @@SERVICENAME
(2)局部變量
①聲明一個CHAR類型的局部變量,并為其賦值
DECLARE @MYCHAR CHAR(20)
SET @MYCHAR='THIS IS A STRING'
SELECT @MYCHAR
②聲明一些局部變量,為其賦值,并用select顯示結(jié)果
DECLARE @MYCHAR CHAR(10)
DECLARE @變量 float,@per int
DECLARE @price money,@timed datetime
set @MYCHAR="HELLO WORLD"
SET @變量=1234.5
set @per=7
set @price=8888.34
set @timed='2012-09-22'
select @MYCHAR,@變量,@per,@price,@timed
③聲明一個局部變量,為其賦值,然后對變量取負(fù)和取反
DECLARE @mm int
set @mm=5
select @mm as 取正,-@mm as 取負(fù),~@mm AS 取反
④查詢表中所有20歲的學(xué)生
USE SM
GO
DECLARE @xsnl INT
SET @xsnl=20
select * from Student where age=@xsnl
(3)函數(shù)
①求指定數(shù)的絕對值
SELECT ABS(-13/6),ABS(-25),ABS(-13.6),ABS(10-98)
②對變量賦值并計算反余弦,然后將結(jié)果輸出
DECLARE @aa FLOAT
SET @aa=0
select 'the acos='+CONVERT(VARCHAR,ACOS(@aa))
③求字符'A','B','AB'的ASCII值
SELECT ASCII('A'),ASCII('B'),ASCII('AB')
④產(chǎn)生一個使用1作為種子的隨機(jī)數(shù)
DECLARE @bb SMALLINT
SET @bb=1
SELECT RAND(@bb)
⑤查找學(xué)號為06001的學(xué)生的姓名及其長度
Select SNAME,DATALENGTH(SName) as namel
from Student where sno='06001'
⑥取出所有同學(xué)的姓
use sm
select left(sname,1) from student
⑦取出字符串"abcdef"中的ef
select substring('abcedef',5,2)
⑧查找“王_”在表student中學(xué)生姓名列某一特定行中的位置
use sm
select patindex('%王_%',SName) from student where sno='09999'
(4)自定義函數(shù)(以下為轉(zhuǎn)載內(nèi)容)
轉(zhuǎn))SQL Server自定義函數(shù)
自定義函數(shù)
?
用戶定義自定義函數(shù)像內(nèi)置函數(shù)一樣返回標(biāo)量值,也可以將結(jié)果集用表格變量返回
用戶自定義函數(shù)的類型:
標(biāo)量函數(shù):返回一個標(biāo)量值
表格值函數(shù){內(nèi)聯(lián)表格值函數(shù)、多表格值函數(shù)}:返回行集(即返回多個值)
?
1、標(biāo)量函數(shù)
Create function 函數(shù)名(參數(shù))
Returns 返回值數(shù)據(jù)類型
[with {Encryption | Schemabinding }]
[as]
begin
SQL語句(必須有return 變量或值)
?
End
?
Schemabinding :將函數(shù)綁定到它引用的對象上(注:函數(shù)一旦綁定,則不能刪除、修改,除非刪除綁定)
?
?
Create function AvgResult(@scode varchar(10))
Returns real
As
Begin
?? Declare @avg real
?? Declare @code varchar(11)
?? Set @code=@scode + ‘%’
?? Select @avg=avg(result) from LearnResult_baijiali
Where scode like @code
Return @avg
End
?
執(zhí)行用戶自定義函數(shù)
select 用戶名。函數(shù)名 as 字段別名
select dbo.AvgResult(‘s0002’) as result
?
用戶自定義函數(shù)返回值可放到局部變量中,用set ,select,exec賦值
declare @avg1 real ,@avg2 real ,@avg3 real
select @avg1= dbo.AvgResult(‘s0002’)
set @avg2= dbo.AvgResult(‘s0002’)
exec @avg3= dbo.AvgResult ‘s0002’
select @avg1 as avg1 ,@avg2 as avg2 ,@avg3 as avg3
?
函數(shù)引用
?
create function code(@scode varchar(10))
returns varchar(10)
as
begin
declare @ccode varchar(10)
set @scode = @scode + ‘%’
select @ccode=ccode from cmessage
?? where ccode like @scode
return @ccode
end
?
select name from class where ccode = dbo.code(‘c001’)
?
2、表格值函數(shù)
a、 內(nèi)聯(lián)表格值函數(shù)
格式:
create function 函數(shù)名(參數(shù))
returns table
[with {Encryption | Schemabinding }]
as
return(一條SQL語句)
?
create function tabcmess(@code varchar(10))
returns table
as
return(select ccode,scode from cmessage where ccode like @ccode)
?
b、 多句表格值函數(shù)
?? create function 函數(shù)名(參數(shù))
?? returns 表格變量名table (表格變量定義)
?? [with {Encryption | Schemabinding }]
as
?? begin
??? SQL語句
?? end
?
多句表格值函數(shù)包含多條SQL語句,至少有一條在表格變量中填上數(shù)據(jù)值
表格變量格式
returns @變量名 table (column 定義| 約束定義 [,…])
對表格變量中的行可執(zhí)行select,insert,update,delete , 但select into 和 insert 語句的結(jié)果集是從存儲過程插入。
?
Create function tabcmessalot (@code varchar(10))
Returns @ctable table(code varchar(10) null,cname varchar(100) null)
As
Begin
Insert @ctable
Select ccode,explain from cmessage
Where scode like @code
return
End
?
Select * from tabcmessalot(‘s0003’)
自定義函數(shù)部分為轉(zhuǎn)載部分( http://www.cnblogs.com/jiajinyi/archive/2009/03/13/1410148.html )
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
