XP_Sendmail @Query

  • Following is part of TSQL to email the quey as attachemnet. There is a variable parameter in query @TblID. If I replace @TblID with a value (integer) all thing works and send the quey as email attachment. But query does not work with variable parameter.

    Declare @email As varchar(70)

    Declare @TblID as int

    'following values populate from table after update trigger

    SELECT @email=SendEmail FROM inserted

    SELECT @TblID=TblID FROM inserted

    EXEC master..xp_sendmail @recipients = @email,

    @query = 'SELECT Field1, Field2 FROM MyDB..dbo.MyTbl WHERE TblID =@tblID;',

    @subject = 'Subject ',

    @message = 'Message:',

    @attach_results = 'TRUE', @width = 250,

    @no_output ='FALSE'

    Thanks in advance

  • I work out my error:

    'SELECT ID, filed2 FROM Mydb.dbo.Mytbl WHERE ID =' + @TblID

  • Heh. Glad we could help. 🙂

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply