使用标识列在临时表中插入新记录

时间:2017-02-08 07:11:17

标签: sql tsql

 create procedure Task_request @Tstart datetime, @Tend datetime
  as 
DECLARE @dummy TABLE( ID INT Identity, task_number nvarchar(50) ,
Pad_id  VARCHAR(10)   ,startdate datetime,enddate datetime,
  previous_value float, next_value float,DCDC_OUTPUT_AVG float,DOWNTIME_IN_MINS  int )
declare @pdate datetime
declare @previous_value FLOAT
declare @next_value FLOAT
DECLARE @end_time datetime
declare @loopdate datetime
declare @padid varchar(10)
declare @dc_dc float
 declare @tasknumber nvarchar(50)
set @loopdate = '2001-01-01'
set @pdate = '2016-10-01 01:00:00'

SET @tasknumber = '0'

while exists ( select   DCDC_OUTPUT_AVG,TASK_NUMBER
from I_TASK T, I_HOURLY_DATA F,I_DATE D, I_SITE_LOCATION P
where F.D_DATE = D.ID AND F.D_PWM_LOCATION = P.ID 
AND T.SITE_ID like P.NAME+'%' 

AND D.DATE_KEY between cast(TASK_CREATION_DATE as date) 
AND dateadd("d",1,CAST( coalesce(ACCEPTED_DATE, working_date) AS DATE)) 
and task_priority > '01' and TASK_PRIORITY <'06' 
and TASK_CLOSED_DATE between @Tstart and @Tend 
 and TASK_NUMBER > @tasknumber
 )

Begin 

select top 1  @pdate=DATEADD(hour, D_TIME_OF_DAY-1, d.date_key),
@tasknumber     = t.TASK_NUMBER
from I_TASK T, I_HOURLY_DATA F,I_DATE D, I_SITE_LOCATION P
 where F.D_DATE = D.ID AND F.D_PWM_LOCATION = P.ID 
 AND T.SITE_ID like P.NAME+'%' 
AND D.DATE_KEY between cast(TASK_CREATION_DATE as date)  
AND dateadd("d",1,CAST( coalesce(ACCEPTED_DATE, working_date) AS DATE)) 
and task_priority > '01' and TASK_PRIORITY <'06' 
and TASK_CLOSED_DATE between @Tstart and @Tend 
and DOWNTIME_IN_MINS  >0
and TASK_NUMBER > @tasknumber
order by task_number, SITE_ID, TASK_CREATION_DATE, d.Date_key, D_TIME_OF_DAY


 print 'testing'
 print @pdate
 print @tasknumber

 select top 1  @previous_value=DCDC_OUTPUT_AVG
 from I_TASK T, I_HOURLY_DATA F,I_DATE D, I_SITE_LOCATION P
where F.D_DATE = D.ID AND F.D_PWM_LOCATION = P.ID 
 AND T.SITE_ID like   P.NAME+'%' 
AND D.DATE_KEY between cast(TASK_CREATION_DATE as date)  AND 
dateadd("d",1,CAST( coalesce(ACCEPTED_DATE, working_date) AS DATE)) and
task_priority > '01' and TASK_PRIORITY <'06' 
and TASK_CLOSED_DATE between @Tstart and '2016-11-01'  
   AND DATEADD(hour, D_TIME_OF_DAY-1, d.date_key) =                DATEADD("HH",-1,@pdate)   
    and task_number=@tasknumber

  print @previous_value
  print DATEADD("HH",-1,@pdate)

  select top 1 @next_value=DCDC_OUTPUT_AVG,
 @end_time=DATEADD(hour, D_TIME_OF_DAY-1, d.date_key)
 from I_TASK T, I_HOURLY_DATA F,I_DATE D, I_SITE_LOCATION P
 where F.D_DATE = D.ID AND F.D_PWM_LOCATION = P.ID 
 AND T.SITE_ID like  P.NAME+'%' 
 AND D.DATE_KEY between cast(TASK_CREATION_DATE as date)
 AND dateadd("d",1,CAST( coalesce(ACCEPTED_DATE, working_date) AS DATE)) and
 task_priority > '01' and TASK_PRIORITY <'06' 
  and TASK_CLOSED_DATE between @Tstart and @Tend 
  and DCDC_OUTPUT_AVG>=0.6*@previous_value  
  AND DATEADD(hour, D_TIME_OF_DAY-1, d.date_key) > @pdate  
   and   task_NUMBER=@tasknumber


  print @next_value
 print datediff("hh", @pdate, @end_time)

SET @loopdate = @pdate
if @previous_value>0 and @next_value>0

begin
INSERT INTO @dummy VALUES(@id,tasknumber,@padid,
 @pdate,@end_time,@previous_value,@next_value,@dc_dc,@DOWNTIME_IN_MINS)
 End
 End  

SELECT t1.ID,t1.task_number ,t1.Pad_id ,
t1.startdate ,t1.enddate ,    t1.previous_value , t1.next_value ,      t1.DCDC_OUTPUT_AVG,t1.DOWNTIME_IN_MINS,
datediff(hour,t1.startdate,t1.enddate)
FROM @dummy  t1

我正在尝试在ID上创建一个增量,这样每次添加带有不同时间戳的新记录时,都会使用新的ID进行更新。

但我无法做到。

这是我得到的错误:&#39;只有在使用列列表并且IDENTITY_INSERT为ON时才能指定表中标识列的显式值。&#39;

我试过了:

   SET IDENTITY_INSERT id ON
   INSERT INTO @dummy VALUES(@id, @tasknumber  ,@padid,
   @pdate,@end_time,@previous_value,
   @next_value,@dc_dc,@DOWNTIME_IN_MINS)
   SET IDENTITY_INSERT id OFF

我所接受的错误是:&#39;找不到对象&#34; id&#34;因为它不存在或您没有权限。&#39;

我执行此程序为:

 exec task_request '2016-10-01','2016-10-11'

我需要帮助根据插入的每个新记录更新带有标识列的记录。

谢谢

1 个答案:

答案 0 :(得分:1)

你的@dummy根本没有identity列,它的IDENTITY_INSERT错误怎么样?如果它确实有identity列,则无法使用IDENTITY_INSERT ON / OFF。请参阅连接项https://connect.microsoft.com/SQLServer/feedback/details/757012/set-identity-insert-on-table-variables

  

使用表变量时,SET IDENTITY INSERT命令错误   

     

Msg 102,Level 15,State 1,Line 9语法不正确&#39; @ tmp&#39;。

关闭为&#34;按设计&#34;。