如何使 CREATE OR REPLACE VIEW 在 SQL Server 中工作?

2021-12-29 00:00:00 sql sql-server view porting

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

CREATE OR REPLACE VIEW doesn't seem to work in SQL Server. So how do I port CREATE OR REPLACE VIEW to work on SQL Server?

这就是我想要做的:

CREATE OR REPLACE VIEW data_VVVV AS 
SELECT 
    VCV.xxxx,
        VCV.yyyy AS yyyy,
        VCV.zzzz AS zzzz
FROM 
TABLE_A
;

有什么想法吗?

推荐答案

虽然这个问题被标记为重复,但它仍然受到关注.@JaKXz 提供的答案是正确的,应该是公认的答案.

您需要检查视图是否存在.然后根据结果执行 CREATE VIEWALTER VIEW.

You'll need to check for the existence of the view. Then do a CREATE VIEW or ALTER VIEW depending on the result.

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

相关文章