SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


UDF Function to search files in SQL Server


UDF Function to search files in SQL Server

Author
Message
buyungafrianto
buyungafrianto
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 4
Here is my case, i have a PRODUCTS table

create table PRODUCTS
(
ID_PRODUCTS CHAR(10) primary key not null,
NAME CHAR(30),
PRICE INTEGER
)


Then I fill it with some data,...

insert into PRODUCTS values('B1','Samsung Galaxy Ace 2',250)
insert into PRODUCTS values('B2','Samsung Galaxy Tab 3',375)
insert into PRODUCTS values('B3','Samsung Galaxy Note 2',700)
insert into PRODUCTS values('B4','Apple iPod Touch',200)
insert into PRODUCTS values('B5','Apple Macbook Pro',1250)


Then i wanna create a stored function to search for data based a keyword on NAME column in PRODUCTS table. For example, when i execute that function with a "Samsung" keyword, it will be show a list which contains word "Samsung" inside it. I hope the list will be like this the apperance

========================================
ID_PRODUCTS | NAME | PRICE
========================================
B1 | Samsung Galaxy Ace 2 | 250
----------------------------------------
B2 | Samsung Galaxy Tab 3 | 375
----------------------------------------
B3 | Samsung Galaxy Note 2 | 700
========================================


Here is the code, but it show nothing when execute it (select*from dbo.products_fun)

create function product_fun
(
@name char(30)
)
returns TABLE
as
return
(
select * from products where name like '%@name%'
)


I think it show nothing because the query

select * from products where name like '%@name%'


It is not a search for keywords inside the variable @name, .. but the search for the keyword "@name",... that's why it show nothing when execute it. Anyone wanna help ???
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50581 Visits: 21152
I couldn't find a way of doing this in an iTVF.

If you execute the following:

sp_executesql N'select * from products where name like @ProdName'
,N'@ProdName varchar(30)'
,@ProdName = 'Samsung%'



You'll get the idea of how to do this sort of thing, while protecting yourself a little from SQL Injection. It would work fine in a stored proc ...

--Edit: oh and by the way, using char(n) for a column of variable length is generally best avoided, as all 'n' bytes will always get used, so a varchar(n) might be better in your examples.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
lgegerton
lgegerton
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 26
replace
'%@name%'

with

'%' + @name + '%'
mister.magoo
mister.magoo
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10405 Visits: 7891
Got to say, just add a Manufacturer column and do away with the LIKE query...

MM


select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);




  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Sergiy
    Sergiy
    One Orange Chip
    One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

    Group: General Forum Members
    Points: 25161 Visits: 12464
    lgegerton (5/19/2013)
    replace
    '%@name%'

    with

    '%' + @name + '%'


    '%' + RTRIM(@name) + '%'
    since CHAR data type is used.
    Go


    Permissions

    You can't post new topics.
    You can't post topic replies.
    You can't post new polls.
    You can't post replies to polls.
    You can't edit your own topics.
    You can't delete your own topics.
    You can't edit other topics.
    You can't delete other topics.
    You can't edit your own posts.
    You can't edit other posts.
    You can't delete your own posts.
    You can't delete other posts.
    You can't post events.
    You can't edit your own events.
    You can't edit other events.
    You can't delete your own events.
    You can't delete other events.
    You can't send private messages.
    You can't send emails.
    You can read topics.
    You can't vote in polls.
    You can't upload attachments.
    You can download attachments.
    You can't post HTML code.
    You can't edit HTML code.
    You can't post IFCode.
    You can't post JavaScript.
    You can post emoticons.
    You can't post or upload images.

    Select a forum

































































































































































    SQLServerCentral


    Search