Insert Into SQL server
I have to create a database, create tables and insert information:
I have the database and tables but when I insert I'm having problems...
Any help would be appreciated. It is my first attempt at SQL.
/* This script creates the SQL database and tables
** for Kudler Fine Foods
** Human Resources
*/
CREATE DATABASE Mor ;
Go
USE Mor
CREATE TABLE Employee (
Emp_id int NOT NULL IDENTITY(1,1),
Last_name varchar(25),
First_name varchar(25),
Address varchar(40),
City varchar (15),
State char(2),
Telephone_area_code varchar(3),
Telephone_number varchar(8),
Job_title varchar(50),
Hire_date smalldatetime,
Wage money,
Gender char(1),
Race varchar(25),
Age int );
CREATE TABLE Job_title (
Job_title varchar (50) PRIMARY KEY,
EEO_1_Classification varchar(30),
Job_description varchar(250),
Exempt_Non_Exempt_Status bit );
/* This script inserts values into Job_title table
** (Note: 1 means exempt (salaried)
** 0 means non-exempt (hourly)
*/
INSERT INTO Job_title
VALUES
('Accounting Clerk', 'Office/Clerical',
'Computes, classifies, records, and verifies numerical data for use in maintaining
accounting records.',
'0');
VALUES
('Assistant Manager', 'Officials & Managers',
'Supervises and coordinates activities of workers in department of food store.
Assists store manager in daily operations of store.' ,
'1');
VALUES
('Bagger','Sales Workers',
'Places customer orders in bags. Performs carryout duties for customers.',
'0');
VALUES
('Cashier','Sales Workers',
'Operates cash register to itemize and total customer’s purchases in grocery
store.',
'0');
VALUES
('Computer Support Specialist','Technician',
'Installs, modifies, and makes minor repairs to personal computer hardware and
software systems, and provides technical assistance and training to system
users.',
'0');
VALUES
('Dir. of Fin. & Acct.','Officials & M开发者_StackOverflow社区anagers',
'Plans and directs the finance and accounting activities for Kudler Fine Foods.',
'1');
VALUES
('Asst. - Bakery & Pastry','Craft Workers (Skilled)',
'Obtains or prepares food items requested by customers in retail food store.',
'0');
VALUES
('Asst. - Butchers & Seafood Specialists','Operatives (Semi skilled)',
'Obtains or prepares food items requested by customers in retail food store.',
'0');
VALUES
('Stocker','Office/Clerical',
'Stores, prices and restocks merchandise displays in store.',
'0')
Well to start with, you need an insert statement for each 'values' clause in the insert statements.
INSERT INTO Job_title
(Job_title, EEO_1_Classification, Job_description, Exempt_Non_Exempt_Status )
VALUES ('Accounting Clerk', 'Office/Clerical', 'Computes, classifies, records, and verifies numerical data for use in maintaining accounting records.', '0');
INSERT INTO Job_title
(Job_title, EEO_1_Classification, Job_description, Exempt_Non_Exempt_Status )
VALUES ('Assistant Manager', 'Officials & Managers', 'Supervises and coordinates activities of workers in department of food store. Assists store manager in daily operations of store.' , '1');
Your EEO_1_Classification column in the Job_Title table is varchar(30) which is too short, make it varchar(200) or something a bit bigger.
Your bit column (the fourth on Job_Title) accepts values of 0 and 1 - don't put quotes around the values.
You need a GO between the CREATE TABLE
and INSERT INTO
. What happens is that the SQL Server compiles requests in batches, a batch being the entire SQL text send in one single request. The SQL Server Management Studio (the tool you use to edit the queries) uses the magic word GO
as a batch separator. W/o a GO between CREATE and INSERT, both statements are part of the same batch and the SQL Server tries to compile them together. When it attempts this, the INSERT statement will fail, because the table does not yet exists. Since the batch failed to compile, the CREATE is not executed either. If you add a GO between the CREATE and the INSERT then you'll be sending two batches to the server, one with the CREATE and one with the INSERT. The CREATE will succeed, and then when the INSERT is being compiled the table will exists and it will compile successfully.
I know is a little bit different from the normal way of thinking about source code and compilation, but in SQL word the compilation itself depends on the content of the database (the metadata), and this is what ultimately triggers the error.
精彩评论