3

I have a SQL view, and I want to filter it using parameters passed to it. I was thinking about creating a stored procedure, however, after some research, I came to the conclusion that this is not possible.

Other similar threads suggested either creating a stored function or a stored procedure with the View's code embedded into it. My question is, what is the most efficient way to perform such task. My view is made up of around 70 lines of code, just for the record. What do you think? Below are some snippets that are taken from another thread.

The stored procedure would look like

CREATE PROCEDURE s_emp
(
    @enoNumber INT
) 
AS 
SQL VIEW CODE + 
WHERE 
    parameter=@stored_parameter

Or the user defined function would look like

CREATE FUNCTION u_emp
(   
    @enoNumber INT
)
RETURNS TABLE 
AS
RETURN 
(
    SQL VIEW CODE + 
    WHERE     
        parameter=@stored_parameter
)

Source Thread

4
  • 9
    Treat the View like a table. Use a stored procedure to Select from the view with parameters in the Where clause. Commented Aug 1, 2017 at 12:34
  • @WEI_DBA so you sort of execute the view from the stored procedure? Commented Aug 1, 2017 at 12:50
  • 4
    Select * From View_Name Treat your view as a table. That's it. Add a Where clause to narrow your selection down with parameters, etc. Use Order by to sort. Everything you do with a table, do with your view. Commented Aug 1, 2017 at 12:53
  • 1
    @WEI_DBA Great. Thanks for the clarification. Commented Aug 1, 2017 at 12:54

2 Answers 2

3

You can use the view inside stored procedure and filter based on parameter

CREATE PROCEDURE s_emp
(
    @enoNumber INT
) 
AS 
BEGIN
   SELECT *
   FROM VIEW_NAME
   WHERE COLUMN_NAME =  @enoNumber
END
Sign up to request clarification or add additional context in comments.

6 Comments

So you create the view independently and then use the stored procedure to further filter the view and return the result?
if the view is only used in the stored procedure then why still create the view ? Just put the entire select into the stored procedure
Also always begin every stored procedure with "set nocount on"
"@huMpty duMpty" I would like to ask the same question as @GuidoG, why should we create the view in this scenario, if we can put code directly inside the stored procedure? Is there any advantage to this approach?
@observer: I would suggest you read Is a view faster than a simple query?
|
3

You can create a table valued function that behaves exactly like a parameterized view...

SELECT 
    mt.SomeColumn,
    mt.ComeOtherColumn,
    mtvf.FunctionColumn,
    mtvf.AnotherFunctionColumn
FROM 
    dbo.MyTable mt
    JOIN dbo.MyTableValuedFunction('2017-07-01', '2017-08-01') mtvf
        ON mt.SomeColumn = mtvf.SomeColumn;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.