This question already has answers here:
SQL Server equivalent to Oracle's CREATE OR REPLACE VIEW

(9个答案)


4年前关闭。



CREATE OR REPLACE VIEW在SQL Server中似乎不起作用。那么如何在SQL Server上移植CREATE OR REPLACE VIEW呢?

这就是我想要做的:
CREATE OR REPLACE VIEW data_VVVV AS
SELECT
    VCV.xxxx,
        VCV.yyyy AS yyyy,
        VCV.zzzz AS zzzz
FROM
TABLE_A
;

有任何想法吗?

最佳答案

编辑:尽管此问题已标记为重复,但仍在引起注意。 @JaKXz提供的答案是正确的,应该是可接受的答案。

您需要检查该 View 是否存在。然后根据结果执行CREATE VIEWALTER VIEW

IF OBJECT_ID('dbo.data_VVVV') IS NULL
BEGIN
    CREATE VIEW dbo.data_VVVV
    AS
    SELECT VCV.xxxx, VCV.yyyy AS yyyy, VCV.zzzz AS zzzz FROM TABLE_A VCV
END
ELSE
    ALTER VIEW dbo.data_VVVV
    AS
    SELECT VCV.xxxx, VCV.yyyy AS yyyy, VCV.zzzz AS zzzz FROM TABLE_A VCV
BEGIN
END

09-27 05:15