Reputation: 251
I'd like to insert the results of this query to the table I created below.
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)
And I'd like to insert the Data from this query using this, however I get this error "Insert Error: Column name or number of supplied values does not match table definition"
because I have an extra column [CompanyRegistration] nvarchar NULL, which I decalred NULL upon creation of the table. How can I insert these query results with my [CompanyRegistration] column tagged as NULL in the table??
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
How do I solve this? I need your help Guys. Thank you in Advance!
Beau
Upvotes: 3
Views: 1631
Reputation: 2351
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
Upvotes: 4
Reputation: 78525
You need to explicitly set CompanyRegistration
to NULL
in your SELECT
statement:
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
Upvotes: 3
Reputation: 204756
It is a good habit to name the columns you want to insert into
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
That way you don't have to fill columns you don't have a value for.
Upvotes: 0