Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-22
9
Medium Priority
?
275 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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 1000 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Integration Management Part 2
Screencast - Getting to Know the Pipeline

886 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