Using ALTER VIEW from within a stored procedure

Posted on 2010-01-07
Last Modified: 2012-06-22
Im looking to fire a stored procedure one per day.  It retrieves the primary key of records that have been added in the last 30 days. Im then looking to ALTER VIEW so that the view filters for records where the primary key is greater than the one just retieved.
Having issues with ALTER VIEW needing to be the first statement.
Using the PK opposed to just using date criteria is required for speed.
Question by:PeterMatthews
    LVL 4

    Expert Comment

    is a better strategy. You could try it.

    From the manual:
    This statement creates a new view, or replaces an existing
    one if the OR REPLACE clause is given. If the view does
    not exist, CREATE OR REPLACE VIEW is the same as CREATE
    VIEW. If the view does exist, CREATE OR REPLACE VIEW is
    the same as ALTER VIEW.

    Author Comment

    Sorry only put on the tags that Im using SQL 2000 which doesnt appear to support this.
    LVL 41

    Accepted Solution

    You can alter your view like this:

    alter procedure yourprocedure
    ... code blah blah
    execute sp_executeSQL N'ALTER VIEW dbo.yourview AS SELECT column1, column2, column3 FROM dbo.yourtable;'

    Open in new window


    Author Closing Comment

    Great that will do fine, thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    I recently came across an interesting Question In EE ( and was puzzled about how to achieve that using SSIS out of the box tasks, which was i…
    Introduced in Microsoft SQL Server 2005, the Copy Database Wizard ( is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Viewers will learn how the fundamental information of how to create a table.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now