简体   繁体   中英

HTMl Anchor tag in stored procedure

i want to use a anchor tag in stored procedure that would be sent as a body of the mail and it's not working

declare @loginlink VARCHAR(MAX)=N'';

SELECT @loginlink= N' '+ CONVERT(varchar(36),@loginlink)+'ClickMe ' AS CaseLink

If you want to know how to set a T-SQL variable to an HTML anchor, then you could use the code below. Just replace the href value with the value in your situation.

Note the use of two single quotes around href value. This is necessary since we want single quote to be around the href value and we are using single quotes for enclosing string in SQL Server.

declare @loginlink VARCHAR(MAX)=N'';
set @loginlink = '<a href=''http://www.yahoo.com''>Click Me</a>';
SELECT @loginlink as CaseLink;

If you want to use a T-SQL variable for the href part of anchor link, then you can use code like below.

declare @loginlink VARCHAR(MAX)=N'';
DECLARE @href  varchar(500);
SET @href = 'http://www.yahoo.com';

set @loginlink = '<a href=''' + @href + '''>Click Me</a>';
SELECT @loginlink as CaseLink;

If you are sending this link using database mail then you need to create a html page with all relevant html tags and send that as body, plus you need to make sure that the mail format is Html .

declare @body nvarchar(max)
set @body = '<html><head><title>Results</title></head><body>' + @loginlink + '</body></html>'

EXEC sp_send_dbmail 
  @profile_name='emailProfile',
  @copy_recipients ='abc@xyz.com',
  @recipients='opq@oxyz.com',
  @subject='New Results',
  @body=@body , --this needs to be an html with all necessary html tags
  @body_format = 'HTML' ;

OMG it was a very silly mistake i did

EXECUTE AS USER = 'dbo'  
    EXEC msdb.dbo.sp_send_dbmail
     @recipients='abc.com   ',
      @copy_recipients='abc.com',
      --@blind_copy_recipients=
      @from_address = 'abc.com',
      --@reply_to=
      @importance='High', -- High | Normal | Low
      @profile_name = 'test',
      @subject = @EmailSubject, 
      @body = @EmailBody,
     @body_format='HTML'; --TEXT | HTML 

@body_format='HTML'; --TEXT | HTML ::: my body format of mail was set to text that is why it was not treating anchor tag as it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM