Problems with cursor

Getting the following errors when I run it:

Msg 512, Level 16, State 1, Line 34
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

(1 row(s) affected)

(1 row(s) affected)
Msg 512, Level 16, State 1, Line 34
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

(1 row(s) affected)

(1 row(s) affected)
Msg 512, Level 16, State 1, Line 34
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)
Msg 512, Level 16, State 1, Line 34
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

--declare all variables used
declare @warningdate date
declare @daysLeft varchar (50)
declare @user varchar(50) 
 
 

--set the warning date variable. This says if its less than 30 days away
 SET @warningdate =   getdate() +30
--SELECT @warningdate




DECLARE c1 CURSOR READ_ONLY
FOR
(SELECT 
      [usedBy34] 
      
  FROM [AssetManagement].[dbo].[secureToken]
  
  where  [DateDeath]  < @warningdate )


OPEN c1

FETCH NEXT FROM c1
INTO @user

WHILE @@FETCH_STATUS = 0
BEGIN
	
  
 select dbo.Users_Master.NameReg,(select dbo.secureToken.[DateDeath] from dbo.secureToken
 where usedBy34 = @user) as expiresDate from dbo.Users_Master 
 where
dbo.Users_Master.user34 = @user
 




	FETCH NEXT FROM c1
	INTO @user

	

END


 ---strictly the email portion of the script.    
--        IF @user is not NULL   
--Begin  
--EXEC msdb.dbo.sp_send_dbmail @recipients = 'darryl.emily@scresearch.net', --- 'dup_ran@kswiring.com' 
--@query = 'select dbo.Users_Master.NameReg,(select dbo.secureToken.[DateDeath] from dbo.secureToken where usedBy34 = @user) as expiresDate from dbo.Users_Master where dbo.Users_Master.user34 = @user', 
--@subject = 'There are secure Tokens that are about to expire', 
--@body = 'The following tokens will expire:', 
--@query_result_separator='    ', 
--@attach_query_result_as_file = 'TRUE', 
--@query_result_width=400 
--End  
------ End of the emai section. 


CLOSE c1
DEALLOCATE c1

Open in new window

dboyd02Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
Looks like this part is the problem:



select dbo.Users_Master.NameReg,(select dbo.secureToken.[DateDeath] from dbo.secureToken
 where usedBy34 = @user) as expiresDate from dbo.Users_Master
 where
dbo.Users_Master.user34 = @user



You must have at least one value for @user where the subquery returns >1 DateDeath value.

Do you want the earliest DateDeath?  In that case, use:

select dbo.Users_Master.NameReg,(select min(dbo.secureToken.[DateDeath]) from dbo.secureToken
 where usedBy34 = @user) as expiresDate from dbo.Users_Master
 where
dbo.Users_Master.user34 = @user


The latest?  Use:

select dbo.Users_Master.NameReg,(select max(dbo.secureToken.[DateDeath]) from dbo.secureToken
 where usedBy34 = @user) as expiresDate from dbo.Users_Master
 where
dbo.Users_Master.user34 = @user
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this query:
(select dbo.secureToken.[DateDeath] from dbo.secureToken where usedBy34 = @user)


is likely to return more than  row....
ensure that you change the query in a way that it returns 1 single row.
for example:

(select top 1 dbo.secureToken.[DateDeath] from dbo.secureToken
 where usedBy34 = @user)
dboyd02Author Commented:
Well there is only one DateDeath (which is a single token with a SerialNBR) assigned for every usedBy34. The usedBy34 is the unique user ID for the users. There are 225 employees. The procedure is trying to find any dateDeaths that are going to expire in less than 30 days from the current date. This procedure will run once a day as a warning system that there are users out there that there VPN token will expire in less than 30 days.
I hope that makes sense. So to answer your question "matthewSpatrick" I dont really need the earliest or the latest, I need every record that has a date about to expire. Once I find that record, I need to get the usedby34, SerialNBR, NameReg from those records and email all those records.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

dboyd02Author Commented:
angellll,
Will this (top 1) still allow me to pull all records in the table that have a date about to expire?
dboyd02Author Commented:
Okay, Angelllll, I just changed the code and added top 1 where you said. I looks like it is giving the records that I need. I will run a few more tests. Thanks so far.
dboyd02Author Commented:
Okay, now I am trying to email those results that I just queried. Adding the TOP 1 gives me the records I need, now I un commented the code for the email part and here is the error I am getting:

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)
Msg 22050, Level 16, State 1, Line 0
Error formatting query, probably invalid parameters
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 504
Query execution failed: ¿Msg 137, Level 15, State 2, Server CANNIS13, Line 1
Must declare the scalar variable "@user".
dboyd02Author Commented:
Here is the code that is giving the above error when I try to email the results of the cursor select.
SET NOCOUNT ON;
	
--declare all variables used
declare @warningdate date
declare @daysLeft varchar (50)
declare @user varchar(50) 
 
 

--set the warning date variable. This says if its less than 30 days away
 SET @warningdate =   getdate() +30
--SELECT @warningdate




DECLARE c1 CURSOR READ_ONLY
FOR
(SELECT 
      [usedBy34] 
      
  FROM [AssetManagement].[dbo].[secureToken]
  
  where  [DateDeath]  < @warningdate )


OPEN c1

FETCH NEXT FROM c1
INTO @user

WHILE @@FETCH_STATUS = 0
BEGIN
	
  
 select dbo.Users_Master.NameReg,(select top 1 dbo.secureToken.[DateDeath] from dbo.secureToken
 where usedBy34 = @user) as expiresDate from dbo.Users_Master 
 where
dbo.Users_Master.user34 = @user
 




	FETCH NEXT FROM c1
	INTO @user

	

END


 ---strictly the email portion of the script.    
        IF @user is not NULL   
Begin  
EXEC msdb.dbo.sp_send_dbmail @recipients = 'darryl.emily@scresearch.net', --- 'dup_ran@kswiring.com' 
@query = 'select dbo.Users_Master.NameReg,(select top 1 dbo.secureToken.[DateDeath] from dbo.secureToken where usedBy34 = @user) as expiresDate from dbo.Users_Master where dbo.Users_Master.user34 = @user', 
@subject = 'There are secure Tokens that are about to expire', 
@body = 'The following tokens will expire:', 
@query_result_separator='    ', 
@attach_query_result_as_file = 'TRUE', 
@query_result_width=400 
End  
------ End of the emai section. 


CLOSE c1
DEALLOCATE c1

End

Open in new window

Guy Hengel [angelIII / a3]Billing EngineerCommented:
please change this:

EXEC msdb.dbo.sp_send_dbmail @recipients = 'darryl.emily@scresearch.net', --- 'dup_ran@kswiring.com'
@query = 'select dbo.Users_Master.NameReg,(select top 1 dbo.secureToken.[DateDeath] from dbo.secureToken where usedBy34 = @user) as expiresDate from dbo.Users_Master where dbo.Users_Master.user34 = @user',
@subject = 'There are secure Tokens that are about to expire',
@body = 'The following tokens will expire:',
@query_result_separator='    ',
@attach_query_result_as_file = 'TRUE',
@query_result_width=400

into:

declare @sql varchar(1000)
set @sql =  'select dbo.Users_Master.NameReg,(select top 1 dbo.secureToken.[DateDeath] from dbo.secureToken where usedBy34 = @user) as expiresDate from dbo.Users_Master where dbo.Users_Master.user34 = ''' + replace(@user, '''', '''''') + '' '

EXEC msdb.dbo.sp_send_dbmail @recipients = 'darryl.emily@scresearch.net', --- 'dup_ran@kswiring.com'
@query = @sql,
@subject = 'There are secure Tokens that are about to expire',
@body = 'The following tokens will expire:',
@query_result_separator='    ',
@attach_query_result_as_file = 'TRUE',
@query_result_width=400

dboyd02Author Commented:
Looks like there is an extra ' at the end of the set because it changed everything red after that in the code.

Here is the error:
Msg 102, Level 15, State 1, Line 60
Incorrect syntax near '

EXEC msdb.dbo.sp_send_dbmail @recipients = '.
Msg 105, Level 15, State 1, Line 67
Unclosed quotation mark after the character string ',
@query_result_width=400
End  
------ End of the emai section.


CLOSE c1
DEALLOCATE c1

End
'.

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
--declare all variables used
declare @warningdate date
declare @daysLeft varchar (50)
declare @user varchar(50) 
 
 

--set the warning date variable. This says if its less than 30 days away
 SET @warningdate =   getdate() +30
--SELECT @warningdate




DECLARE c1 CURSOR READ_ONLY
FOR
(SELECT 
      [usedBy34] 
      
  FROM [AssetManagement].[dbo].[secureToken]
  
  where  [DateDeath]  < @warningdate )


OPEN c1

FETCH NEXT FROM c1
INTO @user

WHILE @@FETCH_STATUS = 0
BEGIN
	
  
 select dbo.Users_Master.NameReg,(select top 1 dbo.secureToken.[DateDeath] from dbo.secureToken
 where usedBy34 = @user) as expiresDate from dbo.Users_Master 
 where
dbo.Users_Master.user34 = @user
 




	FETCH NEXT FROM c1
	INTO @user

	

END


 ---strictly the email portion of the script.    
        IF @user is not NULL   
Begin  
declare @sql varchar(1000)
set @sql =  'select dbo.Users_Master.NameReg,(select top 1 dbo.secureToken.[DateDeath] from dbo.secureToken where usedBy34 = @user) as expiresDate from dbo.Users_Master where dbo.Users_Master.user34 = ''' + replace(@user, '''', '''''') + '' '

EXEC msdb.dbo.sp_send_dbmail @recipients = 'darryl.emily@scresearch.net', --- 'dup_ran@kswiring.com' 
@query = @sql, 
@subject = 'There are secure Tokens that are about to expire', 
@body = 'The following tokens will expire:', 
@query_result_separator='    ', 
@attach_query_result_as_file = 'TRUE', 
@query_result_width=400 
End  
------ End of the emai section. 


CLOSE c1
DEALLOCATE c1

End

Open in new window

Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry, I missed a @user variable
declare @sql varchar(1000)
set @sql =  'select dbo.Users_Master.NameReg,(select top 1 dbo.secureToken.[DateDeath] from dbo.secureToken where usedBy34 = ''' + replace(@user, '''''') + ') as expiresDate from dbo.Users_Master where dbo.Users_Master.user34 = ''' + replace(@user, '''', '''''') + '' '

EXEC msdb.dbo.sp_send_dbmail @recipients = 'darryl.emily@scresearch.net', --- 'dup_ran@kswiring.com'
@query = @sql,
@subject = 'There are secure Tokens that are about to expire',
@body = 'The following tokens will expire:',
@query_result_separator='    ',
@attach_query_result_as_file = 'TRUE',
@query_result_width=400 

Open in new window

dboyd02Author Commented:
Still seems to be an extra '  at the end because studio turns all red after the set statement.
I get this error.
Msg 174, Level 15, State 1, Line 60
The replace function requires 3 argument(s).
Msg 105, Level 15, State 1, Line 67
Unclosed quotation mark after the character string ',
@query_result_width=400
End  
------ End of the emai section.


CLOSE c1
DEALLOCATE c1

End
'.

If I remove the last single ' that studios says I should remove, I get this:

Msg 174, Level 15, State 1, Line 60
The replace function requires 3 argument(s).



Thank you so much so far. We are close.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
let me review this ...


ok, this should have the quotes fixed:
declare @sql varchar(1000)
set @sql =  'select dbo.Users_Master.NameReg,(select top 1 dbo.secureToken.[DateDeath] from dbo.secureToken where usedBy34 = ''' + replace(@user, '''', '''''') + ''') as expiresDate from dbo.Users_Master where dbo.Users_Master.user34 = ''' + replace(@user, '''', '''''') + ''' '

EXEC msdb.dbo.sp_send_dbmail @recipients = 'darryl.emily@scresearch.net', --- 'dup_ran@kswiring.com'
@query = @sql,
@subject = 'There are secure Tokens that are about to expire',
@body = 'The following tokens will expire:',
@query_result_separator='    ',
@attach_query_result_as_file = 'TRUE',
@query_result_width=400 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dboyd02Author Commented:
Thank you so much!!!!!!!!!
dboyd02Author Commented:
That was it. Thank you so much angellll !!!!!!!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.