How can we create view from a stored procedure in sql server 2005

  • I want to create a view using the stored procedure named '' GETBILLVIEW ''.

    AS stored procedure is returning dynamic columns. And I want all columns in view....plz help me out as soon as possible.

    Thanx in advance..:)

  • Views cannot be created from stored procedures. They are created from tables.

    http://msdn.microsoft.com/en-us/library/ms189918(SQL.90).aspx

  • Ummmm.... Anyone know for sure if OPENROWSET, OPENQUERY, or OPENDATASOURCE can be used in a view or not? That could be the ticket.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Never mind... the answer is YES... you CAN use OPENROWSET in the view. What that means is that you CAN use a stored procedure as the source for a view. Just remember, you won't be able to pass any parameters to it when it's in the view...

    --===== Create a view based on the stored procedure sp_Who

    CREATE VIEW dbo.OpenRowSetTest

    AS

    SELECT *

    FROM OPENROWSET('SQLOLEDB','Server=yourservernamehere;Trusted_Connection=Yes;Database=Master',

    'Set FmtOnly OFF; EXEC dbo.sp_Who')

    GO

    --===== Select from the view that uses the stored procedure as a source

    SELECT * FROM OpenRowSetTest

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden showed a way to do this, but I would caution you about doing this for your project. Creating a view with dynamic columns is going to cause so many other problems that you will encounter that it just won't be worth it.

    The biggest issue is what is going to happen when you access the view a second time and a different set of data is returned? With a completely different set of columns and datatypes for each column?

    I am not sure SQL Server will allow that - and, if it does in fact allow it to happen I don't know how badly it is going to break your downstream code. Just think what SQL Server would need to do to be able to access that view from multiple processes, returning different sets of data for each process - at the same time.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

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

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