Results 1 to 3 of 3

Thread: xp_sendmail- variable error

  1. #1
    Join Date
    Mar 2006
    Posts
    2

    xp_sendmail- variable error

    i'm trying to pass a variable into the @recipients of xp_sendmail. to start with, i have a stored procedure reading the values from a table and concatenating into a semicolon delimited string as follows:
    /PROCEDURE [dbo].[p_auto_email_get_addresses]
    /@email_group_id varchar(5),
    /@email_addresses varchar(500) output
    /AS
    /declare
    / @email_string varchar(150),
    / @email_str_full varchar(500)
    /
    /set @email_str_full = ''
    /
    /declare crs_email cursor
    / for select email_address
    / from database.dbo.email_addresses
    / WHERE email_group_id like @email_group_id
    /
    /open crs_email
    /
    /fetch NEXT FROM crs_email INTO @email_string
    /
    /WHILE @@FETCH_STATUS = 0
    /BEGIN
    / SELECT @email_str_full = @email_str_full+@email_string+ '; '
    / FETCH NEXT FROM crs_email INTO @email_string
    /END
    /
    /SET @email_str_full = SUBSTRING(@email_str_full,1,DATALENGTH(@email_str_ full)-2)
    /
    /CLOSE crs_email
    /DEALLOCATE crs_email
    /
    /SELECT char(39) + @email_str_full + char(39)

    this returns something like 'jdoe@123.com; djoe@123.com'
    i then pass this into xp_sendmail...

    /declare
    / @email_to varchar(500)
    /
    /exec p_auto_email_get_addresses '%2%', @email_to output
    /
    /exec xp_sendmail @recipients = @email_to, etc, etc

    I always get "Msg 17963, Level 16, State 1, Line 0
    xp_sendmail: Procedure xp_sendmail expects parameter @recipients, which was not supplied." When i simply write in the value that @email_to returns then it works fine, so it's obviously an issue with the variable or the stored procedure. Any ideas or suggestions would be appreciated.

  2. #2
    Join Date
    Sep 2005
    Posts
    168
    /*
    Noticed, that in p_auto_email_get_addresses proc, there is no setting of the @email_addresses OUTPUT variable,
    there should be a line like : SET @email_addresses = @email_str_full instead of SELECT char(39) + @email_str_full + char(39). Thus, output variable is always set to NULL, and when you are calling xp_sendmail, @recipients is always NULL

    */


    CREATE PROCEDURE [dbo].[p_auto_email_get_addresses]
    @email_group_id varchar(5),
    @email_addresses varchar(500) output
    AS

    SELECT @email_addresses = ISNULL(@email_addresses , '') + ISNULL(email_address, '')+';'
    FROM database.dbo.email_addresses
    WHERE email_group_id LIKE @email_group_id

    --Truncate last semicolon
    SELECT @email_addresses = LEFT(@email_addresses , LEN(@email_addresses )-1)

    --EOF--

    DECLARE @email_to varchar(500)

    EXEC p_auto_email_get_addresses @email_group_id = '%2%', @email_addresses = @email_to OUTPUT

    IF LEN(@email_to) > 0
    BEGIN
    EXEC xp_sendmail @recipients = @email_to, etc, etc
    END

    --HTH--
    Last edited by mikr0s; 03-18-2006 at 08:20 AM.

  3. #3
    Join Date
    Mar 2006
    Posts
    2
    that worked. many thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •