2011-09-07 177 views
0

我有2列的ID(身份),errormessage的(10個可能的錯誤消息)聲明的變量

現在我已經從那裏我能得到可能的錯誤消息如果有一定的業務邏輯的表。 我有10個SP。我想要column2 =錯誤消息中的所有錯誤消息。

根據我使用,它僅更新最新更新錯誤(不是全部) 可以說爲企業logic1(通過執行SP1)的ID = 1的邏輯,我得到ERRORMSG = <Data Invalid> 爲ID = 1 ,執行SP2,我越來越<Data Corrupted>

現在,我想要在一列中獲得這兩個消息。 我知道這很難解釋(因爲我有10個SP),請幫助我採用我應該使用的方法。

PS:我已經爲SP中的每個錯誤消息聲明瞭一個變量,並向其中添加了下一條消息,但即使該錯誤沒有發生,它也顯示錯誤。例如:

declare @errormessage1 varchar(20) 
set @errormessage1 = <Data Invalid> 

declare @errormessage2 varchar(20) 
set @errormessage2 = <Data corrupted> 

update my_table 
set errormessage= @errormessage1 + @errormessage2 
from my_table 

即使errormessage1沒有無效的數據,但它仍然顯示我在COL2 = + 現在,這裏的問題是它應該只顯示實際的錯誤不是(所有申報錯誤)

感謝

+0

這是SQL Server的? – Phil

+0

是的,它的SQL Server。 –

+0

您有一個包含10個可能的錯誤消息及其ID的列表。您可以爲給定的呼叫生成全部10條消息。希望將所有的錯誤消息都放在單個列(和行)中。這是對你所問的問題的準確解讀嗎? – billinkc

回答

0

這裏有些亂撞,而我們等待更多細節

-- Create a local table variables to hold processing results 
DECLARE @PROCESSING_RESULTS 
(
    -- size and type these accurately 
    id int NOT NULL 
, error_message varchar(8000) 
) 
DECLARE @ID int 
, @error_message varchar(8000) 

-- Load up the table 
-- Not sure what the signature of the proc looks like so guessing here 
-- Either use an insert into with the execute 
-- This assumes an invocation of the proc returns a result set 
INSERT INTO 
    @PROCESSING_RESULTS 
EXECUTE dbo.SP1 @id = 1 


-- This approach assumes output parameters 
EXECUTE dbo.SP1 @id = 1, @error_message output 
INSERT INTO 
    @PROCESSING_RESULTS 
([id], error_message) 
SELECT 1, @error_message 

-- Repeat the above approach for all 10 procs 

-- At this point, all the errors are in our processing_results table 
-- so we should do something with them. 
-- Assuming you want a CSV list of all the error messages, this will 
-- mash all the results 
SELECT STUFF(
(SELECT ',' + PR.error_message 
FROM @PROCESSING_RESULTS PR 
ORDER BY PR.error_message 
FOR XML PATH('')),1,1,'') AS CSV