sql查询序列号

时间:2011-10-10 17:44:07

标签: sql sql-server tsql

我在SQL Server 2000中编写了一个存储过程。我想要一个输出表的序列号。 因此,当我运行此存储过程时,我收到此错误:

  

表中标识列的显式值   '#tmpSearchResults1'只能在使用列列表时指定   和IDENTITY_INSERT已开启。

我尝试了set IDENTITY_INSERT #tmpSearchResults1 on

Create Procedure dbo.usp_mobile_All_KeyWord(@searchkey varchar(30))                                        
AS          
CREATE TABLE #tmpSearchResults        
(                                                 
 property_id varchar(255),                                                          
 property_number varchar(255),                                                          
 auction_date_reason  varchar(255)                                        
) 

INSERT INTO #tmpSearchResults         

SELECT              

 p.property_id, p.property_number, p.auction_date_reason                                                             
   FROM                                                              
 Pr p               
 INNER JOIN                                                              
 Au a ON p.auction_id = a.auction_id                                                                
 INNER JOIN                                         
 PrAdd pa ON p.property_id = pa.property_id                                        

 INNER JOIN state AS s ON s.state_id=pa.state                
 where            
   (        
   (p.archive = 'N'               

 AND                                                     
 a.show_on_site = 'Y'                                                              
 AND                                                              
 (        
 (        
 ((p.auction_date >= CONVERT(datetime, CONVERT(varchar, GETDATE(), 103), 103) and  (p.auction_date_reason is null or p.auction_date_reason = ''))        
 or         
 (p.auction_date <= CONVERT(datetime, CONVERT(varchar, GETDATE(), 103), 103) and  ( p.auction_date_reason = 'Accepting Offers' )))        
 and        

 pa.property_address_type_id = 1 )) )        
  and         
  (state_abbreviation=@searchkey or  s.state_name like '%'+''+ @searchkey +''+'%' or city  like '%'+''+ @searchkey +''+'%' or  pa.address1 like '%'+''+ @searchkey +''+'%'        
          or  pa.address2 like '%'+''+ @searchkey +''+'%')         
 )      

)         


 CREATE TABLE #tmpSearchResults1        
(            
i1 int identity,                                          
 property_id varchar(255),                                                          
 property_number varchar(255),                                                          
 auction_date_reason  varchar(255)    
)        
 insert into #tmpSearchResults1      

 select     
property_id ,                                                          
 property_number,                                                          
 auction_date_reason                                            

 from #tmpSearchResults       

 order by        
 case when charindex(@searchkey,state) >0 then 1000 else 0 end desc,        
  case when charindex(@searchkey,statename) >0 then 1000 else 0 end desc,        
 case when charindex(@searchkey,city) >0 then 1000 else 0 end desc,         
 case when charindex(@searchkey,address2) >0 then 1000 else 0 end desc,        
 case when charindex(@searchkey,address1) >0 then 1000 else 0 end desc,        
 case when charindex(@searchkey,short_description) >0 then 1000 else 0 end desc      

 select * from #tmpSearchResults1     

Plz帮帮我

2 个答案:

答案 0 :(得分:5)

错误代码非常清晰。

相关部分为...when a column list is used...

您需要在INSERT声明中指定列列表。

INSERT INTO #tmpSearchResults
(i1, 
 property_id,                                                          
 property_number,                                                          
 auction_date_reason)         

SELECT              

 p.property_id, p.property_number, p.auction_date_reason                                                             
   FROM... 

答案 1 :(得分:0)

首先,在第二个陈述的SELECT部分中有一个逗号太多:

 insert into #tmpSearchResults1      

 select     
     property_id ,                                                          
     property_number,                                                          
     auction_date_reason  , <-- THIS ONE!!                                      

 from #tmpSearchResults 

SELECT语句的最后一列必须没有逗号 所以这是正确的:

 insert into #tmpSearchResults1      

 select     
     property_id ,                                                          
     property_number,                                                          
     auction_date_reason                                 

 from #tmpSearchResults

其次,您是否阅读了错误消息的这一部分?

  

显式值[...]只能在使用列列表时指定

“列列表”部分表示您必须在INSERT部分中指定列:

 insert into #tmpSearchResults1  
     (property_id, property_number, auction_date_reason)

 select     
     property_id ,                                                          
     property_number,                                                          
     auction_date_reason                                 

 from #tmpSearchResults

SELECT语句中的列数与插入它们的表中的列数相同时(如果数据类型匹配),您可以不指定列。
如果不满足其中一个条件,则需要指定列,否则SQL Server不知道要将哪个值插入哪个列。