2011-10-10 89 views
0

我在SQL Server 2000中編寫了一個存儲過程。我想要一個輸出表的序列號。 所以,當我運行這個存儲過程中我得到這個錯誤:sql查詢序列號

An explicit value for the identity column in table '#tmpSearchResults1' can only be specified when a column list is used and IDENTITY_INSERT is ON.

我與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   
    ([email protected] 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你幫我

回答

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... 
+0

謝謝,它的工作 – Sree

+1

@Sree - 很樂意幫忙。密切關注錯誤代碼,SQL Server中的很多時間都非常具體和有用。 – JNK

+0

@JNK:提供一個實際**讀**他們! :..... –

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 

其次,你看了錯誤信息的一部分?

An explicit value [...] can only be specified when a column list is used

「列清單」的一部分意味着你必須在INSERT部分指定列:

insert into #tmpSearchResults1 
    (property_id, property_number, auction_date_reason) 

select  
    property_id ,               
    property_number,               
    auction_date_reason         

from #tmpSearchResults 

你可以逃脫不指定列時,列在SELECT數語句與它們應該插入的表中的相同(以及數據類型是否匹配)。
如果這些條件之一未得到滿足,則需要指定列,否則SQL Server不知道要將哪個值插入到哪個列中。