【发布时间】:2018-03-15 12:47:47
【问题描述】:
存储过程
ALTER procedure [work].[update_status]
@item_id int,
@next_status varchar(60)
as begin
declare
@status varchar(60),
@last_status_date datetime2;
set @status = (select [status] from work.items where id = @item_id); --set status to current status
--Check validity of status change
if not exists (select * from work.status_precedence where status = @status and next_status = @next_status)
begin
declare @err varchar(255) = 'Changes not applied: ' + @next_status + ' is not a valid state following ' + @status + '. See work.status_precedence for valid status advancements.';
raiserror(@err,0,-1);
return -1;
end
--Get date time of last status update
set @last_status_date = isnull(
( select max(added)
from work.status_changes
where item_id = @item_id
),
( select requested
from work.items
where id = @item_id
));
--If request has not been reviewed.
if @status = 'Pending Review' and not exists (select * from work.request_reviews where item_id = @item_id and approval = 1 and reviewed > @last_status_date)
begin
raiserror(N'Changes not applied: Request has not been reviewed since set to pending status.',0,-1);
return -1;
end
--If developer has not been assigned.
if @status = 'Request Reviewed' and not exists (select * from work.assignments where item_id = @item_id and assigned > @last_status_date)
begin
raiserror(N'Changes not applied: Developer has not been assigned.',0,-1);
return -1;
end
--If level of effort is not assigned.
if @status = 'Assigned' and not exists (select * from work.developer_review where item_id = @item_id and added > @last_status_date)
begin
raiserror(N'Changes not applied: Level of effort has not been assigned.',0,-1);
return -1;
end
--If work hasn't been peer reviewed.
if @status = 'Peer Review' and @next_status = 'Business Review' and not exists (select * from work.peer_reviews where item_id = @item_id and approval = 1 and reviewed > @last_status_date)
begin
raiserror(N'Changes not applied: No approved peer review after work item last set to Peer Review status.',0,-1);
return -1;
end
--If work hasn't completed business review.
if @status = 'Business Review' and @next_status = 'Complete' and not exists (select * from work.business_reviews where item_id = @item_id and approval = 1 and reviewed > @last_status_date)
begin
raiserror(N'Changes not applied: No approved business review after work item last set to Business Review status.',0,-1);
return -1;
end
--Log status.
insert into work.status_changes (
item_id,
previous_status,
new_status
) values (
@item_id,
@status,
@next_status
);
--Set current status.
update work.items set status = @next_status
where id = @item_id;
return 0;
end;
VB.NET 代码背后
try
command.connection= connection
command.commandtype = storedprocedure
command.commandtext = "work.update_status"
command.parameters.addwithvalue("item_id",value)
connection.open()
command.executenonquery()
catch ex as exception
msgbox(ex.message)
end try
我想从后面的代码中获取引发错误消息,但它不会抛出异常并且永远不会命中 catch 块。我在网上找到了更改 raiseerror 的错误严重性的建议,但我不拥有存储过程,所以我无法更改它。谢谢!
【问题讨论】:
-
我添加了整个存储过程。我无法编辑它,因为它已被其他人使用。只是想知道是否有一种方法可以从 VB.NET 获得引发的错误
标签: sql-server vb.net stored-procedures