Pregunta

I'm trying to learn SQL Server and I'm practicing INSERT INTO.

When I try to enter this query, it throws an error saying that [Intern_DB2].[dbo].[MY_Parcels] is an invalid object.

I had no problems creating this table, why is it saying its invalid all of a sudden?

Insert INTO [INTERN_DB2].[dbo].[My_Parcels] (ID)
Values (000065, N'test') 
¿Fue útil?

Solución

You have two problems with your query .

1st

Insert INTO [INTERN_DB2].[dbo].[My_Parcels] (ID)  --<-- Number of columns 1
Values (000065, N'test')                          --<-- Passed values for two columns

The number of columns mentioned in your INSERT INTO statement must match the number of values passed in VALUES cluase.

2nd

You are getting Error message something along the lines invalid object which means you have not used the correct Table name or you have created that table in some other database and looking for it in some other database

to check this try Executing the following statement and see if table exists

USE INTERN_DB2
GO
SELECT * FROM [dbo].[My_Parcels]
GO

Once you have made sure the tables exists , just correct the number of values passed in VALUES claues of your insert statement and it should work.

Otros consejos

You have to make sure that table is created inside [INTERN_DB2] database that you are specifying in your insert statement.

Locate your table using GUI, right click select Script table as then select Drop and CREATE to then select New query Editor window. Once it opens the new query windows verify first line to show USE [INTERN_DB2] if it says something else than you created it in wrong database.

In your INSERT statement you have listed 1 column, but in VALUES you are specifying two different columns. If you want to insert values into two columns, you need to list them out or if you only have two columns than you can remove columns and just add values. This is dangerous because you are assuming the order.

USE AdventureWorks2008R2;
GO
INSERT INTO Production.UnitMeasure
VALUES (N'FT', N'Feet', '20080414');
GO

USE AdventureWorks2008R2;
GO
INSERT INTO Production.UnitMeasure (Name, UnitMeasureCode,
    ModifiedDate)
VALUES (N'Square Yards', N'Y2', GETDATE());
GO
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top