我想将此查询的结果插入到我在下面创建的表中。
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Entity]') AND type in (N'U'))
DROP TABLE [Entity]
Go
Create Table Entity
([EntCode] [nvarchar](8) NOT NULL,
[Name] [nvarchar](80) NOT NULL,
[CompanyRegistration] [nvarchar](80) NULL,
[Active] [int] NOT NULL,
[AccessLevel] [int] NOT NULL ,
[SiteURN] [nvarchar](128) NOT NULL,
[CompanyURN] [nvarchar](128) NOT NULL,
[SiteName] [nvarchar](30) NOT NULL,
[SiteDesc] [nvarchar](60) NULL,
[SiteURL] [nvarchar](512) NOT NULL)
我想使用此插件从此查询中插入数据,但是我收到此错误 "插入错误:列名或提供的值数与表定义不匹配"
因为我有一个额外的列[CompanyRegistration] nvarchar NULL,我在创建表时对其进行了decalred NULL。如何在表格中标记为NULL的[CompanyRegistration]列中插入这些查询结果?
insert into ResourceTaskFact.dbo.Entity
Select
e.EntCode,
e.Name,
e.Active ,
e.AccessLevel,
ss.SiteURN,
ss.CompanyURN,
ss.SiteName ,
ss.SiteDesc ,
ss.SiteURL
from SMECSite ss, SMECLegalEnt e
where ss.localsiteflag = 1
and e.active = 1
我该如何解决这个问题?我需要你的帮助伙计。 提前谢谢!
博
答案 0 :(得分:4)
insert into ResourceTaskFact.dbo.Entity
(e.EntCode,
e.Name,
e.Active,
e.AccessLevel,
ss.SiteURN,
ss.CompanyURN,
ss.SiteName,
ss.SiteDesc,
ss.SiteURL)
Select e.EntCode,
e.Name,
e.Active,
e.AccessLevel,
ss.SiteURN,
ss.CompanyURN,
ss.SiteName,
ss.SiteDesc,
ss.SiteURL
from SMECSite ss, SMECLegalEnt e
where ss.localsiteflag = 1
and e.active = 1
答案 1 :(得分:3)
您需要在CompanyRegistration
声明中明确设置NULL
至SELECT
:
insert into ResourceTaskFact.dbo.Entity
Select
e.EntCode,
e.Name,
NULL AS CompanyRegistration, -- Put column name in to demonstrate why you're selecting NULL here
e.Active ,
e.AccessLevel,
ss.SiteURN,
ss.CompanyURN,
ss.SiteName ,
ss.SiteDesc ,
ss.SiteURL
from SMECSite ss, SMECLegalEnt e
where ss.localsiteflag = 1
and e.active = 1
答案 2 :(得分:0)
将要插入的列命名为
是一个好习惯insert into ResourceTaskFact.dbo.Entity
(
[EntCode],
[Name],
[Active],
[AccessLevel],
[SiteURN],
[CompanyURN]L,
[SiteName],
[SiteDesc],
[SiteURL]
)
Select
e.EntCode,
e.Name,
e.Active ,
e.AccessLevel,
ss.SiteURN,
ss.CompanyURN,
ss.SiteName ,
ss.SiteDesc ,
ss.SiteURL
from SMECSite ss, SMECLegalEnt e
where ss.localsiteflag = 1
and e.active = 1
这样您就不必填充没有值的列。