如何在表结果中添加0并在特殊列中打印消息(SQL存储过程) [英] How do I put 0 in table result and print message in a special column(SQL stored procedure)

查看:74
本文介绍了如何在表结果中添加0并在特殊列中打印消息(SQL存储过程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DECLARE @check INT = CASE
	WHEN @@ROWCOUNT = 0
    THEN 
	select 0 as [result], 'Success' as [message]
	ELSE 
	select 1 as [result], 'Failed' as [message]
	END





< b>我尝试过的事情:



我已经尝试了,但是出现了合乎逻辑的错误



输出将是这样的

|结果| |留言|

| 0 | |成功|

| 1 | |失败|



What I have tried:

i've tried if else but getting a logical error

Output will be like this
| result | | Message |
| 0 | | Success |
| 1 | | Failed |

推荐答案

有三种方法可以从存储过程返回数据 - 请参阅从存储过程返回数据 - SQL Server Microsoft Docs [ ^ ]

但问题是你试图为变量分配多个东西,你需要把它分开。例如。像这样
There are three ways of returning data from a stored procedure - see Return Data from a Stored Procedure - SQL Server | Microsoft Docs[^]
But your problem is that you are trying to assign more than one thing to a variable, you need to separate that out. E.g. like this
IF OBJECT_ID('CHtest', 'P') IS NOT NULL  
   DROP PROCEDURE CHtest;  
GO  

CREATE PROCEDURE CHtest(@p_Input char(1), @msg varchar(255) OUTPUT)
AS
BEGIN

	-- Whatever it is you are doing to get a value in @@ROWCOUNT goes here

	IF @@ROWCOUNT = 0 
	BEGIN
		SET @msg = 'Success'
		RETURN 0
	END
	ELSE
	BEGIN
		SET @msg = 'Failed'
		RETURN 1
	END 
END

有一种方法可以按照你在问题中的结构方式返回数据。

所以如果我稍微调整一下CHills程序:

There is a way to return the data the way you have structured in the question.
So if I adjust CHills procedure just a little bit:
ALTER PROCEDURE [dbo].[Test]
	-- Add the parameters for the stored procedure here
AS
BEGIN
    -- Whatever it is you are doing to get a value in @@ROWCOUNT goes here
    IF @@ROWCOUNT = 0
    BEGIN
    SELECT 0 AS [result], 'Success' AS [message]
    RETURN 0
    END
	ELSE 
    BEGIN
	SELECT 1 AS [result], 'Failed' AS [message]
    RETURN 1
	END
END


这篇关于如何在表结果中添加0并在特殊列中打印消息(SQL存储过程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆