如何检查 SELECT 查询是否成功? [英] How to check whether a SELECT query is Successful?
问题描述
好吧,我是 SQL 新手,我刚刚读到,存储过程总是返回一个值来确定过程中的查询是否已成功执行,这是一个很好的做法.
所以我有一个简单的存储过程,其中包含一个 select 语句,如果它被执行,我想返回 1,否则返回 -1.
您能告诉我如何在 SQL 中编写该条件吗?
如果重要的话,我的数据库是 MS SQL Server.
谢谢你.
Well i am new to SQL and i have just read that it's a good practice that stored procedures always return a value determining whether the query inside the procedure has been executed successfully or not.
So i have a simple stored procedure with a select statement in it and i want to return 1 if it gets executed and return -1 if it does not.
Can you please tell me how to write that condition in SQL?
If it matters my database is MS SQL Server.
Thank you.
CREATE PROCEDURE myTest
AS
BEGIN
SELECT FirstName,LastName FROM Employees
RETURN 1 (If execution is successfull)
RETURN -1(If execution is not successfull)
END;
推荐答案
使用输出参数与 Try..Catch 块一起返回成功状态
Use an output parameter to return the success status along with Try..Catch blocks
CREATE PROCEDURE myTest
@Result INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
SELECT FirstName,LastName FROM Employees
SET @Result = 1;
END TRY
BEGIN CATCH
SET @Result = 0;
END CATCH
END;
这篇关于如何检查 SELECT 查询是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!