SQL - 在 IF 条件下执行

SQL - GO in IF Condition

我需要制作一个 SQL 脚本,它可以在同一个数据库上执行多次,以检查 table 是否已经存在。如果是,则不做任何事情,如果不是,则创建 table 并插入一些数据。问题是,我不能在 BEGIN 和 END 标签内使用 'GO'。

我需要什么(代码不起作用):

    IF (OBJECT_ID('dbo.Report', 'U') IS NULL)
    BEGIN
        CREATE TABLE [dbo].[Report](
            [ReportID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_Report_ReportID_1]  DEFAULT (newid()),
            [Name] [nvarchar](100) NULL,
            [Description] [ntext] NULL,
            [Query] [ntext] NULL,
            CONSTRAINT [PK_Reporting] PRIMARY KEY CLUSTERED 
            (
                [ReportID] ASC
            )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
            ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];
        )
        GO   

        INSERT [dbo].[Report] ([ReportID], [Name], [Description], [Query]) VALUES (N'1', N'04. People and groups', N'People and groups', N'select * from V_REPORT04USERGROUPS order by Login')
        GO

    END
    GO

有人能告诉我最简洁的方法吗?谢谢!

试试这个并确保为 reportID

提供数据类型
IF (OBJECT_ID('dbo.Report1', 'U') IS NULL)
BEGIN
    CREATE TABLE [dbo].[Report1](
        [ReportID] int NULL,
        [Name] [nvarchar](100) NULL,
        [Description] [ntext] NULL,
        [Query] [ntext] NULL
    );

    INSERT INTO [dbo].[Report1] ([ReportID], [Name], [Description], [Query]) VALUES (N'1', N'04. People and groups', N'People and groups', N'select * from V_REPORT04USERGROUPS order by Login');

END
GO
IF (OBJECT_ID('dbo.Report', 'U') IS NULL)
BEGIN
    CREATE TABLE [dbo].[Report](
        [ReportID] NULL,
        [Name] [nvarchar](100) NULL,
        [Description] [ntext] NULL,
        [Query] [ntext] NULL
    );


    INSERT [dbo].[Report] 
    ([ReportID], [Name], [Description], [Query]) 
    VALUES 
    (N'1', N'04. People and groups', N'People and groups', 
     N'select * from V_REPORT04USERGROUPS order by Login');

END

所有列都接受 NULL 但没有主键?