您的位置:首页 > 数据库

SQL Server自定义函数进行模糊查询返回表类型没有数据的问题

2016-05-28 23:52 871 查看
今天帮一个学弟看SQL Server的问题,他创建了一个自定义函数,使用参数对Book表的B_author和B_name字段进行模糊查询,然后返回与Book表字段类型相同的@fn_wildcard表,但是执行函数之后,没有数据显示。出问题的代码如下:

Book_type表建表语句

create table Book_type(
Bt_type char(10) primary key,
Bt_name char(18) not null,
Bt_qty int check(Bt_qty>=0)
)


Book表的建表语句

create table Book(
B_id INT IDENTITY(2000001,1)PRIMARY KEY,
B_author char(50) not null,
B_name char(50) not null,
B_publish char(50) not null,
B_price char(10) not null,
B_sum_qty int check(B_sum_qty>=0),
B_now_qty int check(B_now_qty>=0),
Bt_type char(10) not null,
B_location char(20) not null
foreign key(Bt_type) references Book_type(Bt_type)
)
创建函数语句

create function wildcard1(@flag char(50))
returns @fn_wildcard table
(
B_i INT,
B_w char(50) not null,
B_na char(50) not null,
B_p char(50) not null,
B_pp char(10) not null,
B_sum int,
B_now int,
Bt_t char(10) not null,
B_Loca char(20) not null
)
as
begin
insert @fn_wildcard
select  B_id,B_name,B_author,B_publish,B_price,B_sum_qty,B_now_qty,Bt_type,B_location
from book
where B_name like '%'+@flag+'%' or B_author like '%'+@flag+'%'
return
end
插入测试数据
insert into Book_type values('1','文学类',100);
insert into Book_type values('2','军事类',100);
insert into Book_type values('3','历史类',100);
<pre name="code" class="sql">insert into Book values('张三','电脑文学','机工','11',111,111,'1','南昌');
insert into Book values('李四','新电脑教程','机工','11',111,111,'1','福州');
insert into Book values('王五','文化评说','机工','11',111,111,'1','杭州');



查询Book表



执行函数wildcard,没有得到预期结果

select * from wildcard1('电脑');



不使用函数时,可以进行模糊查询(如下图所示)

select  B_id,B_name,B_author,B_publish,B_price,B_sum_qty,B_now_qty,Bt_type,B_location
from book where B_name like '%电脑%';



接下来分析数据字段类型是否一致时发现,Book表的B_author和B_name的数据类型是char,对其进行修改,将其改为varchar,函数中的类型也做相应变化(如下所示)

--Book建表语句
create table Book(
B_id INT IDENTITY(2000001,1)PRIMARY KEY,
B_author varchar(50) not null,
B_name varchar(50) not null,
B_publish char(50) not null,
B_price char(10) not null,
B_sum_qty int check(B_sum_qty>=0),
B_now_qty int check(B_now_qty>=0),
Bt_type char(10) not null,
B_location char(20) not null
foreign key(Bt_type) references Book_type(Bt_type)
)
--函数创建语句
create function wildcard1(@flag varchar(50))
returns @fn_wildcard table
(
B_i INT primary key,
B_w varchar(50) not null,
B_na varchar(50) not null,
B_p char(50) not null,
B_pp char(10) not null,
B_sum int,
B_now int,
Bt_t char(10) not null,
B_Loca char(20) not null
)
as
begin
insert @fn_wildcard
select  B_id,B_name,B_author,B_publish,B_price,B_sum_qty,B_now_qty,Bt_type,B_location
from book
where B_name like '%'+@flag+'%' or B_author like '%'+@flag+'%'
return
end
再调用函数,对‘电脑’进行模糊查询,出现预期结果



解决问题之后,我又查找了SQLServer中varchar和char类型的差异,varchar不定长,char定长,不足长度时使用空格进行补足,这个应该是造成前面没有出现预期结果的原因,在SQLServer中需要了解不同的数据类型之间的差异,可以避免许多问题,最后对SQLServer中与字符串相关的几种类型使用的情况做个总结,仅供大家参考:

(1)如果你肯定存储的数据长度,而且不包中文的,可以选择char类型。

(2)如果肯定存储的数据长度,但可能包括中文,可以选择nchar类型。

(3)如果不确定存储的数据长度,存储只有英文、数字的最好用varchar

(4)如果不确定存储的数据长度,也有可能有中文,可以选择nvarchar类型,在SQL Server2005中也是比较常用的字符数据类型。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql sql server 函数