(1)全局變量。
輸入以下sql語句,根據查詢結果,了解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顯示結果
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
③聲明一個局部變量,為其賦值,然后對變量取負和取反
DECLARE @mm int
set @mm=5
select @mm as 取正,-@mm as 取負,~@mm AS 取反
④查詢表中所有20歲的學生
USE SM
GO
DECLARE @xsnl INT
SET @xsnl=20
select * from Student where age=@xsnl
(3)函數
①求指定數的絕對值
SELECT ABS(-13/6),ABS(-25),ABS(-13.6),ABS(10-98)
②對變量賦值并計算反余弦,然后將結果輸出
DECLARE @aa FLOAT
SET @aa=0
select 'the acos='+CONVERT(VARCHAR,ACOS(@aa))
③求字符'A','B','AB'的ASCII值
SELECT ASCII('A'),ASCII('B'),ASCII('AB')
④產生一個使用1作為種子的隨機數
DECLARE @bb SMALLINT
SET @bb=1
SELECT RAND(@bb)
⑤查找學號為06001的學生的姓名及其長度
Select SNAME,DATALENGTH(SName) as namel
from Student where sno='06001'
⑥取出所有同學的姓
use sm
select left(sname,1) from student
⑦取出字符串"abcdef"中的ef
select substring('abcedef',5,2)
⑧查找“王_”在表student中學生姓名列某一特定行中的位置
use sm
select patindex('%王_%',SName) from student where sno='09999'
(4)自定義函數(以下為轉載內容)
轉)SQL Server自定義函數
自定義函數
?
用戶定義自定義函數像內置函數一樣返回標量值,也可以將結果集用表格變量返回
用戶自定義函數的類型:
標量函數:返回一個標量值
表格值函數{內聯表格值函數、多表格值函數}:返回行集(即返回多個值)
?
1、標量函數
Create function 函數名(參數)
Returns 返回值數據類型
[with {Encryption | Schemabinding }]
[as]
begin
SQL語句(必須有return 變量或值)
?
End
?
Schemabinding :將函數綁定到它引用的對象上(注:函數一旦綁定,則不能刪除、修改,除非刪除綁定)
?
?
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
?
執行用戶自定義函數
select 用戶名。函數名 as 字段別名
select dbo.AvgResult(‘s0002’) as result
?
用戶自定義函數返回值可放到局部變量中,用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
?
函數引用
?
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、表格值函數
a、 內聯表格值函數
格式:
create function 函數名(參數)
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、 多句表格值函數
?? create function 函數名(參數)
?? returns 表格變量名table (表格變量定義)
?? [with {Encryption | Schemabinding }]
as
?? begin
??? SQL語句
?? end
?
多句表格值函數包含多條SQL語句,至少有一條在表格變量中填上數據值
表格變量格式
returns @變量名 table (column 定義| 約束定義 [,…])
對表格變量中的行可執行select,insert,update,delete , 但select into 和 insert 語句的結果集是從存儲過程插入。
?
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’)
自定義函數部分為轉載部分( http://www.cnblogs.com/jiajinyi/archive/2009/03/13/1410148.html )
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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