2010-06-29 285 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字中,編譯本身依賴於數據庫的內容(元數據),這就是最終觸發器錯誤。