Using Contains in SQL server

0

Hello friends I want to make an insert, but I will only do it if one of the parameters that my SP is receiving, in this case the extension has the word "pdf" , then I'm trying to do this, but send me error: s

Insert into ArchivosContratos
  (IdContrato, Nombre, Extension, Ruta, RutaWeb, IdUsuarioOperacion, fecha, Activo, esContrato)
values
  (@IdContrato, @Nombre, @Extension, @Ruta, @RutaWeb, @IdUsuarioOperacion, getdate(), 1, 1)
where contains(@Extension, '"pdf"')

I also tried an if but ... it did not send me the same thing

  

if (contains (@Extension, '"pdf"')) ...

Thank you all for your help and your time.

    
asked by E.Rawrdríguez.Ophanim 03.09.2018 в 23:03
source

3 answers

3

Where can not be applied directly to a insert , if you want to do so it is by select and that select strong> what inserts , I leave you the documentation . What you require for your case is a conditional, since what you are sending is a parameter .

Example INSERT with SELECT :

--INSERT...SELECT example
INSERT dbo.EmployeeSales
    SELECT 'SELECT', e.EmployeeID, c.LastName, sp.SalesYTD 
    FROM HumanResources.Employee AS e
        INNER JOIN Sales.SalesPerson AS sp
        ON e.EmployeeID = sp.SalesPersonID 
        INNER JOIN Person.Contact AS c
        ON e.ContactID = c.ContactID
    WHERE e.EmployeeID LIKE '2%'
    ORDER BY e.EmployeeID, c.LastName;
GO

Response to your case:

IF @Extension LIKE '%pdf%'
BEGIN
    INSERT INTO ArchivosContratos (IdContrato, Nombre, Extension, Ruta, RutaWeb, IdUsuarioOperacion, fecha, Activo, esContrato)
    VALUES (@IdContrato, @Nombre, @Extension, @Ruta, @RutaWeb, @IdUsuarioOperacion, GETDATE(), 1, 1)
END
    
answered by 04.09.2018 / 18:33
source
1

If you want to use the WHERE in your SP , I think what you are looking for is to do something similar to:

DECLARE @Extension AS NVARCHAR(255) = 'ejemplo de prueba.pdf';

IF @Extension LIKE '%pdf'
BEGIN
    -- Coloca aquí tu sentencia insert:
    INSERT INTO ArchivosContratos (IdContrato, Nombre, Extension, Ruta, RutaWeb, IdUsuarioOperacion, fecha, Activo, esContrato)
    VALUES (@IdContrato, @Nombre, @Extension, @Ruta, @RutaWeb, @IdUsuarioOperacion, GETDATE(), 1, 1)
END
    
answered by 04.09.2018 в 00:05
1

In my case, what I have done is declare an action variable and use IF with BEGIN & END , look at the attached example.

USE NAMEBD
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SP_NombreSP]
    @cAccion            varchar(1)      = '',
    @nIdConcepto        int             = -1,
    @cNombre            varchar(100)    = ''

AS
SET NOCOUNT ON

DECLARE @nError     INT
DECLARE @vchDescripcion VARCHAR(100)
DECLARE @cNom VARCHAR(100)

ErrorProc:
    IF @nError <> 0 
        BEGIN
            SELECT  @vchDescripcion = description 
            FROM    master..sysmessages 
            WHERE   error = @nError

            SELECT -1                   Codigo,
                    @nError * -1        CodigoSql, 
                    @vchDescripcion     Descripcion
            RETURN -1
        END

    IF @cAccion = 'C' Or @cAccion = 'M' -- Creación o Modificación
        BEGIN

            if @cAccion = 'C'                               -- Inserta nuevo
                BEGIN
                    --INSERT INTO

                END
            else        
                BEGIN                                   -- Actualiza 
                    --UPDATE
                    SET @nError = @@Error
                END

        END

    ELSE                                        -- Solicitud de Eliminacion
        BEGIN
            --DELETE
        END
    IF @nError <> 0    
        GOTO ErrorProc
    ELSE
        SELECT  @nIdConcepto            Codigo,
                0                       CodigoSql, 
                'Ok'                    Descripcion

RETURN 0
    
answered by 05.09.2018 в 17:53