Msg 207, Level 16, State 1 "Invalid column name 'Name'" - sql

I'm trying to drop and recreate a table with a new schema in SQL Server, and then insert some test data into it to validate that it's working correctly. I have the following DDL:
CREATE TABLE [dbo].[Product](
[ID] int IDENTITY(1,1) PRIMARY KEY,
[Name] VARCHAR(100) NOT NULL,
[Description] VARCHAR(200) NULL,
[Modified] DATETIME NOT NULL DEFAULT(GETUTCDATE()),
[ModifiedBy] VARCHAR(32) NOT NULL
);
and the following insert statement:
INSERT INTO [dbo].[Product]([Name], [Description], [Modified], [ModifiedBy])
VALUES('Test', 'Description', GETUTCDATE(), 'Me');
Whenever I attempt to insert the aforementioned row, I get an error:
Msg 207, Level 16, State 1, Line 38
Invalid column name 'Name'.
Msg 207, Level 16, State 1, Line 38
Invalid column name 'Description'.
I know the table is there since I can SELECT * FROM Product and get an empty result set with the correct columns and no errors... I just can't insert into it for some reason. Any help would be greatly appreciated!

As per Martin's answer, I did some more research and he is correct. The schema manipulation needs to be done in separate batches (I simply added a GO after dropping my old table and after creating my new one with a different schema). A more detailed answer can be found here: "Invalid column name" error when calling insert after table created

Maybe try refreshing the cache. Easy way is to press Ctrl + shift + R
Or
Edit > IntelliSense > Refresh Local Cache
If doesnt work, then use qualified names [dbo].table or username.tablename etc
More here: http://msdn.microsoft.com/en-us/library/ms174205.aspx

For me, your query looks good, and i test it successfully.
Try adding schema name and put all column name inside brackets:
CREATE TABLE [dbo].[Product](
[ID] int IDENTITY(1,1) PRIMARY KEY,
[Name] VARCHAR(100) NOT NULL,
[Description] VARCHAR(200) NULL,
[Modified] DATETIME NOT NULL DEFAULT(GETUTCDATE()),
[ModifiedBy] VARCHAR(32) NOT NULL
);
INSERT INTO [dbo].[Product]([Name], [Description], [Modified], [ModifiedBy])
VALUES('Test', 'Description', GETUTCDATE(), 'Me');

Don't do anything just try to insert (' ')single quotes instead of (") quotes.. or if you insert data with ('')single quotes try for (") double quotes it's really helping you.

Related

SQL Insert - String or binary data would be truncated for INT values

I'm getting the "String or binary data would be truncated" error when trying to insert integers to one of my tables.
I've read several post about the length of the column vs the length of the value one is inserting, but it doesn't seem to be my case once the columns are all int or smallint type and the values are all maximum two digits.
The table structure is the following:
CREATE TABLE [tblvUserLocation] (
[User_Location_ID] [int] IDENTITY (1, 1) NOT NULL ,
[Location_ID] [int] NULL ,
[Line_Type_ID] [int] NULL ,
[User_ID] [int] NULL ,
[Active] [smallint] NULL CONSTRAINT [DF_tblvUserLocation_Active] DEFAULT (1),
[Last_Updated] [smalldatetime] NULL CONSTRAINT [DF_tblvUserLocation_Last_Updated] DEFAULT (getdate()),
[Last_Updated_By] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_tblvUserLocation_Last_Updated_By] DEFAULT (suser_sname())
) ON [PRIMARY]
GO
The insert I'm trying to run is the following:
insert into tblvUserLocation (Location_ID, Line_Type_ID, [User_ID], Active)
values (20, 2, 41, 1)
And the error I'm getting is the following:
Server: Msg 8152, Level 16, State 2, Line 1 String or binary data
would be truncated. The statement has been terminated.
If that makes any difference, I'm using SQL Server 2000.
Please let me know what your thoughts are.
Thanks!
Looks like the problem comes from your [DF_tblvUserLocation_Last_Updated_By] constraint.
It's pulling the current username which is more than likely longer than the length of your [Last_Updated_By] column VARCHAR(10).
Update your DDL to:
[Last_Updated_By] [varchar] (128)

SQL server Invalid Column name Invalid object name

I'm having a problem with a table I created. I am trying to run a query however a red line appears under my code ('excursionID', and 'excursions'), claiming 'Invalid Column name 'excursionID' and 'Invalid object name 'dbo.excursions' even though I have created the table already!
Here is the query
SELECT
excursionID
FROM [dbo].[excursions]
Here is the query I used to create the table
USE [zachtravelagency]
CREATE TABLE excursions (
[excursionID] INTEGER NOT NULL IDENTITY (1,1) PRIMARY KEY,
[companyName] NVARCHAR (30) NOT NULL,
[location] NVARCHAR (30) NOT NULL,
[description] NVARCHAR (30) NOT NULL,
[date] DATE NOT NULL,
[totalCost] DECIMAL NOT NULL,
I've tried dropping the table and inserting table again.
For some reason all my other tables work, it's just this table that doesn't identify itself. I'm very new to SQL so thank you for your patience!
You use DB [zachtravelagency] for create table.And You dont use this DB in your query. Default used db master in SSMS. Try
SELECT
excursionID
FROM [zachtravelagency].[excursions]

Error when inserting into a simple two column table with SQL Server 2012?

I am running the following:
insert into [authentication].[dbo].[AspNetUserRoles] ("RoleId","UserId")
values ("0918fb0f-79c0-4298-b184-9a754dc5c30e", "527ddbd5-14d3-4fb9-a7ae-374e66f635d4")
Here's my table:
CREATE TABLE [dbo].[AspNetUserRoles] (
[RoleId] NVARCHAR (128) NOT NULL,
[UserId] NVARCHAR (128) NOT NULL
);
However it's giving me an error saying:
Msg 207, Level 16, State 1, Line 2
Invalid column name '0918fb0f-79c0-4298-b184-9a754dc5c30e'.
Is there something wrong with the way I have coded the insert ?
Put the values in '' not "":
insert into [authentication].[dbo].[AspNetUserRoles] ("RoleId","UserId")
values ('0918fb0f-79c0-4298-b184-9a754dc5c30e',
'527ddbd5-14d3-4fb9-a7ae-374e66f635d4');
The problem is that the values with "" around them are treated as identifiers not literals. Like in "RoleId","UserId".

Can't create new table

I'm new to SQL Server 2000 and face a problem. I want to make a new table but I encounter an error message with the following code:
create table Buku
(
Kode_Buku char(5) constraint PK_Kode_Buku Primary Key,
Judul_Buku varchar(10)not null,
Nama_Pengarang varchar(30) not null,
Penerbit varchar(30),
Kota_Terbit varchar(30) default,
Tahun_Terbit varchar(4) default,
Bahasa varchar(4) check,
Harga_Jual money,
)
and here's the error code:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'Buku'.
You have three problems:
You have multiple cases where you say default but don't specify anything
You have a check but don't specify anything
Your last column definition says money, with a trailing comma
Now, none of these lead to the exact error message you're getting, so maybe there is more you're not telling us (is there more code before the create table bit?), but these little syntax problems are far too localized to be useful in this Q & A format.
EDIT
I just ran this on a SQL Server 2000 instance and it worked just fine:
create table Buku
(
Kode_Buku char(5) constraint PK_Kode_Buku Primary Key,
Judul_Buku varchar(10) not null, -- added space here
Nama_Pengarang varchar(30) not null,
Penerbit varchar(30),
Kota_Terbit varchar(30), -- removed default here
Tahun_Terbit varchar(4), -- removed default here
Bahasa varchar(4), -- removed check here
Harga_Jual money -- removed comma here
)
So I'm not sure what you're doing differently, but I can't get the error message you are seeing with the information you've provided in the question. If you're still getting an error message with this code (and only this code), you'll need to provide more information, such as ##VERSION, what interface you're using to submit the create table statement to SQL Server, etc.

Error while altering table in SQL Server

I'm just trying to add 2 columns to customer table
alter table CUSTOMER ADD ( CUSTOMERNAME VARCHAR(255) NULL
, CUSTOMERDESC VARCHAR(30) NULL )
but I get the following error when I try to run the script,
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '('.
Can someone tell me what is the mistake that I'm doing. Thanks in anticipation
You don't need the brackets for an alter statement so remove them. ie:
alter table CUSTOMER ADD CUSTOMERNAME VARCHAR(255) NULL,
CUSTOMERDESC VARCHAR(30) NULL
You don't need any ( before the column names - just use:
ALTER TABLE dbo.Customer
ADD CustomerName VARCHAR(255) NULL, CustomerDesc VARCHAR(30) NULL
Does that work?