'('。期待ID)附近的语法不正确

时间:2017-05-12 16:40:20

标签: sql sql-server tsql

我已经查看了其他几个此类问题,但没有找到一个能帮我解决这个问题的问题。我想要做的是:我想为所有员工创建一个表,以便分配将在其他几个表中使用的EmployeeID。该表和其他表工作正常。当我尝试基于EmployeeType创建新表时,我的问题出现了,因此我可以仅根据特定类型的员工提供信息。在所有三个表的SELECT语句中,我收到此错误:

  

'('。期待ID。

附近的语法不正确

我已经用Google搜索并搜索了如何解决它,但我尝试过的任何工作都没有。我错过了什么?

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

2 个答案:

答案 0 :(得分:7)

SQL Server没有CREATE TABLE .... AS (SELECT ...功能。这不是有效的T-SQL语法 - 因此是错误。

如果您想从SELECT创建新表格(目前还不存在!),则需要使用以下语法:

SELECT
    EmployeeID AS ReviewerID,
    EmpFirstName,
    EmpLastName,
    EmpAddress,
    EmpCity,
    EmpState,
    EmpZipCode,
    EmpPhone,
    EmpJobTitle
INTO 
    dbo.Volunteers    
FROM    
    dbo.Employees
WHERE
    EmployeeType = 'Volunteer';

如果新表格dbo.Volunteers - 尚不存在,则此 有效。

如果需要在现有表中插入行,则需要使用以下语法:

INSERT INTO dbo.Volunteers (list-of-columns)
    SELECT (list-of-columns)
    FROM dbo.Employees
    WHERE EmployeeType = 'Volunteer';

答案 1 :(得分:2)

我会说你应该使用视图而不是创建额外的表。

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
);
go
CREATE view Collective AS
    (SELECT
        *
    FROM    
        [dbo].[Employees]
    WHERE
        --EmployeeID = Employees.EmployeeID AND /* this does not do anything */
        EmployeeType = 'Collective');
go
CREATE view PaidStaff AS
    (SELECT
        EmployeeID AS ReviewerID,
        EmpFirstName,
        EmpLastName,
        EmpAddress,
        EmpCity,
        EmpState,
        EmpZipCode,
        EmpPhone,
        EmpJobTitle
        Salary,
        HoursPerWeek
    FROM    
        Employees
    WHERE
        --EmployeeID = Employees.EmployeeID AND /* this does not do anything */
        EmployeeType = 'PaidStaff');
go
CREATE view Volunteers AS
    (SELECT
        EmployeeID AS ReviewerID,
        EmpFirstName,
        EmpLastName,
        EmpAddress,
        EmpCity,
        EmpState,
        EmpZipCode,
        EmpPhone,
        EmpJobTitle
    FROM    
        Employees
    WHERE
        EmployeeType = 'Volunteer');