Validate if a record exists in a SQL SERVER table

0

Cordial Greeting,

What I want to do is validate if a record exists in a table with a stored procedure from SQL SERVER.

USE [BDCredito]
GO
/****** Object:  StoredProcedure [dbo].[SP_CONS_SOLCUPO_APRO_EXISTE]    Script Date: 11/15/2018 08:48:27 ******/
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)
)
AS 
SELECT * FROM TBL_DOC_APRO WHERE EXISTS (SELECT * FROM TBL_DOC_APRO WHERE Id_Solicitud = ''+@Id_Solicitud+'' and CC_Asociado = ''+@CC_Asociado+'');

I do not know how to make that work, I hope you help me.

Thanks

    
asked by Andres Rodriguez 15.11.2018 в 15:14
source

2 answers

0

To start you do not have to put quotes in the variables, they already arrive as text, the concatenation is unnecessary.

You can validate if it exists and return a boolean, the cast as bit is to force the bit return and not an int. (causes conflict in some MNOs if not specified)

You could also consider putting this better as a function since it does not perform any modification procedure to the database.

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)
)
AS 
IF EXISTS (SELECT * FROM TBL_DOC_APRO WHERE Id_Solicitud = @Id_Solicitud and CC_Asociado = @CC_Asociado)
    RETURN CAST(1 AS bit);
ELSE 
    RETURN CAST(0 AS bit);
    
answered by 15.11.2018 в 15:39
0

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.

    
answered by 15.11.2018 в 18:21