passing parameter to view

  • Hi!

    i have two question. First, i use views for showing datas(tables) that don't made changes on it. For example, i show new products on a page. i use view for this. is it true?

    Second, when i use views adding parameter. for example i want to list products that has categroyid=2.

    can i pass categoryid as parameter to view.

    Thankss..

  • You can't create a view with a parameter.

    Create a Stored Procedure.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • I would prefer an inline-Table valued function over of a stored procedure.

    The benefit: you can still use it like a view (e.g. SELECT * FROM your_function(param1))



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • LutzM (5/24/2011)


    I would prefer an inline-Table valued function over of a stored procedure.

    The benefit: you can still use it like a view (e.g. SELECT * FROM your_function(param1))

    Agreed on the iTVF...some people even refer to them as "parameterized views".

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • You don't need to use a view to see data that you're not currently trying to modify. You can just select from the table:

    create table product (

    id int identity(1,1) not null,

    name varchar(255) not null,

    categoryid int not null,

    price money not null

    )

    select name, price, categoryid from product

    If you want to restrict to just one category you can do this:

    select name, price, categoryid from product

    where categoryid = 2

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

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