Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sjpaq@rogers.com
Starting Member
18 Posts |
Posted - 2008-05-30 : 21:30:52
|
I was trying to execute the following update statementupdate dbo.t1 set proc_long_descr = c.pc_mncfrom dbo.t1 p, dbo.t2 c where p.proc_cat_urn = c.proc_cat_urn I am basically updating a description field from a lookup into another table. This normally works without a problem. However I get the following error. Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.This is related to the fact that my table t1 above has the following trigger defined on it.CREATE TRIGGER dbo.tr_t1_ins_upd ON dbo.t1 FOR INSERT, UPDATE AS Update ORprocedure set t1.mod_timestamp_server = getdate() from inserted i where t1.proc_urn = i.proc_urn So I tried to create a cursor loop script where within each iteration of the cursor I update a row. However, I still get the same message.Any help is appreciated. |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-05-30 : 23:43:45
|
quote: Originally posted by sjpaq@rogers.com I was trying to execute the following update statementupdate dbo.t1 set proc_long_descr = c.pc_mncfrom dbo.t1 p, dbo.t2 c where p.proc_cat_urn = c.proc_cat_urn I am basically updating a description field from a lookup into another table. This normally works without a problem. However I get the following error. Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.This is related to the fact that my table t1 above has the following trigger defined on it.CREATE TRIGGER dbo.tr_t1_ins_upd ON dbo.t1 FOR INSERT, UPDATE AS Update ORprocedure set t1.mod_timestamp_server = getdate() from inserted i where t1.proc_urn = i.proc_urn So I tried to create a cursor loop script where within each iteration of the cursor I update a row. However, I still get the same message.Any help is appreciated.
Your posted code must not be the actual code - the trigger won't work as posted. is is dbo.t1 the same table as [ORProcedure] ?also, the error message indicates a subquery is being used to compare to a single value. Your code does not contain any subqueries.One thing about your style of UPDDATE...try coding it like thisupdate t1 set t1.mod_timestamp_server = getdate()from inserted iinner join ORPRocedure t1 on t1.proc_urn = i.proc_urn--This assumes that proc_urn is the primary key of ORPRocedure (or has a Unique constraint on it) Similar problem with your update statement update p set p.proc_long_descr = c.pc_mncfrom dbo.t1 pinner join dbo.t2 c on c.proc_cat_urn = p.proc_cat_urn Be One with the OptimizerTG |
 |
|
sjpaq@rogers.com
Starting Member
18 Posts |
Posted - 2008-05-31 : 06:15:35
|
Thanks TG, yes you are correct the ORProcedure table is indeed t1, I missed replacing one instance of it.Thanks for the tips on SQL, the update statement is mine, however the trigger belongs to a vendor.If the error message is not related to the trigger, is there another DB object that may exist that would be executing when my update runs that would cause this error to occur. I blamed the trigger because I saw another similar post that described this as a problem.Again, appreciate any help. |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-05-31 : 09:00:20
|
>>is there another DB object that may exist that would be executing when my update runs...hmm - is the user update performed inside an SP? - if yes, it maybe due to another statement in the SP.or in a batch with other statements in a series? if yes, it maybe due to another statement in the batch.See if that column you are updating has a "check constraint" on it.Check for other triggers on [ORProcedure]Is the UPDATE statement called from application code? Perhaps the app code is making another statement in the same Method. You could verify this with a sql profiler trace on the server while the update is being made.Be One with the OptimizerTG |
 |
|
sjpaq@rogers.com
Starting Member
18 Posts |
Posted - 2008-05-31 : 09:53:53
|
The update is being fun from a command line in an SQL tool, so not from an SP or the app.These is not check constraint on the column that I am updating.There is another huge trigger on the table which I'll include below. Must be the culprit but I can't understand it all. It's quite long, can you see anything in this trigger that would cause the error that I am getting: "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression." update dbo.t1 set proc_long_descr = c.pc_mncfrom dbo.t1 p, dbo.t2 c where p.proc_cat_urn = c.proc_cat_urn and p.proc_urn = 3 --When this is removed I get the errorThanks again.Table Trigger Text:/****** Object: Trigger dbo.tU_t1 Script Date: 11/18/00 6:16:14 PM ******/CREATE trigger tU_t1 on t1 for UPDATE as/* ERwin Builtin Wed Nov 19 13:39:29 1997 *//* UPDATE trigger on t1 */begin declare @numrows int, @nullcnt int, @validcnt int, @insproc_urn int, @errno int, @errmsg varchar(255) select @numrows = @@rowcount /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* t1 R/137 proc_resource ON PARENT UPDATE RESTRICT */ if /* %ParentPK(" or",update) */ update(proc_urn) begin if exists ( select * from deleted,proc_resource where /* %JoinFKPK(proc_resource,deleted," = "," and") */ proc_resource.proc_urn = deleted.proc_urn ) begin select @errno = 30005, @errmsg = 'Cannot UPDATE "t1" because "proc_resource" exists.' goto error end end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* t1 R/128 surg_proc_history ON PARENT UPDATE SET NULL */ if /* %ParentPK(" or",update) */ update(proc_urn) begin update surg_proc_history set /* %SetFK(surg_proc_history,NULL) */ surg_proc_history.proc_urn = NULL from surg_proc_history,deleted where /* %JoinFKPK(surg_proc_history,deleted," = "," and") */ surg_proc_history.proc_urn = deleted.proc_urn end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* t1 R/126 surg_proc_auth ON PARENT UPDATE RESTRICT */ if /* %ParentPK(" or",update) */ update(proc_urn) begin if exists ( select * from deleted,surg_proc_auth where /* %JoinFKPK(surg_proc_auth,deleted," = "," and") */ surg_proc_auth.proc_urn = deleted.proc_urn ) begin select @errno = 30005, @errmsg = 'Cannot UPDATE "t1" because "surg_proc_auth" exists.' goto error end end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* t1 R/124 booking_procedure ON PARENT UPDATE RESTRICT */ if /* %ParentPK(" or",update) */ update(proc_urn) begin if exists ( select * from deleted,booking_procedure where /* %JoinFKPK(booking_procedure,deleted," = "," and") */ booking_procedure.proc_urn = deleted.proc_urn ) begin select @errno = 30005, @errmsg = 'Cannot UPDATE "t1" because "booking_procedure" exists.' goto error end end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* t1 R/115 procedure_group ON PARENT UPDATE RESTRICT */ if /* %ParentPK(" or",update) */ update(proc_urn) begin if exists ( select * from deleted,procedure_group where /* %JoinFKPK(procedure_group,deleted," = "," and") */ procedure_group.proc_urn = deleted.proc_urn ) begin select @errno = 30005, @errmsg = 'Cannot UPDATE "t1" because "procedure_group" exists.' goto error end end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* t1 R/66 exploded_proc ON PARENT UPDATE RESTRICT */ if /* %ParentPK(" or",update) */ update(proc_urn) begin if exists ( select * from deleted,exploded_proc where /* %JoinFKPK(exploded_proc,deleted," = "," and") */ exploded_proc.exp_proc_urn = deleted.proc_urn ) begin select @errno = 30005, @errmsg = 'Cannot UPDATE "t1" because "exploded_proc" exists.' goto error end end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* t1 R/65 exploded_proc ON PARENT UPDATE RESTRICT */ if /* %ParentPK(" or",update) */ update(proc_urn) begin if exists ( select * from deleted,exploded_proc where /* %JoinFKPK(exploded_proc,deleted," = "," and") */ exploded_proc.proc_urn = deleted.proc_urn ) begin select @errno = 30005, @errmsg = 'Cannot UPDATE "t1" because "exploded_proc" exists.' goto error end end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* t1 R/58 proc_block ON PARENT UPDATE RESTRICT */ if /* %ParentPK(" or",update) */ update(proc_urn) begin if exists ( select * from deleted,proc_block where /* %JoinFKPK(proc_block,deleted," = "," and") */ proc_block.proc_urn = deleted.proc_urn ) begin select @errno = 30005, @errmsg = 'Cannot UPDATE "t1" because "proc_block" exists.' goto error end end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* t1 R/42 room_proc_rest ON PARENT UPDATE RESTRICT */ if /* %ParentPK(" or",update) */ update(proc_urn) begin if exists ( select * from deleted,room_proc_rest where /* %JoinFKPK(room_proc_rest,deleted," = "," and") */ room_proc_rest.proc_urn = deleted.proc_urn ) begin select @errno = 30005, @errmsg = 'Cannot UPDATE "t1" because "room_proc_rest" exists.' goto error end end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* t1 R/17 proc_surg_dur ON PARENT UPDATE RESTRICT */ if /* %ParentPK(" or",update) */ update(proc_urn) begin if exists ( select * from deleted,proc_surg_dur where /* %JoinFKPK(proc_surg_dur,deleted," = "," and") */ proc_surg_dur.proc_urn = deleted.proc_urn ) begin select @errno = 30005, @errmsg = 'Cannot UPDATE "t1" because "proc_surg_dur" exists.' goto error end end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* ud_table_value R/151 t1 ON CHILD UPDATE SET NULL */ if /* %ChildFK(" or",update) */ update(min_wound_class) begin update t1 set /* %SetFK(t1,NULL) */ t1.min_wound_class = NULL from t1,inserted where /* %JoinPKPK(t1,inserted," = "," and") */ t1.proc_urn = inserted.proc_urn and not exists ( select * from ud_table_value where /* %JoinFKPK(inserted,ud_table_value," = "," and") */ inserted.min_wound_class = ud_table_value.udtv_nbr ) end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* ud_table_value R/150 t1 ON CHILD UPDATE SET NULL */ if /* %ChildFK(" or",update) */ update(proc_film_request) begin update t1 set /* %SetFK(t1,NULL) */ t1.proc_film_request = NULL from t1,inserted where /* %JoinPKPK(t1,inserted," = "," and") */ t1.proc_urn = inserted.proc_urn and not exists ( select * from ud_table_value where /* %JoinFKPK(inserted,ud_table_value," = "," and") */ inserted.proc_film_request = ud_table_value.udtv_nbr ) end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* department R/147 t1 ON CHILD UPDATE SET NULL */ if /* %ChildFK(" or",update) */ update(dept_urn) begin update t1 set /* %SetFK(t1,NULL) */ t1.dept_urn = NULL from t1,inserted where /* %JoinPKPK(t1,inserted," = "," and") */ t1.proc_urn = inserted.proc_urn and not exists ( select * from department where /* %JoinFKPK(inserted,department," = "," and") */ inserted.dept_urn = department.dept_urn ) end /* ERwin Builtin Wed Nov 19 13:39:29 1997 */ /* proc_category R/96 t1 ON CHILD UPDATE RESTRICT */ if /* %ChildFK(" or",update) */ update(proc_cat_urn) begin select @nullcnt = 0 select @validcnt = count(*) from inserted,proc_category where /* %JoinFKPK(inserted,proc_category) */ inserted.proc_cat_urn = proc_category.proc_cat_urn /* %NotnullFK(inserted," is null","select @nullcnt = count(*) from inserted where"," and") */ if @validcnt + @nullcnt != @numrows begin select @errno = 30007, @errmsg = 'Cannot UPDATE "t1" because "proc_category" does not exist.' goto error end end /* ERwin Builtin Wed Nov 19 13:39:29 1997 *//* trigger messages to MAGIC interface */DECLARE @mnemonic varchar(10)DECLARE @descr varchar(50)DECLARE @active char(1)DECLARE @proc_cat varchar(10)DECLARE @major_minor intDECLARE @setup_time intDECLARE @teardown_time intDECLARE @proc_len intDECLARE @icd9_code varchar(30)DECLARE @cpt_code varchar(10)DECLARE @proc_long_descr varchar(255)DECLARE @seq_no intDECLARE @msg varchar(255)DECLARE @err_msg varchar(100)If Update(proc_mnc) OR Update(proc_descr) OR Update(proc_active) OR Update(proc_cat_urn) OR Update(proc_major_minor) OR Update(meddiag_urn) OR Update(proc_cpt) OR Update(proc_long_descr)BEGINSelect @mnemonic = proc_mnc, @active = proc_active, @descr = proc_descr, @proc_cat = (Select pc_mnc From proc_category a, inserted b Where a.proc_cat_urn = b.proc_cat_urn), @major_minor = proc_major_minor, @setup_time = (Select ps_setup_dur From proc_surg_dur a, inserted b Where fac_urn = 0 AND surg_urn = 0 AND a.proc_urn = b.proc_urn), @teardown_time = (Select ps_teardown_dur From proc_surg_dur a, inserted b Where fac_urn = 0 AND surg_urn = 0 AND a.proc_urn = b.proc_urn), @proc_len = (Select ps_proc_dur From proc_surg_dur a, inserted b Where fac_urn = 0 AND surg_urn = 0 AND a.proc_urn = b.proc_urn), @icd9_code = (select diag_code from meddiagnosis where diag_urn = meddiag_urn), @cpt_code = proc_cpt, @proc_long_descr = ' 'From insertedIf @mnemonic IS NULL ReturnBEGIN TRANSACTIONSelect @seq_no = Max(seq_no) From magic_interface Where 1=1If @seq_no IS NULL Select @seq_no = 0Select @seq_no = @seq_no + 1Select @msg = "PROC HDR" + "|" + @mnemonic + '|' + @active + '|' + @descr + '|' + @proc_cat + '|' + CONVERT(char(1),@major_minor) + '|' + CONVERT(varchar(10),@setup_time) + '|' + CONVERT(varchar(10),@teardown_time) + '|' + CONVERT(VARCHAR(10),@proc_len) + '|' + @icd9_code + '|' + @cpt_code + '|' + @proc_long_descrInsert Into magic_interface values (@seq_no,@msg)If @@error <> 0 BEGIN SELECT @err_msg = "Error adding procedure " + @mnemonic + " to MAGIC message queue." RAISERROR(@err_msg,16,-1) ROLLBACK TRANSACTION ENDElse COMMIT TRANSACTIONEND returnerror: raiserror @errno @errmsg rollback transactionend |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-05-31 : 10:21:52
|
yep that is one ugly trigger...The problem is likely in the section that contains this statement:If Update(proc_mnc) OR Update(proc_descr) OR Update(proc_active) OR Update(proc_cat_urn) OR Update(proc_major_minor) OR Update(meddiag_urn) OR Update(proc_cpt) OR Update(proc_long_descr)BEGINSelect @mnemonic = proc_mnc,@active = proc_active,@descr = proc_descr,@proc_cat = (Select pc_mnc From proc_category a,inserted bWhere a.proc_cat_urn = b.proc_cat_urn),@major_minor = proc_major_minor,@setup_time = (Select ps_setup_durFrom proc_surg_dur a,inserted bWhere fac_urn = 0 ANDsurg_urn = 0 ANDa.proc_urn = b.proc_urn),.... any of those @param = (select...) statements will generate that error if the "sub-query" returns more than one row.Be One with the OptimizerTG |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-05-31 : 10:30:10
|
Besides that sub-query error it looks as though the trigger assumes that only one row could ever be updated at a time. My assumption is based on the fact that those parameter assignments are derived from the values of the INSERTED table. What if the INSERTED table contains multiple rows? I haven't studied the code in detail so it may protect against that somewhere else but that is a pretty typical error from inexperienced programmers new to triggers. More likely it was written on coordination with the application developers who guantee that only one row will ever get updated at a time. I would still trap for that error in the trigger code just in case...EDIT:or better yet, write the trigger in such a way that it would support multiple row updates even if they aren't expected.Be One with the OptimizerTG |
 |
|
|
|
|
|
|