Can we pass parameters to a view in SQL?

前端 未结 20 2368
庸人自扰
庸人自扰 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:45

    A hacky way to do it without stored procedures or functions would be to create a settings table in your database, with columns Id, Param1, Param2, etc. Insert a row into that table containing the values Id=1,Param1=0,Param2=0, etc. Then you can add a join to that table in your view to create the desired effect, and update the settings table before running the view. If you have multiple users updating the settings table and running the view concurrently things could go wrong, but otherwise it should work OK. Something like:

    CREATE VIEW v_emp 
    AS 
    SELECT      * 
    FROM        emp E
    INNER JOIN  settings S
    ON          S.Id = 1 AND E.emp_id = S.Param1
    

提交回复
热议问题