Can we pass parameters to a view in SQL?

前端 未结 20 2268
庸人自扰
庸人自扰 2020-11-29 18:29

Can we pass a parameter to a view in Microsoft SQL Server?

I tried to create view in the following way, but it doesn\'t work:

create o         


        
20条回答
  •  遥遥无期
    2020-11-29 18:39

    There are 2 ways to acheive what you want unfortunatly neither can be done using a view.

    You can either create a table valued user defined function that takes the parameter you want and returns a query result

    Or you can do pretty much the same thing but create a stored procedure instead of a user defined function.

    For Example

    the stored procedure would look like

    CREATE PROCEDURE s_emp
    (
        @enoNumber INT
    ) 
    AS 
    SELECT
        * 
    FROM
        emp 
    WHERE 
        emp_id=@enoNumber
    

    Or the user defined function would look like

    CREATE FUNCTION u_emp
    (   
        @enoNumber INT
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
        SELECT    
            * 
        FROM    
            emp 
        WHERE     
            emp_id=@enoNumber
    )
    

提交回复
热议问题