Mostrando entradas con la etiqueta SQL. Mostrar todas las entradas
Mostrando entradas con la etiqueta SQL. Mostrar todas las entradas

martes, 24 de abril de 2012

Procedimiento para enviar correos electrónicos desde la misma Base de Datos


CREATE     PROCEDURE [dbo].[sp_sendEmail]
(
    @cfgSMTPServer varchar(100),
    @cfgMail varchar(100),
    @cfgPwd varchar(100),
    @cfgPort varchar(5),
    @De varchar(100) ,
    @Para varchar(8000) ,
    @Titulo varchar(100)=" ",
    @Mensaje varchar(8000) =" ",
    @Attachment VARCHAR(1000) = " ",
    @Query VARCHAR(8000)= " "
)
AS
    set @De = 'Aqui va el titulo por default'
    Declare @Correo int
    Declare @proc int
    Declare @Dir varchar(500)
    Declare @DirUse varchar(500)
   
    SET @Dir = 'Configuration.fields("'
    SET @Dir = @Dir + 'http://schemas.microsoft.com/'
    SET @Dir = @Dir + 'cdo/configuration/'
   
    EXEC @proc = sp_OACreate 'CDO.Message', @Correo OUT
    SET @DirUse = @Dir + 'sendusing").Value'
    EXEC @proc = sp_OASetProperty @Correo, @DirUse,'2'
   

    if @proc <> 0
    begin
        print 'error'
    end
    --Servidor SMTP
   
    SET @DirUse = @Dir + 'smtpserver'+'").Value'
    EXEC @proc = sp_OASetProperty @Correo, @DirUse, @cfgSMTPServer
   
    if @proc <> 0
    begin
        RAISERROR('Error enviando correo 1',15,1)
        RETURN
    end
    -- Puerto normalmente el 25
    SET @DirUse = @Dir + 'smtpserverport").Value'
    EXEC @proc = sp_OASetProperty @Correo, @DirUse, @cfgPort
   
    if @proc <> 0
    begin
        RAISERROR('Error enviando correo 2',15,1)
        RETURN
    end
    -- Autenticacion, para usar cuenta y contraeña
   
    SET @DirUse = @Dir + 'smtpauthenticate").Value'
    EXEC @proc = sp_OASetProperty @Correo, @DirUse, '1'


    -- Cuenta
    SET @DirUse = @Dir + 'sendusername").Value'
    EXEC @proc = sp_OASetProperty @Correo, @DirUse, @cfgMail
   
    -- Contraseña
   
    SET @DirUse = @Dir + 'sendpassword").Value'
    EXEC @proc = sp_OASetProperty @Correo, @DirUse, @cfgPwd
   
    if @proc <> 0
    begin
        RAISERROR('Error enviando correo 3',15,1)
        RETURN
    end
    -- Autenticac
    -- Adjuntar Archivo
    CREATE TABLE #FileExists (FileExists int, FileIsDir int, ParentDirExists int)
   
    IF @Attachment <> ' '
    BEGIN
        declare @lcContAttach int
        set @lcContAttach = 0
        declare @StrEnd INT
        DECLARE @lcAttach VARCHAR(100)
        WHILE isnull(len(@Attachment),0) > 0
        BEGIN
            SELECT @StrEnd = CASE charindex(';', @Attachment)
                    WHEN 0 THEN len(@Attachment)
                    ELSE charindex(';', @Attachment) - 1
                    END
            SELECT @lcAttach = substring(@Attachment, 1, @StrEnd)
            SELECT @Attachment = substring(@Attachment, @StrEnd+2, len(@Attachment))
       

           
            DELETE #FileExists
            INSERT #FileExists
            EXEC master..xp_fileexist @lcAttach
           
            IF NOT EXISTS (SELECT * FROM #FileExists WHERE FileExists = 1)
            BEGIN
                RAISERROR ('El archivo no existe.', 16, 1,@lcAttach)
                RETURN 1
            END
           
            EXEC @proc = sp_OAMethod @Correo, 'AddAttachment', NULL, @lcAttach
            IF @proc <> 0
            BEGIN
                SET @lcContAttach = @lcContAttach + 1
            ENd

        END
        PRINT 'Archivo(s) adjuntado(s): ' +LTRIM(RTRIM(str(@lcContAttach)))
    END
    --Fin adjuntar Archivo
   
    --Consulta HTML
    if @Query <> ' '
    begin
        declare @Columns varchar(8000)
           declare @ColHeader varchar(8000)
           Declare @SqlCmd varchar(8000)
           Declare @HTMLBody varchar(8000)
        -- drop temporary tables used.
        IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects WHERE name = '##TEMPhtml1')
        DROP TABLE ##TEMPhtml1
        IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects WHERE name = '##TEMPhtml2')
        DROP TABLE ##TEMPhtml2
       
        -- prepare query
        set @SqlCmd = 'select * into ##tempHTML1 from (' + @Query + ') as t1'
        execute (@SqlCmd)
       
        --Prepare columns details
        SELECT @columns =
            COALESCE(@columns + ' + ''</td><td>'' + ', '') +
            'RTrim(convert(varchar(100),isnull(' + column_name +','' '')))'
            FROM tempdb.information_schema.columns
            where table_name='##tempHTML1'
           
        --Prepare column Header
        set @colHeader = '<tr bgcolor=#FAAC58 align=Left>'
        SELECT @colHeader = @colHeader + '<td><b>' + column_name + '</b></td>'
        FROM tempdb.information_schema.columns where table_name='##tempHTML1'
        set @colHeader=@colHeader + '</tr>'
       
        --prepare final output
        set @SqlCmd =
            'Select ''<tr><td>'' + ' +
            @columns +
            ' ''</td></tr> '' into ##tempHTML2 from ##tempHTML1 '
        execute( @SqlCmd)
           
        --set @finalhtmlout=
        set @HtmlBody =
            ' <html> <body><style type="text/css" media="all"> ' +
            'table { margin-bottom: 2em; border-collapse: collapse } ' +
            'td,th {border= 1 solid #999; padding: 0.2em 0.2em; font-size: 12;} ' +
            '</style> <table width="100%"> ' +
            @colHeader
       
        select @HtmlBody = @HtmlBody + [</td></tr>]
        from ##tempHTML2
       
        set @HtmlBody = @HtmlBody + ' </table></body></htmL>'
   
        --EXEC @proc = sp_OASetProperty @Correo, 'HTMLBody',  @HtmlBody
        set @Mensaje = @HtmlBody
        -- drop temporary tables used.
        IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects WHERE name = '##TEMPhtml1')
        DROP TABLE ##TEMPhtml1
        IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects WHERE name = '##TEMPhtml2')
        DROP TABLE ##TEMPhtml2   
    end
    --Fin Consulta
   
    -- Con esto grabas la configuracion
    EXEC @proc = sp_OAMethod @Correo, 'Configuration.Fields.Update', null
   
   
    -- Metes los parametros
    EXEC @proc = sp_OASetProperty @Correo, 'To', @Para
    EXEC @proc = sp_OASetProperty @Correo, 'From', @De
    EXEC @proc = sp_OASetProperty @Correo, 'Subject', @Titulo
   
    if @proc <> 0
    begin
        RAISERROR('Error enviando correo 4',15,1)
        RETURN
    end
    -- Autenticac
    -- Puedes usar 'HTMLBody' o 'Textbody'
   
    EXEC @proc = sp_OASetProperty @Correo, 'HTMLBody', @Mensaje
   
    if (@proc <> 0 )
    begin
        RAISERROR('Error enviando correo HTML',15,1)
        RETURN
    end
    -- Autenticac
    -- Aqui mandas el mensaje
    EXEC @proc = sp_OAMethod @Correo, 'send',NULL
   
    -- Por si hay error
    Declare @iMsg int
    Declare @hr int
    Declare @source varchar(255)
    Declare @description varchar(500)
    Declare @output varchar(1000)
    IF ( @proc <> 0 )
    BEGIN
        IF @proc <>0
        BEGIN
            EXEC @proc = sp_OAGetErrorInfo NULL, @source OUT, @description OUT
            IF @proc = 0
            BEGIN
                SELECT @output = ' Source: ' + @source
                PRINT @output
                SELECT @output = ' Description: ' + @description
                PRINT @output
            END
            ELSE
            BEGIN
                PRINT ' sp_OAGetErrorInfo failed.'
                RETURN
            END
        END
    -- Limpia el objeto
        EXEC @proc = sp_OADestroy @Correo
        RAISERROR('Error enviando correo last',15,1)
        RETURN
    END
   
    PRINT 'Correo enviado satisfactoriamente'
    -- Limpia el objeto
    EXEC @proc = sp_OADestroy @Correo

/*
Es necesario crear este procedimiento en la base de datos
master . Ejemplo de implementación:

exec master.dbo.sp_sendEmail
            @cfgSMTPServer = 'correoweb.misupercito.com.mx',
            @cfgMail = 'correo@misupercito.com.mx', --Este correo es el emisor
            @cfgPwd = 'tupass',
            @cfgPort = '25', --Este es el puerto normalmente usado
            @De = 'saig.dap@campeche.gob.mx', --Es lo que aparecera como emisor, puede ser dif al @cfgMail
            @Para=@lcMails,-- Correo Receptor (Clientes, empleados etc..)
            @Titulo = 'Felicidades por tu cumpleaños', '' --Titulo del mensje
            @mensaje = @lcMSG,-- 'Mensaje'
            @Attachment = ' ',--Sin hay archivos adjuntos
            @Query ='select * from midb.dbo.mitabla'--Si deseas enviar una consulta, ten en cuenta que se ejecuta el store en Master
 Puedes costumizarlo a tu gusto...
*/

jueves, 19 de abril de 2012

Consultar varios LIKE´s sin SQL Dinámico

--Creamos una tabla temporal para la prueba
Declare @vTable TABLE(id INT, NAME VARCHAR(100))
INSERT INTO @vTable
SELECT 1,'Jose' UNION ALL
SELECT 2,'Gabriel' UNION ALL
SELECT 3,'Gabino' UNION ALL
SELECT 4,'Francisco'

DECLARE @vParam VARCHAR(100)
-- Aqui introducimos los valores separados por coma.
SET @vParam = 'Gab,Fran'

-- Utilizamos CROSS APPLY para completar esta tarea.
SELECT * FROM @vTable
CROSS APPLY (SELECT [item] FROM dbo.fnSplit(@vParam,',')) b
WHERE NAME LIKE '%' + b.[item] + '%'

/*
* La funcion dbo.fnSplit es muy común encontrarla en la
* red, pero se las dejo aqui para que la utilicen
* */

create FUNCTION [dbo].[fnSplit](
    @sInputList VARCHAR(8000) -- Lista de los valores separados
  , @sDelimiter VARCHAR(8000) = ',' -- Delimitador
) RETURNS @List TABLE (item VARCHAR(8000))

BEGIN
    DECLARE @sItem VARCHAR(8000)
    WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
     BEGIN
         SELECT
          @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
          @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))

         IF LEN(@sItem) > 0
          INSERT INTO @List SELECT @sItem
     END

    IF LEN(@sInputList) > 0
         INSERT INTO @List SELECT @sInputList -- Ultimo item
    RETURN
END