Solved

Basic T-SQL Script Failure

Posted on 2016-09-06
7
60 Views
Last Modified: 2016-09-07
Points of My Scenario:

1. I am studying T-SQL as a newbie/novice.

2. I have successfully created three tables as per commands in attached SQL script, CREATE-Tables.txt

3. I attempt to perform a data insert into the Sales table with the following SQL statement: " INSERT Sales SELECT NEWID(),1, 1, 4, '02/01/2012' " - without the quotes (" "), of course! :-)

4. I receive the following error: "Msg 213, Level 16, State 1, Line 1. Column name or number of supplied values does not match table definition."

QUESTION/REQUEST: Please correct the syntax in my T-SQL statement or advise me what I am doing wrongly. Remember that I am a newbie! :-)
CREATE-Tables.txt
0
Comment
Question by:waltforbes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 23

Assisted Solution

by:Snarf0001
Snarf0001 earned 150 total points
ID: 41786851
You have more columns in your insert than the table has, as per the error message.
The table you defined for sales has 4 columns:

SaleID
ProductID
EmployeeID
Quantity

But your insert statement also has a date appended to the end which doesn't exist on the table.
Inserting 5 columns into a table with 4.
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 300 total points
ID: 41786855
Your CREATE TABLE Sales script has four columns, and your INSERT INTO Sales script has five columns.
An INSERT has to have an equal number of columns in the INSERT clause and the SELECT clause.

CREATE TABLE Sales (
	SaleID uniqueidentifier NOT NULL,
	ProductID int NOT NULL,
	EmployeeID int NOT NULL,
	Quantity smallint NOT NULL)

INSERT Sales
SELECT NEWID(), 1, 1, 4, '02/01/2012'  

Open in new window

Since I don't see a date column, guessing you want to do this..
INSERT INTO Sales(SaleID, ProductID, EmployeeID, Quantity) 
VALUES (NEWID(), 1, 1, 4) 

Open in new window

Also, if you're inserting specific values you have to use VALUES instead of SELECT.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41786878
As an aside, what's the purpose of SaleID being a uniqueidentifier instead of an int with or without an identity?
0
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 50 total points
ID: 41787023
NB: It is "best practice" to specify the columns within the insert statement. If you do not do that then you will have problems aligning the data to the correct table column.

Another matter you should learn early is to NEVER assume mm/dd/yyyy is the date firmat. In TSQL YYYYMMDD is the safest.
0
 

Author Closing Comment

by:waltforbes
ID: 41787057
You guys are so awesome here! You have totally resolved my issue. I am happy & very grateful.
I decided to add another column to the Sales table named 'PurchaseDate' with the data type of datetime.

@Jim Horn: I used the uniqueidentifier data type in the Sales table, just for experimentation with data types. I am studying with CBT Nuggets, and was going along with the lesson. Alas! they supplied bad scripts! It wasn't even in my paradigm that it was possible that their script was wrong - after all, they demonstrated it! Oh well.

Thank goodness for Experts Exchange! I am much grateful.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41787059
Ok.  Most places I've seen will use an int identity(1,1) and it's good enough.  That and it's four bytes in size vs. 16-bytes for a GUID, so the GUID takes up four times the memory and would take longer to load.

Thanks for the split, good luck with your learning.  Feel free to ask questions here anytime.
0
 

Author Comment

by:waltforbes
ID: 41787867
'Hello world' - and hear ye, world: wonderful people live at Experts Exchange! Yaaay! :-D
ThumbsUpEE.PNG
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question