SQL过程获取错误

时间:2014-03-13 13:46:37

标签: sql sql-server

我正在尝试在SQL中创建一个简单的过程,我需要从一个表中复制数据并将其存储到实例表中。我写了这段代码但是我收到了错误。这是我的代码

ALTER PROCEDURE dbo.ShaniProc -- Always apend the schema
AS
DECLARE @Driver nvarchar(20)

DECLARE @active_surveys TABLE
    (
    active_id int IDENTITY(1,1) PRIMARY KEY,
    ani varchar,
    callDisposition int,
    CallTypeID int,
    [DateTIme] datetime, -- [Datetime]  is a really bad idea for a column name, but 
                         -- if you want to use it, you need to enclose it in square brackets
    DbDateTime datetime,
    TalkTime int,
    DNIS varchar(32),
    Duration int,
    PerpheralID smallint,
    RecoveryKey float
    )

Insert into @active_surveys (active_id,ani,callDisposition,CallTypeID,[DateTime],DbDateTime,TalkTime,DNIS,Duration,PerpheralID,RecoveryKey ) 
-- You shouldn't use VALUES if you are doing a SELECT for inserting rows
Select t.AgentSkillTargetID,t.ANI,t.CallDisposition,t.CallTypeID,t.[DateTime],t.DbDateTime,t.TalkTime,
t.DNIS,t.Duration,t.PeripheralID,t.RecoveryKey from TCD t;

Declare @total int;
Select @total=COUNT(*) from @active_surveys;
print '>> Total Surveys: '+ISNULL(CONVERT(varchar(10),@total),'')

我收到此错误

Msg 156, Level 15, State 1, Procedure ShaniProc, Line 26
Incorrect syntax near the keyword 'Select'.
Msg 102, Level 15, State 1, Procedure ShaniProc, Line 28
Incorrect syntax near ')'.

4 个答案:

答案 0 :(得分:4)

您的查询不仅有一个问题:

ALTER PROCEDURE dbo.ShaniProc -- Always apend the schema
AS
DECLARE @Driver nvarchar(20)

DECLARE @active_surveys TABLE
    (
    active_id int PRIMARY KEY,
    ani varchar,
    callDisposition int,
    CallTypeID int,
    [DateTIme] datetime, -- [Datetime]  is a really bad idea for a column name, but 
                         -- if you want to use it, you need to enclose it in square brackets
    DbDateTime datetime,
    TalkTime int,
    DNIS varchar(32),
    Duration int,
    PerpheralID smallint,
    RecoveryKey float
    )


Insert into @active_surveys (active_id,ani,callDisposition,CallTypeID,[DateTime],DbDateTime,TalkTime,DNIS,Duration,PerpheralID,RecoveryKey ) 
-- You shouldn't use VALUES if you are doing a SELECT for inserting rows
Select t.AgentSkillTargetID,t.ANI,t.CallDisposition,t.CallTypeID,t.[DateTime],t.DbDateTime,t.TalkTime,
t.DNIS,t.Duration,t.PeripheralID,t.RecoveryKey from TCD t;

Declare @total int;
Select @total=COUNT(*) from @active_surveys;
print '>> Total Surveys: '+ISNULL(CONVERT(varchar(10),@total),'')

另外,正如HLGEM指出的那样,为什么在变量表上创建IDENTITY列然后不使用它?有什么意义呢?

答案 1 :(得分:0)

ALTER PROCEDURE ShaniProc
AS
DECLARE @Driver nvarchar(20)



DECLARE @active_surveys TABLE
    (
    active_id int IDENTITY(1,1) PRIMARY KEY,
    ani varchar,
    callDisposition int,
    CallTypeID int,
    DateTIme datetime,
    DbDateTime datetime,
    TalkTime int,
    DNIS varchar(32),
    Duration int,
    PerpheralID smallint,
    RecoveryKey float

    )
SET IDENTITY_INSERT active_surveys On

Insert into @active_surveys (active_id,ani,callDisposition,CallTypeID,DateTime,DbDateTime,TalkTime,DNIS,Duration,PerpheralID,RecoveryKey )
Select t.AgentSkillTargetID,t.ANI,t.CallDisposition,t.CallTypeID,t.DateTime,t.DbDateTime,t.TalkTime,
t.DNIS,t.Duration,t.PeripheralID,t.RecoveryKey from TCD t

SET IDENTITY_INSERT active_surveys OFF
Declare @total int;
Select @total=COUNT(*) from @active_surveys;
print '>> Total Surveys: '+ISNULL(CONVERT(varchar(10),@total),'')

GO

答案 2 :(得分:0)

将插入语句更改为

INSERT INTO @active_surveys (
    active_id
    , ani
    , callDisposition
    , CallTypeID
    , DATETIME
    , DbDateTime
    , TalkTime
    , DNIS
    , Duration
    , PerpheralID
    , RecoveryKey
    )

    SELECT t.AgentSkillTargetID
    , t.ANI
    , t.CallDisposition
    , t.CallTypeID
    , t.DATETIME
    , t.DbDateTime
    , t.TalkTime
    , t.DNIS
    , t.Duration
    , t.PeripheralID
    , t.RecoveryKey FROM TCD t;

答案 3 :(得分:0)

试试这个..

ALTER PROCEDURE ShaniProc
AS
DECLARE @Driver nvarchar(20)



DECLARE @active_surveys TABLE
    (
    active_id int IDENTITY(1,1) PRIMARY KEY,
    ani varchar,
    callDisposition int,
    CallTypeID int,
    DateTIme datetime,
    DbDateTime datetime,
    TalkTime int,
    DNIS varchar(32),
    Duration int,
    PerpheralID smallint,
    RecoveryKey float

    )
SET IDENTITY_INSERT active_surveys On

Insert into @active_surveys (active_id,ani,callDisposition,CallTypeID,DateTime,DbDateTime,TalkTime,DNIS,Duration,PerpheralID,RecoveryKey )
Select t.AgentSkillTargetID,t.ANI,t.CallDisposition,t.CallTypeID,t.DateTime,t.DbDateTime,t.TalkTime,
t.DNIS,t.Duration,t.PeripheralID,t.RecoveryKey from TCD t;
SET IDENTITY_INSERT active_surveys OFF
Declare @total int;
Select @total=COUNT(*) from @active_surveys;
print '>> Total Surveys: '+ISNULL(CONVERT(varchar(10),@total),'')

GO