How to execute stored procedure inside view in SQL server

Reportboy2021 1 Reputation point
2022-01-05T15:36:28.483+00:00

How to execute stored procedure inside view in SQL server

SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,061 questions
Developer technologies Transact-SQL
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Joyzhao-MSFT 15,631 Reputation points
    2022-01-06T02:11:08.4+00:00

    Hi @Reportboy2021 ,
    You cannot call a stored proc from inside a view. It is not supported. However you can make views call other views, or table-valued user-defined functions.

    For the latter you must make sure that you're using inline functions. Otherwise, any subsequent clauses like WHERE, GROUP BY and ORDER BY to will be performed on the dynamically produced resultset instead. Thus, you won't benefit from index searches and the likes. This may have a huge impact on performance.

    Best Regards,
    Joy


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Tom Phillips 17,771 Reputation points
    2022-01-05T21:15:21.043+00:00

    You can't. It is not possible.

    A view can only contain a SELECT statement.

    0 comments No comments

  3. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-01-05T22:42:24.81+00:00

    What Tom says is not 100 % true. However, the solution is so completely flawed that I am not going to share it. Describe your real problem instead and we may be able to help you.


  4. LiHong-MSFT 10,056 Reputation points
    2022-01-06T02:17:33.29+00:00

    Hi,@Reportboy2021
    The view (VIEW) virtual table is a section of SELECT statement of the query.Please refer to this Document for more details.
    If you insist on excuting a stored procedure in the view,I have few immature ideas :
    (1)Encapsulate the statement in the stored procedure into a function and then call it in the view.
    (2)Use OPENROWSET like this:

    Create VIEW vwTestViewName               
    AS  
    SELECT *  
    FROM OPENROWSET( 'SQLNCLI',   --DBlink   
                     'DRIVER={SQL Server};SERVER=192.168.0.13;UID=sa;PWD=sa; Trusted_Connection=no',  
                     'SET FMTONLY OFF;SET NOCOUNT ON;EXEC PieroTest.dbo.SP_StoredProcName')   
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.