简体   繁体   中英

Invalid Column Name when Inserting Data

I have a local installation of SQLExpress, and accessing it using SQL Server Management Studio.

I created a new database BirdSite in SSMS, with a single table TMasterCountry , with these columns:

Id (int)
CountryName (varchar(50))

Where Id is the primary, auto-incrementing key.

When I use the Script table as... option in the object explorer, I am able to view the empty table with this bit of SQL:

USE [BirdSite]
GO

SELECT [Id]
      ,[CountryName]
  FROM [dbo].[TMasterCountry]
GO

Without any issues.

However, using the same option to try inserting some data, brings up this SQL:

USE [BirdSite]
GO

INSERT INTO [dbo].[TMasterCountry]
           ([CountryName])
     VALUES
           (<CountryName, varchar(50),>)
GO

So I changed the line under VALUES to

(CountryName, 'test')

But when I try running the code, I get these errors:

Msg 207, Level 16, State 1, Line 4

Invalid column name 'CountryName'.

Msg 110, Level 15, State 1, Line 4

There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

Does anyonee know what might be going on here? The column clearly does exist, as I can SELECT the data just fine.

Following some similar questions on SO, I have tried restarting SSMS and also tried refreshing the local Intellisense cache, but with on luck. I also tried surrounding CountryName in square brackets but with the same result.

Change it to:

USE [BirdSite]
GO

INSERT INTO [dbo].[TMasterCountry]
           ([CountryName])
     VALUES
           ('test')
GO

<CountryName, varchar(50),> refers to name of column and it's datatype.

Syntax error; Should be

INSERT INTO [dbo].[TMasterCountry]
           ([CountryName])
     VALUES
           ('test')

You need to replace the entire content between < and > with the value:

INSERT INTO [dbo].[TMasterCountry]
       ([CountryName])
VALUES
       ('test')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM