Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[spc_getemail]
As
Begin
-- Declare Variable To Hold The Email Address Retrived
DECLARE @Email varchar(255)
DECLARE @Name  varchar(255)
DECLARE @ID    int
--Declare Cursor  to loop through the table data
DECLARE EmailCursor Cursor For
Select ID,email,name from demoemail
-- Open cursor for fetching data in email variable
open EmailCursor 
Fetch Next From EmailCursor into @ID,@Email,@Name
while(@@FETCH_STATUS = 0)
Begin
DECLARE @out_desc varchar(1000),
@out_mesg varchar(10)

EXEC  [dbo].[sp_send_mail] 
'a@a.com',
'a@g.com',
' Newsletter Submission',
'<a href=''http://someurl.com?id=' + CAST(@ID As varchar(20)) + '''>Please click here to fill your missing details',
'htmlbody', 
@output_mesg = @out_mesg output,
@output_desc = @out_desc output
print 'Email id  is ' + @Email
print 'Email id  is ' + @ID
End;
Close EmailCursor
Deallocate EmailCursor
End;


What I have tried:

I have tried all escape sequences in sql to resolve.
Posted
Updated 9-Jun-17 20:01pm
v5
Comments
PeejayAdams 1-Jun-17 8:13am    
It's very hard to say without seeing your definition for IDCursor and your declaration of @ID.
Abhi1 Kanobi 1-Jun-17 8:28am    
the declaration is below.

Begin
-- Declare Variable To Hold The Email Address Retrived
DECLARE @Email varchar(255)
DECLARE @Name varchar(255)
DECLARE @ID int


--Declare Cursor to loop through the table data
DECLARE EmailCursor Cursor For
Select email From demoemail

DECLARE NameCursor Cursor For
Select name from demoemail


DECLARE IDCursor Cursor For
Select ID from demoemail


-- Open cursor for fetching data in email variable
open EmailCursor
Fetch Next From EmailCursor into @Email

open NameCursor
Fetch Next from NameCUrsor into @Name

open IDCursor
Fetch Next from IDCUrsor into @ID
Richard Deeming 1-Jun-17 8:30am    
The obvious problem is that you're missing the + between the different parts of the string:
'<a href=''http://newsletter.qendidate.com?id=' + @ID + '''>Please click here to fill your missing details </a>'

Also, you don't need three separate cursors; you just need one cursor which selects three fields.
Abhi1 Kanobi 1-Jun-17 8:56am    
It's still giving error as "incorrect sysntax near +"
Abhi1 Kanobi 1-Jun-17 8:57am    
USE [orion]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[spc_getemail]
As
Begin
-- Declare Variable To Hold The Email Address Retrived
DECLARE @Email varchar(255)
DECLARE @Name varchar(255)
DECLARE @ID int
--Declare Cursor to loop through the table data
DECLARE EmailCursor Cursor For
Select ID,email,name from demoemail
-- Open cursor for fetching data in email variable
open EmailCursor
Fetch Next From EmailCursor into @ID,@Email,@Name
while(@@FETCH_STATUS = 0)
Begin
DECLARE @out_desc varchar(1000),
@out_mesg varchar(10)

EXEC [dbo].[sp_send_mail]
'2@a.com',
'a@a.com',
'Newsletter Submission',
'<a href=''someurl.com?id='+ID'''>Please click here to fill your missing details ',
'htmlbody',
@output_mesg = @out_mesg output,
@output_desc = @out_desc output
print 'Email id is ' + @Email
print 'Email id is ' + @ID
End;
Close EmailCursor
Deallocate EmailCursor
End;

I updated the URL , and now the id 's are braodcasting .

Thanks everyone for the input's

DECLARE @body varchar(max) = 
'<a href=''http://someurl.com/?id=' + CAST(@ID As varchar(20))+'''>Please click here to fill your missing details</a>';

EXEC  [dbo].[sp_send_mail] 
'a@a.com',
@Email,
' Submission',
 @body,
'htmlbody', 
 
Share this answer
 
Though this solution may work, architecturally it is not a suitable solution, as we are invoking too much business functionalities using just a single SP. To my opinion, there should be separation of concerns in the business layer, and SRP (single responsibility principle) should be followed. If this SP is written for the sake of achieving performance, then performance factor should be improved by some other means, but not at the cost of code maintainability.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900