2010-06-29 107 views
4

我必须建立一个数据库,创建表并插入信息:插入到SQL Server

我有数据库和表,但是当我插入我有问题...

任何帮助将是赞赏。这是我第一次尝试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 & Managers', 
'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') 
+2

有一天我的编辑权利会到来。 – amelvin 2010-06-29 00:43:55

回答

2

那么首先,你需要insert语句中的每个'values'子句的insert语句。在JOB_TITLE表

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'); 

你EEO_1_Classification柱为varchar(30)太短,让VARCHAR(200)或一些大一点。

您的位列(Job_Title中的第四个)接受0和1的值 - 不要在值周围加引号。

+2

+1只需添加一下,如果SQL Server 2008可以用逗号分隔不同的插入行而不是重复整个语句。 http://blog.sqlauthority.com/2008/07/02/sql-server-2008-insert-multiple-records-using-one-insert-statement-use-of-row-constructor/ – 2010-06-29 00:53:29

+0

非常感谢!我做了更改......当我执行插入时出现: 消息208,级别16,状态1,行9 无效的对象名称'Job_title'。 这是因为我的表是Job_title,第一列是Job_title发生冲突? – rhonda 2010-06-29 00:56:51

+0

谢谢大家 – rhonda 2010-06-29 01:48:49

2

您需要在CREATE TABLEINSERT INTO之间执行GO操作。会发生什么情况是SQL Server批量编译请求,批处理是在一个请求中发送完整的SQL文本。 SQL Server Management Studio(用于编辑查询的工具)使用魔术字GO作为批分隔符。在CREATE和INSERT之间没有GO,这两个语句都是同一批次的一部分,SQL Server试图将它们一起编译。当它尝试这样做时,INSERT语句将失败,因为该表尚不存在。由于该批未能编译,所以CREATE也不会执行。如果您在CREATE和INSERT之间添加了一个GO,那么您将发送两个批次到服务器,一个使用CREATE,另一个使用INSERT。 CREATE将成功,然后当INSERT正在编译时,该表将存在并且它将成功编译。

我知道与源代码和编译的正常思维方式有点不同,但在SQL字中,编译本身依赖于数据库的内容(元数据),这就是最终触发器错误。