jcoke
jcoke

Reputation: 1891

Column name or number of supplied values does not match table

IF EXISTS ( SELECT [name] FROM sys.databases WHERE [name] = 'BlogDB' )
DROP DATABASE BlogDB
GO

CREATE DATABASE BlogDB
GO

IF EXISTS ( SELECT [name] FROM sys.tables WHERE [name] = 'Editor' )
DROP TABLE Editor
GO

CREATE TABLE Editor 
(
    UsernameID VARCHAR (30),
    EMail VARCHAR (30),
    DateOfBirth VARCHAR(30),
    BlogTitle VARCHAR(30),
    PRIMARY KEY(UsernameID)
);

INSERT INTO Editor VALUES ('Mdbuzzer', '[email protected]', '1995/03/15', 'Nearly Bound');
INSERT INTO Editor VALUES ('Kally32', '[email protected]', '1993/10/13', 'Tomorrows War');

SELECT * FROM Editor;

IF EXISTS ( SELECT [name] FROM sys.tables WHERE [name] = 'Post' )
DROP TABLE Post
GO

CREATE TABLE Post
(
   UsernameID VARCHAR(30),
   BlogTitle VARCHAR (30),
   PostTitle VARCHAR (30),
   Category VARCHAR (20),
   TimeofPost VARCHAR (10)
);

INSERT INTO Post VALUES ('Kally32','Tomorrows War', 'Mystery', 'General', '12:03pm');
INSERT INTO Post VALUES ('Kally32','Tomorrows War', 'Let It Shine','Musing','10:05pm');
INSERT INTO Post VALUES ('Kally32', 'Tomorrows War','Two Can Play That Game','Work', '12:00pm');
INSERT INTO Post VALUES ('Mdbuzzer','Nearly Bound', 'Goal','Social', '10:05pm');
INSERT INTO Post VALUES ('Mdbuzzer', 'Nearly Bound','Life After Death','General', '12:03pm');
INSERT INTO Post VALUES ('Mdbuzzer', 'Nearly Bound','Times Up','Work', '14:06');

SELECT * FROM Post;

IF EXISTS ( SELECT [name] FROM sys.tables WHERE [name] = 'Comments' )
DROP TABLE Comments
GO

CREATE TABLE Comments
(
    UsernameID VARCHAR (30),
    PostTitle VARCHAR (50),
    Comments VARCHAR (100),
    TimeofComments VARCHAR (10)
);

INSERT INTO Comments VALUES ('Kally32','Mystery', 'Highly Recommend', '18:36pm'); 
INSERT INTO Comments VALUES ('Kally32','Let It Shine', 'Touching story', '19:46pm');
INSERT INTO Comments VALUES ('Kally32','Two Can Play That Game', 'Truly Remarkable', '13:40pm');
INSERT INTO Comments VALUES ('Kally32','Let It Shine', 'Amazing Ending', '09:36am');
INSERT INTO Comments VALUES ('Kally32','Mystery', 'Highly Recommend', '20:36pm');
INSERT INTO Comments VALUES ('Mdbuzzer','Goal', 'I love the team spirit at the end', '18:36pm');
INSERT INTO Comments VALUES ('Mdbuzzer','Life After Death','Very Emotional', '08:40am');
INSERT INTO Comments VALUES ('Mdbuzzer','Times Up', 'Brings back memories', '23:16pm');
INSERT INTO Comments VALUES ('Mdbuzzer','Times Up', 'Best post I have seen in a while', '22:55pm', '22:00pm');
INSERT INTO Comments VALUES ('Mdbuzzer','Life After Death', 'A touching story', '09:50am', '07:00am');

SELECT * FROM Comments;

I keep getting an error when I try and execute the script I'm not sure what the problem is, I would be grateful if someone could help me. It says the error is on

Level 16, State 1, Line 25

Upvotes: 0

Views: 30

Answers (1)

Ben Reich
Ben Reich

Reputation: 16324

Here:

INSERT INTO Comments VALUES
('Mdbuzzer','Times Up', 'Best post I have seen in a while', '22:55pm', '22:00pm');

You are inserting 5 values into a table with 4 columns.

Sometimes using the more verbose syntax can help both with organization and error messages:

INSERT INTO Comments(UsernameID, PostTitle, Comments, TimeofComments)
VALUES('Mdbuzzer','Times Up', 'Best post I have seen in a while', '22:55pm', '22:00pm');

Upvotes: 2

Related Questions