UDF Function to search files in SQL Server -


this question exact duplicate of:

here case, have products table

create table products  (   id_products           char(10) primary key not null,   name                  char(30),   price                 integer ) 

then fill data,...

insert products values('b1','samsung galaxy ace 2',250) insert products values('b2','samsung galaxy tab 3',375) insert products values('b3','samsung galaxy note 2',700) insert products values('b4','apple ipod touch',200) insert products values('b5','apple macbook pro',1250) 

then want create stored function search data based keyword on name column in products table. example, when execute function samsung keyword, should showing list contains word samsung in name. hope list appearance

id_products | name | price ======================================== b1 | samsung galaxy ace 2 | 250 b2 | samsung galaxy tab 3 | 375 b3 | samsung galaxy note 2 | 700 

here code, show nothing when execute (select * dbo.products_fun)

create function product_fun (    @name char(30) ) returns table    return     (select * products name '%@name%') 

i think show nothing because query

select * products name '%@name%' 

it not search keywords inside variable @name, .. search keyword "@name",... that's why show nothing when execute it. wanna ???

as said - product name, never use char(x) value. char(x) should used fixed-length strings of 3-5 characters in length, e.g. iso country codes, or currency symbols etc. else should varchar(x).

this code works fine:

create table products1 (productid int identity(1,1) primary key clustered,  productname varchar(30),  productprice decimal(16,2) )  insert products1(productname, productprice) values('samsung galaxy ace 2',250.0) insert products1(productname, productprice) values('samsung galaxy tab 3',375.0) insert products1(productname, productprice) values('samsung galaxy note 2',700.0) insert products1(productname, productprice) values('apple ipod touch',200.0) insert products1(productname, productprice) values('apple macbook pro',1250.0) go  create function findproducts (@name varchar(30)) returns table    return (select * dbo.products1 productname @name + '%' ) 

now calling function:

select * dbo.findproducts('samsu') 

returns result set:

productid   productname             productprice    1        samsung galaxy ace 2       250.00    2        samsung galaxy tab 3       375.00    3        samsung galaxy note 2      700.00 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -