UDF Function to search files in SQL Server

  • 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 ???

  • 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.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • replace

    '%@name%'

    with

    '%' + @name + '%'

  • 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
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • lgegerton (5/19/2013)


    replace

    '%@name%'

    with

    '%' + @name + '%'

    '%' + RTRIM(@name) + '%'

    since CHAR data type is used.

    _____________
    Code for TallyGenerator

  • Viewing 5 posts - 1 through 4 (of 4 total)

    You must be logged in to reply to this topic. Login to reply