如果值为插入数据SQL Server 2008,请查看value是否为null

时间:2013-06-05 16:05:28

标签: 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,
Description nvarchar(50)
);

3 个答案:

答案 0 :(得分:0)

if exists (select * from [Audit])
  begin    
  ...
  end
else
  begin
  ...
  end

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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,...)