我已经浏览了其他几个此类问题,但没有找到一个可以帮助我解决此问题的问题。我想做的是:我想为所有员工创建一个表,以分配EmployeeID将在其他多个表中使用的表。那张桌子和其他桌子工作正常。当我尝试基于该表创建新表时,就会出现我的问题,EmployeeType这样我就可以仅基于特定类型的员工来调出信息。在SELECT所有三个表的语句上,我得到此错误:
EmployeeID
EmployeeType
SELECT
‘(’附近的语法不正确。期望的ID。
我一直在搜索如何解决它,但是我没有尝试过。我想念什么?
CREATE TABLE Employees ( EmployeeID int NOT NULL PRIMARY KEY IDENTITY, EmpFirstName char(50) NOT NULL, EmpLastName char(50) NOT NULL, EmpAddress varchar(50) NOT NULL, EmpCity char(50) NOT NULL, EmpState char(2) NOT NULL, EmpZipCode varchar(10) NOT NULL, EmpPhone varchar(12) NOT NULL, EmpJobTitle char(30) NOT NULL, EmployeeType char(30) NOT NULL, Salary money NOT NULL, HoursPerWeek int NOT NULL, ); CREATE TABLE Collective AS (SELECT * FROM [dbo].[Employees] WHERE EmployeeID = Employees.EmployeeID AND EmployeeType = 'Collective'); CREATE TABLE PaidStaff AS (SELECT EmployeeID AS ReviewerID, EmpFirstName, EmpLastName, EmpAddress, EmpCity, EmpState, EmpZipCode, EmpPhone, EmpJobTitle Salary, HoursPerWeek FROM Employees WHERE EmployeeID = Employees.EmployeeID AND EmployeeType = 'PaidStaff'); CREATE TABLE Volunteers AS (SELECT EmployeeID AS ReviewerID, EmpFirstName, EmpLastName, EmpAddress, EmpCity, EmpState, EmpZipCode, EmpPhone, EmpJobTitle FROM Employees WHERE EmployeeType = 'Volunteer');
SQL Server没有CREATE TABLE .... AS (SELECT ...功能。那只是无效的T-SQL语法-因此出错。
CREATE TABLE .... AS (SELECT ...
如果要从创建 新表 (尚不存在!)SELECT,则需要使用以下语法:
SELECT EmployeeID AS ReviewerID, EmpFirstName, EmpLastName, EmpAddress, EmpCity, EmpState, EmpZipCode, EmpPhone, EmpJobTitle INTO dbo.Volunteers FROM dbo.Employees WHERE EmployeeType = 'Volunteer';
这 只是 -如果新表的工作原理dbo.Volunteers- 还不存在 。
dbo.Volunteers
如果需要在 现有表中 插入行,则需要使用以下语法:
INSERT INTO dbo.Volunteers (list-of-columns) SELECT (list-of-columns) FROM dbo.Employees WHERE EmployeeType = 'Volunteer';