Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
DECLARE @check INT = CASE
	WHEN @@ROWCOUNT = 0
    THEN 
	select 0 as [result], 'Success' as [message]
	ELSE 
	select 1 as [result], 'Failed' as [message]
	END


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 |
Posted
Updated 21-Feb-19 2:00am
Comments
Santosh kumar Pithani 19-Feb-19 0:10am    
Variable returns only single values not table records.Find my solution
select [result],CASE WHEN [result]=@@ROWCOUNT THEN ' Success ' ELSE 'Failed' END AS [message]
from (values(0),(1)) AS tab([result])
CHill60 20-Feb-19 5:56am    
What is the error?

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:
XML
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
 
Share this answer
 
Comments
CHill60 21-Feb-19 11:04am    
5'd
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
SQL
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
 
Share this answer
 
Comments
Maciej Los 20-Feb-19 7:26am    
5ed!
Jörgen Andersson 21-Feb-19 7:55am    
There are four ways to return data in your link
CHill60 21-Feb-19 11:03am    
?? Output parameter, result set and return code. I only see 3.
Jörgen Andersson 21-Feb-19 11:05am    
Need coffee
CHill60 21-Feb-19 11:11am    
LOL! I cleaned my glasses twice just to be sure :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900