运行一个存储过程时返回异常 [英] return exception when run one stored procedure
本文介绍了运行一个存储过程时返回异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
创建 过程
@ pinpatientid int
AS
开始
从中选择 Patient_ID tblPatientAdmissionDetails 其中 ID = @ pinpatientid 和状态= 1
END
GO
如果此条件是错误的,则我得到null异常.我想至少返回null值或其他任何值.如何避免此错误
在我的代码中,我使用
字符串 ds = UserDataBase.ExecuteScalar(Command).ToString();
解决方案
您可以这样编写查询:
创建 过程 @ pinpatientid int = 0 AS 开始 选择 ISNULL(患者ID, 0 )> AS 来自 tblPatientAdmissionDetails的Patient_ID > ID = @ pinpatientid END GO
谢谢,
Viprat
您可以通过在存储过程中使用变量来避免错误.请参见以下代码块
创建 过程 @ pinpatientid int AS 开始 DECLARE @ PatientId BIGINT ; SET @ PatientId = 0 选择 @ PatientId = ISNULL(Patient_ID, 0 )来自 tblPatientAdmissionDetails 其中 ID = @ pinpatientid and 状态= 1 选择 @ PatientId END
谢谢..
hi
i hve one stored procedure
create PROCEDURE spgetoutpatientidbyinpatientid
@pinpatientid int
AS
BEGIN
select Patient_ID from tblPatientAdmissionDetails where ID=@pinpatientid and Status=1
END
GO
if this condition is wrong i got null exception.i want to return at least null value or anything.how can i avoid this error
in my code i use
string ds = UserDataBase.ExecuteScalar(Command).ToString();
how can i change my sql query..?
解决方案
Hi,
you can write your query like this :
create PROCEDURE spgetoutpatientidbyinpatientid @pinpatientid int = 0 AS BEGIN select ISNULL(Patient_ID,0) AS Patient_ID from tblPatientAdmissionDetails where ID=@pinpatientid and Status=1 END GO
Thanks,
Viprat
You can avoid the error by using a variable inside your stored procedure. See the following code block
create PROCEDURE spgetoutpatientidbyinpatientid @pinpatientid int AS BEGIN DECLARE @PatientId BIGINT; SET @PatientId = 0 select @PatientId = ISNULL(Patient_ID,0) from tblPatientAdmissionDetails where ID=@pinpatientid and Status=1 SELECT @PatientId END
Thank you..
这篇关于运行一个存储过程时返回异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文