huakaiyueluo

导航

sqlserver 自学笔记之 常量,变量及函数

(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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2013-10-15 00:52  huakaiyueluo  阅读(1492)  评论(0编辑  收藏  举报