Solved

MS SQL Server - issue with inserting data into table with trigger on it..

Posted on 2011-03-22
9
263 Views
Last Modified: 2012-05-11
I get this error:

Msg 512, Level 16, State 1, Procedure TRIGGER_GRP_Msg_Queue_Recieve, Line 98
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
0
Comment
Question by:GlobaLevel
  • 4
  • 3
  • 2
9 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 35190781
We will need to see the query to be of any help.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35190801
Most likely you are setting the value of a field to the result of a query, and that query is returning more than one value.  For example:

update tbl
set X = (select Y from Z)

If there is more than one row returned from the sub-query, the update will fail.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35190874
check the trigger code, it seems it does not handle inserts with multiple rows properly
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 10

Author Comment

by:GlobaLevel
ID: 35191630
okay here is the code section from trigger...
/*****************
 
 ---------
 
 --   REPORT --
 
          --------
 
 ******************/
 
 --
 
 -- if the msg is nothing text back to recepient and say...try
 again--------------
 if @campaign_message = ''
 BEGIN
 
 INSERT INTO Message_Queue (TO_Number, Campaign_Message) Values 
 (@to_number,'Oops, we did not understand ur reply. Probably due
 2 spelling error
 or u took 2 long to reply. Pls check spelling and resend to' + 
 @sms_server_number + '. U can reply STOP 2 end.')
 
 END
 
 
 ----------------------------------------------------------------------
 ---------------
 
 -- if recp...STOP...then STOP...
 if @campaign_message = 'STOP'
 BEGIN
 
 INSERT INTO Message_Queue (TO_Number, Campaign_Message) Values 
 (@to_number,'You have been opted out of this campaign. Thank you for 
 your time.')
 
 Update grp_campaign set stop = 'STOP' where to_number = @to_number and 
 sms_server_num = @sms_server_number
 
 END
 

 
 -- if recp...send message..repond appropriatelly...
 if @campaign_message <> ''
 BEGIN
 
 -- 1. get the old message into a var
 
 declare @old_grp_msg nvarchar(max)
 set @old_grp_msg = (SELECT rolling_convo_msg from grp_campaign where 
 to_number = @to_number and sms_server_num =  @sms_server_number)
 
 
 -- 2. get convo id...
 declare @convo_id nvarchar(max)
 set @convo_id = (SELECT convo_id from grp_campaign WHERE to_number = 
 @to_number)
 
 -- 3. then tack on the newest response + NEW LINE SPACE!!!
 
 declare @grp_tc_id nvarchar(max)
 set @grp_tc_id = (SELECT TC_ID from grp_campaign where to_number = 
 @to_number and sms_server_num =  @sms_server_number)
 
 declare @new_part nvarchar(max)
 set @new_part = @campaign_message
 --set @new_part = @old_grp_msg + '%0a' + UPPER(@convo_id) + ' :' + 
 --'%0a' + @new_part + '%0a'
 set @new_part = @old_grp_msg + '  <<<    ' + UPPER(@convo_id)  +  ' >>> ' + ' :' +
 '     ' + @new_part + '     '
 
 -- 4. get the last 500 chars into a new var update grp_campaign set 
 rolling_convo_msg = RIGHT(@new_part,250) where tc_id = @grp_tc_id
 
 -- 5. send update msg to all participats insert into grp_message_queue 
 (to_number,rolling_convo_msg,sms_server_num)
 SELECT to_number, rolling_convo_msg,sms_server_num from grp_campaign 
 where tc_id = @grp_tc_id
 
 
 
 END

Open in new window

0
 
LVL 33

Accepted Solution

by:
knightEknight earned 250 total points
ID: 35191770
Here is an example of a query that might cause a problem IF it returns more than one row (which is data dependent, so I can't tell by looking at it)


 declare @old_grp_msg nvarchar(max)
 set @old_grp_msg = (SELECT rolling_convo_msg from grp_campaign where
   to_number = @to_number and sms_server_num =  @sms_server_number)
 
 You can try adding DISTINCT, which will only fix the problem if the multiple-row issue is due to identical rows:


 declare @old_grp_msg nvarchar(max)
 set @old_grp_msg = (SELECT DISTINCT rolling_convo_msg from grp_campaign where
   to_number = @to_number and sms_server_num =  @sms_server_number)
 
 or you can try adding TOP 1, which will cause the query to work, but may not return the value you expect, if there is more than one possible value:


 declare @old_grp_msg nvarchar(max)
 set @old_grp_msg = (SELECT TOP 1 rolling_convo_msg from grp_campaign where
   to_number = @to_number and sms_server_num =  @sms_server_number)
 
 This needs to be done for all queries like this in your trigger where a value from a query is being assigned to a variable.
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35191794
copy/paste...messed it up a bit..
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35191807
copy/paste...messed it up a bit..
/*****************
 
 ---------
 
 --   REPORT --
 
          --------
 
 ******************/
 
 --
 
 -- if the msg is nothing text back to recepient and say...try
 again--------------
 if @campaign_message = ''
 BEGIN
 
 INSERT INTO Message_Queue (TO_Number, Campaign_Message) Values 
 (@to_number,'Oops, we did not understand ur reply. Probably due
 2 spelling error
 or u took 2 long to reply. Pls check spelling and resend to' + 
 @sms_server_number + '. U can reply STOP 2 end.')
 
 END
 
 
 ----------------------------------------------------------------------
 ---------------
 
 -- if recp...STOP...then STOP...
 if @campaign_message = 'STOP'
 BEGIN
 
 INSERT INTO Message_Queue (TO_Number, Campaign_Message) Values 
 (@to_number,'You have been opted out of this campaign. Thank you for 
 your time.')
 
 Update grp_campaign set stop = 'STOP' where to_number = @to_number and 
 sms_server_num = @sms_server_number
 
 END
 

 
 -- if recp...send message..repond appropriatelly...
 if @campaign_message <> ''
 BEGIN
 
 -- 1. get the old message into a var
 
 declare @old_grp_msg nvarchar(max)
 set @old_grp_msg = (SELECT rolling_convo_msg from grp_campaign where 
 to_number = @to_number and sms_server_num =  @sms_server_number)
 
 
 -- 2. get convo id...
 declare @convo_id nvarchar(max)
 set @convo_id = (SELECT convo_id from grp_campaign WHERE to_number = 
 @to_number)
 
 -- 3. then tack on the newest response + NEW LINE SPACE!!!
 
 declare @grp_tc_id nvarchar(max)
 set @grp_tc_id = (SELECT TC_ID from grp_campaign where to_number = 
 @to_number and sms_server_num =  @sms_server_number)
 
 declare @new_part nvarchar(max)
 set @new_part = @campaign_message
 --set @new_part = @old_grp_msg + '%0a' + UPPER(@convo_id) + ' :' + 
 --'%0a' + @new_part + '%0a'
 set @new_part = @old_grp_msg + '  <<<    ' + UPPER(@convo_id)  +  ' >>> ' + ' :' +
 '     ' + @new_part + '     '
 
 -- 4. get the last 500 chars into a new var 
 update grp_campaign set 
 rolling_convo_msg = RIGHT(@new_part,250) where tc_id = @grp_tc_id
 
 -- 5. send update msg to all participats 
insert into grp_message_queue (to_number,rolling_convo_msg,sms_server_num)
 SELECT to_number, rolling_convo_msg,sms_server_num from grp_campaign 
 where tc_id = @grp_tc_id
 
 
 
 END

Open in new window

0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 35192405
...any help on this..?
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 35193019
the issue has been explained above, aka queries like this:
set @grp_tc_id = (SELECT TC_ID from grp_campaign where to_number = 
 @to_number and sms_server_num =  @sms_server_number) 

Open in new window

can raise the error mentioned if there are 2 or more rows for the selected @sms_server_num value.

we cannot solve/check that, you have to go through the data + code to isolate the issue.
and then solve it by not using such non-set programming code, but SQL (T-SQL) code

  update  c
 set  rolling_convo_msg = RIGHT(@new_part,250) 
  from grp_campaign c
  where to_number =  @to_number 
   and sms_server_num =  @sms_server_number 

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question