2011-09-07 207 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