Sql server 2008 查看值是否为null,是否为insert data SQL Server 2008

Sql server 2008 查看值是否为null,是否为insert data SQL Server 2008,sql-server-2008,Sql Server 2008,我正在努力 如果表不存在,请创建新表 如果有,并且其中没有数据,请插入一些 以下是我到目前为止的情况: --See if AuditActivities exists IF OBJECT_ID('Audit') is not null --here is where I need to see if either rows are null, if so, enter data ELSE CREATE TABLE [dbo].[Audit] ( AuditSys int, Descripti

我正在努力

  • 如果表不存在,请创建新表
  • 如果有,并且其中没有数据,请插入一些
以下是我到目前为止的情况:

--See if AuditActivities exists
IF OBJECT_ID('Audit') is not null
--here is where I need to see if either rows are null, if so, enter data
ELSE
CREATE TABLE [dbo].[Audit]
(
AuditSys int,
Description nvarchar(50)
);

您可以使用一个简单的Count()语句查看是否有行,如果结果为0,则使用INSERT。如果不存在,这不是吗(从[audit]中选择*),这样会更干净。如果您不想使用exists,可以检查计数(auditsys)>0,如KingCronus所述
if exists (select * from [Audit])
  begin    
  ...
  end
else
  begin
  ...
  end
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Audit]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[Audit]
    (
        AuditSys int,
        Description nvarchar(50)
    )
END


IF NOT EXISTS (SELECT * FROM [dbo].[Audit])
BEGIN
    --INSERT here
END
IF OBJECT_ID('Audit') is null
BEGIN
    CREATE TABLE [dbo].[Audit]
    (
    AuditSys int,
    Description nvarchar(50)
    )
END
ELSE 
   IF exists(select * from Audit)
   --insert some record
     INSERT INTO Audit(COL1,COL2,..)
     VALUES (VAL1,VAL2,...)