Solved

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

Posted on 2011-03-22
9
260 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 142

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
 
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 142

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now