得到错误不正确的语法我在做什么错

时间:2019-08-14 18:47:46

标签: sql

我从此代码中得到一个错误:

DECLARE @New_Hospitalstaff TABLE 
        (
            [NAME] VARCHAR(50),
            JOB VARCHAR(50), 
            HIREDATE DATETIME,
            CITY VARCHAR(50),
            [STATE] VARCHAR(50)
        )

INSERT INTO @New_Hospitalstaff (NAME, JOB, HIREDATE, CITY, STATE)
    SELECT 
        (SUBSTRING(Name, 1, (CHARINDEX('_', Name) - 1,))), AS Name
        (SUBSTRING(JoB, (CHARINDEX('_'job) + 1, 15)) AS JobHireDate,
        (SUBSTRING(Location, 1, (CHARINDEX('_', Location) - 1))) AS City
        (SUBSTRING(Location, CHARINDEX('_', Location) + 1, 10)) AS State 
    FROM 
        DBO.HospitalStaff

SELECT *
FROM @New_Hospitalstaff

1 个答案:

答案 0 :(得分:3)

您遇到很多语法错误。我认为格式化代码并密切注意括号将对您有所帮助。看看这个,并与您进行比较。

DECLARE @New_Hospitalstaff TABLE ( [NAME] varchar(50) , [JOB] varchar(50) , [HIREDATE] datetime , 
                                   [CITY] varchar(50) , [STATE] varchar(50) )

INSERT INTO @New_Hospitalstaff(NAME,JOB,HIREDATE,CITY,STATE ) 
SELECT (SUBSTRING(Name,1, (CHARINDEX('',Name)-1))) AS Name, 
       -- Missing [JOB] field in query. Needed in order for INSERT to work. 
       -- You probably want the whole field so: JOB, 
       (SUBSTRING(JOB, (CHARINDEX('', job)+1),15)) AS JobHireDate, 
       (SUBSTRING(Location,1, (CHARINDEX('',Location)-1))) As City,
       (SUBSTRING(Location, CHARINDEX('',Location)+1,10)) As State
FROM DBO.HospitalStaff 

SELECT * FROM @New_Hospitalstaff

我根据调用的函数修复了逗号不正确,缺少括号和多余的括号的问题。