I would only do SELECT COUNT(*)
of the table if it returns 0
does not exist if it returns something else means that it exists. If only single records are handled under those conditions, then I can use the response from shop350 if what you want is a bool
as a response in your application. Or alternatively you can also do a case
if you get many records in the query and convert that answer to a bool
. Where @Resultado
is an output variable.
First alternative (Returns the number of records that meet the condition):
USE [BDCredito]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_CONS_SOLCUPO_APRO_EXISTE]
(
@Id_Solicitud nvarchar (255),
@CC_Asociado nvarchar (255),
@Resultado int output
)
AS
SELECT @Resultado = COUNT(*) FROM TBL_DOC_APRO
WHERE Id_Solicitud = @Id_Solicitud
AND CC_Asociado = @CC_Asociado;
Second alternative (If only one record is obtained as an answer it returns a bool
):
USE [BDCredito]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_CONS_SOLCUPO_APRO_EXISTE]
(
@Id_Solicitud nvarchar (255),
@CC_Asociado nvarchar (255),
@Resultado bit output
)
AS
SELECT @Resultado = CAST(COUNT(*) AS BIT) FROM TBL_DOC_APRO
WHERE Id_Solicitud = @Id_Solicitud
AND CC_Asociado = @CC_Asociado;
Third alternative (If several records are obtained a case
is made to convert the answer in bool
):
USE [BDCredito]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_CONS_SOLCUPO_APRO_EXISTE]
(
@Id_Solicitud nvarchar (255),
@CC_Asociado nvarchar (255),
@Resultado bit output
)
AS
SELECT @Resultado = CAST(CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS BIT) FROM TBL_DOC_APRO
WHERE Id_Solicitud = @Id_Solicitud
AND CC_Asociado = @CC_Asociado;
Single quotes do not need to be placed.