Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:49 PM
pyrahna
 
Posts: n/a
Default Error on Trigger Launch

I get the following error when i try to update a table with the a
trigger (trigger script below),
"Error Message: The row value(s) updated or deleted either do not make
the row unique or they alter multiple rows(3 rows)"

When i run the update on the table with the trigger disabled it works
with no problem. Every table involved has a primary key and as you
can see in the trigger code i set the nocount property to on. I am at
a loss for ideas. thanks for your help, Names have been changed to
protect the guilty


GO
/****** Object: Trigger [dbo].[ecr_approval_update] Script Date:
02/26/2008 09:04:47 ******/
SET NOCOUNT ON
GO

SET ANSI_NULLS ON
go

SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[ecr_approval_update]
ON [dbo].[ecr_approval]
after UPDATE

as
IF ( UPDATE (rd_approval) or UPDATE (prod_approval) )
BEGIN
Declare @r_d_release bit, @production_release bit, @ecr_id
uniqueidentifier,
@mail_text nvarchar(2000), @ecr_num numeric(6, 0), @date datetime,
@ecr_user nvarchar(50), @ecr_type nvarchar(50), @ecr_project
nvarchar(50),
@part_number nvarchar(50), @part_rev nvarchar(5), @part_group
nvarchar(50),
@part_descrip nvarchar(40), @ecr_description nvarchar(500),
@drawing_location nvarchar(100), @email_list nvarchar(1000)

declare @emails table(user_email nvarchar(50))

Select @ecr_id = ecr_id, @r_d_release = rd_approval,
@production_release = prod_approval
from inserted

DECLARE @tableHTML NVARCHAR(MAX) ;

SET @tableHTML =
N'<font size="2" face="Ariel">' +
N'<H1>Change Notification Submital Notice</H1>' +
N'<table border="1" >' +--cellpadding="20"
N'<tr><th>CN #</th><th>Date</th>' +
N'<th>CN User</th><th>CN Type</th><th>CN Project</th>' +
N'<th> Part_Number </th><th>Part Rev</th><th>Part Group</th>' +
N'<th>Part Description</th><th>CN Description</th><th>Drawing
Location</th></tr>' +
CAST ( ( SELECT td = ecr_num, '',
td = date, '',
td = ecr_user, '',
td = ecr_type, '',
td = ecr_project, '',
td = part_number, '',
td = part_rev, '',
td = part_group, '',
td = part_descrip, '',
td = ecr_description, '',
td = drawing_location
from tbl_ecr where ecr_id = @ecr_id
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>'+
N'<F1>This is an auto generated email please do not respond.</F1>'
+
N'<F2>The information contained in this message is confidential and
is intended solely for the recipient(s)</F2>' +
N'<F3>identified above. If you are not the intended recipient or
there are any problems, please notify the sender </F3>' +
N'<F4>immediately. The unauthorized use, disclosure, or copying of
this message is strictly forbidden and may subject you to legal
action.</F4>' +
N'</font>';
Declare @email nvarchar(50)
if ( @r_d_release = 'True')
Begin
insert into @emails(user_email)
SELECT user_email
from tbl_CN_Notifications
where RD_Approved = 'True'
if ((select r_d_cup_open from tbl_ecr where ecr_id = @ecr_id) =
'True')
begin
insert into @emails(user_email)
SELECT user_email
from tbl_CN_Notifications
where RD_Approved_Cup_Open = 'True'
end
if ((select r_d_cup_plate from tbl_ecr where ecr_id = @ecr_id) =
'True')
begin
insert into @emails(user_email)
SELECT user_email
from tbl_CN_Notifications
where RD_Approved_Cup_Plate = 'True'
end
if ((select r_d_busch_open from tbl_ecr where ecr_id = @ecr_id) =
'True' or (select r_d_busch_plate from tbl_ecr where ecr_id = @ecr_id)
= 'True')
begin
insert into @emails(user_email)
SELECT user_email
from tbl_CN_Notifications
where RD_Approved_Busch = 'True'
end
if ((select part_group from tbl_ecr where ecr_id = @ecr_id) =
'Cylinder Head System')
begin
insert into @emails(user_email)
SELECT user_email
from tbl_CN_Notifications
where RD_Approved_Cylinder_Head = 'True'
end
if ((select part_group from tbl_ecr where ecr_id = @ecr_id) =
'Valvetrain System')
begin
insert into @emails(user_email)
SELECT user_email
from tbl_CN_Notifications
where RD_Approved_Valvetrain = 'True'
end
Declare email_curs CURSOR FOR
Select user_email From @emails group by user_email
Open email_curs

Set @email_list = ''
Fetch Next FROM email_curs into @email

While (@@FETCH_STATUS <> -1)
BEGIN
if @email_list <> ''
begin
select @email_list = @email_list+ '; '
end
select @email_list = @email_list + @email
Fetch Next FROM email_curs into @email
END

CLOSE email_curs
DEALLOCATE email_curs

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'ECR',
@recipients = @email_list,
@body = @tableHTML,
@body_format = 'HTML',
@subject = 'A New R+D CN Has Been Approved'
End
if ( @production_release = 'True')
Begin
insert into @emails(user_email)
SELECT DISTINCT user_email
from tbl_CN_Notifications
where Production_Approval = 'True' or CNs_created = 'True'
or Production_All = 'True'
if ((select production_busch_open from tbl_ecr where ecr_id =
@ecr_id) = 'True' or (select production_busch_plate from tbl_ecr where
ecr_id = @ecr_id) = 'True')
begin
insert into @emails(user_email)
SELECT DISTINCT user_email
from tbl_CN_Notifications
where Production_Busch = 'True'
end
if ((select part_group from tbl_ecr where ecr_id = @ecr_id) =
'Cylinder Head System')
begin
insert into @emails(user_email)
SELECT DISTINCT user_email
from tbl_CN_Notifications
where Production_Cylinder_Head = 'True'
end
if ((select part_group from tbl_ecr where ecr_id = @ecr_id) =
'Valvetrain System')
begin
insert into @emails(user_email)
SELECT DISTINCT user_email
from tbl_CN_Notifications
where Production_Valvetrain = 'True'
end
Declare email_curs CURSOR FOR
SELECT DISTINCT user_email From @emails group by user_email
Open email_curs
Set @email_list = ''
Fetch Next FROM email_curs into @email

While (@@FETCH_STATUS <> -1)
BEGIN
if @email_list <> ''
begin
select @email_list = @email_list+ '; '
end
select @email_list = @email_list + @email
Fetch Next FROM email_curs into @email
END

CLOSE email_curs
DEALLOCATE email_curs

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'ECR',
@recipients = @email_list,
@body = @tableHTML,
@body_format = 'HTML',
@subject = 'A New Production CN Has Been Approved'
End
END

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:49 PM
pyrahna
 
Posts: n/a
Default Re: Error on Trigger Launch

I figured it out. I needed to move the "SET NOCOUNT ON" below the as
statement. Then it worked flawlessly.



On Feb 26, 9:34 am, pyrahna <pltayl...@gmail.com> wrote:
> I get the following error when i try to update a table with the a
> trigger (trigger script below),
> "Error Message: The row value(s) updated or deleted either do not make
> the row unique or they alter multiple rows(3 rows)"
>
> When i run the update on the table with the trigger disabled it works
> with no problem. Every table involved has a primary key and as you
> can see in the trigger code i set the nocount property to on. I am at
> a loss for ideas. thanks for your help, Names have been changed to
> protect the guilty
>
> GO
> /****** Object: Trigger [dbo].[ecr_approval_update] Script Date:
> 02/26/2008 09:04:47 ******/
> SET NOCOUNT ON
> GO
>
> SET ANSI_NULLS ON
> go
>
> SET QUOTED_IDENTIFIER ON
> GO
>
> ALTER TRIGGER [dbo].[ecr_approval_update]
> ON [dbo].[ecr_approval]
> after UPDATE
>
> as
> IF ( UPDATE (rd_approval) or UPDATE (prod_approval) )
> BEGIN
> Declare @r_d_release bit, @production_release bit, @ecr_id
> uniqueidentifier,
> @mail_text nvarchar(2000), @ecr_num numeric(6, 0), @date datetime,
> @ecr_user nvarchar(50), @ecr_type nvarchar(50), @ecr_project
> nvarchar(50),
> @part_number nvarchar(50), @part_rev nvarchar(5), @part_group
> nvarchar(50),
> @part_descrip nvarchar(40), @ecr_description nvarchar(500),
> @drawing_location nvarchar(100), @email_list nvarchar(1000)
>
> declare @emails table(user_email nvarchar(50))
>
> Select @ecr_id = ecr_id, @r_d_release = rd_approval,
> @production_release = prod_approval
> from inserted
>
> DECLARE @tableHTML NVARCHAR(MAX) ;
>
> SET @tableHTML =
> N'<font size="2" face="Ariel">' +
> N'<H1>Change Notification Submital Notice</H1>' +
> N'<table border="1" >' +--cellpadding="20"
> N'<tr><th>CN #</th><th>Date</th>' +
> N'<th>CN User</th><th>CN Type</th><th>CN Project</th>' +
> N'<th> Part_Number </th><th>Part Rev</th><th>Part Group</th>' +
> N'<th>Part Description</th><th>CN Description</th><th>Drawing
> Location</th></tr>' +
> CAST ( ( SELECT td = ecr_num, '',
> td = date, '',
> td = ecr_user, '',
> td = ecr_type, '',
> td = ecr_project, '',
> td = part_number, '',
> td = part_rev, '',
> td = part_group, '',
> td = part_descrip, '',
> td = ecr_description, '',
> td = drawing_location
> from tbl_ecr where ecr_id = @ecr_id
> FOR XML PATH('tr'), TYPE
> ) AS NVARCHAR(MAX) ) +
> N'</table>'+
> N'<F1>This is an auto generated email please do not respond.</F1>'
> +
> N'<F2>The information contained in this message is confidential and
> is intended solely for the recipient(s)</F2>' +
> N'<F3>identified above. If you are not the intended recipient or
> there are any problems, please notify the sender </F3>' +
> N'<F4>immediately. The unauthorized use, disclosure, or copying of
> this message is strictly forbidden and may subject you to legal
> action.</F4>' +
> N'</font>';
> Declare @email nvarchar(50)
> if ( @r_d_release = 'True')
> Begin
> insert into @emails(user_email)
> SELECT user_email
> from tbl_CN_Notifications
> where RD_Approved = 'True'
> if ((select r_d_cup_open from tbl_ecr where ecr_id = @ecr_id) =
> 'True')
> begin
> insert into @emails(user_email)
> SELECT user_email
> from tbl_CN_Notifications
> where RD_Approved_Cup_Open = 'True'
> end
> if ((select r_d_cup_plate from tbl_ecr where ecr_id = @ecr_id) =
> 'True')
> begin
> insert into @emails(user_email)
> SELECT user_email
> from tbl_CN_Notifications
> where RD_Approved_Cup_Plate = 'True'
> end
> if ((select r_d_busch_open from tbl_ecr where ecr_id = @ecr_id) =
> 'True' or (select r_d_busch_plate from tbl_ecr where ecr_id = @ecr_id)
> = 'True')
> begin
> insert into @emails(user_email)
> SELECT user_email
> from tbl_CN_Notifications
> where RD_Approved_Busch = 'True'
> end
> if ((select part_group from tbl_ecr where ecr_id = @ecr_id) =
> 'Cylinder Head System')
> begin
> insert into @emails(user_email)
> SELECT user_email
> from tbl_CN_Notifications
> where RD_Approved_Cylinder_Head = 'True'
> end
> if ((select part_group from tbl_ecr where ecr_id = @ecr_id) =
> 'Valvetrain System')
> begin
> insert into @emails(user_email)
> SELECT user_email
> from tbl_CN_Notifications
> where RD_Approved_Valvetrain = 'True'
> end
> Declare email_curs CURSOR FOR
> Select user_email From @emails group by user_email
> Open email_curs
>
> Set @email_list = ''
> Fetch Next FROM email_curs into @email
>
> While (@@FETCH_STATUS <> -1)
> BEGIN
> if @email_list <> ''
> begin
> select @email_list = @email_list+ '; '
> end
> select @email_list = @email_list + @email
> Fetch Next FROM email_curs into @email
> END
>
> CLOSE email_curs
> DEALLOCATE email_curs
>
> EXEC msdb.dbo.sp_send_dbmail
> @profile_name = 'ECR',
> @recipients = @email_list,
> @body = @tableHTML,
> @body_format = 'HTML',
> @subject = 'A New R+D CN Has Been Approved'
> End
> if ( @production_release = 'True')
> Begin
> insert into @emails(user_email)
> SELECT DISTINCT user_email
> from tbl_CN_Notifications
> where Production_Approval = 'True' or CNs_created = 'True'
> or Production_All = 'True'
> if ((select production_busch_open from tbl_ecr where ecr_id =
> @ecr_id) = 'True' or (select production_busch_plate from tbl_ecr where
> ecr_id = @ecr_id) = 'True')
> begin
> insert into @emails(user_email)
> SELECT DISTINCT user_email
> from tbl_CN_Notifications
> where Production_Busch = 'True'
> end
> if ((select part_group from tbl_ecr where ecr_id = @ecr_id) =
> 'Cylinder Head System')
> begin
> insert into @emails(user_email)
> SELECT DISTINCT user_email
> from tbl_CN_Notifications
> where Production_Cylinder_Head = 'True'
> end
> if ((select part_group from tbl_ecr where ecr_id = @ecr_id) =
> 'Valvetrain System')
> begin
> insert into @emails(user_email)
> SELECT DISTINCT user_email
> from tbl_CN_Notifications
> where Production_Valvetrain = 'True'
> end
> Declare email_curs CURSOR FOR
> SELECT DISTINCT user_email From @emails group by user_email
> Open email_curs
> Set @email_list = ''
> Fetch Next FROM email_curs into @email
>
> While (@@FETCH_STATUS <> -1)
> BEGIN
> if @email_list <> ''
> begin
> select @email_list = @email_list+ '; '
> end
> select @email_list = @email_list + @email
> Fetch Next FROM email_curs into @email
> END
>
> CLOSE email_curs
> DEALLOCATE email_curs
>
> EXEC msdb.dbo.sp_send_dbmail
> @profile_name = 'ECR',
> @recipients = @email_list,
> @body = @tableHTML,
> @body_format = 'HTML',
> @subject = 'A New Production CN Has Been Approved'
> End
> END


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 02:49 PM
Hugo Kornelis
 
Posts: n/a
Default Re: Error on Trigger Launch

On Tue, 26 Feb 2008 06:34:02 -0800 (PST), pyrahna wrote:

>I get the following error when i try to update a table with the a
>trigger (trigger script below),
>"Error Message: The row value(s) updated or deleted either do not make
>the row unique or they alter multiple rows(3 rows)"


Hi pyrahna,

That does not sound like a SQL Server error message. But rather like a
message generated by your front end. Many front end programs can get
confused when triggers modify data. This message sounds like such an
issue.

However, I saw a different problem with your trigger:

(snip)
> Select @ecr_id = ecr_id, @r_d_release = rd_approval,
> @production_release = prod_approval
> from inserted


If an UPDATE statement affects multiple rows, this will cause one (more
or less at random) of them to be processed. The others will be ignored
by your trigger. I don't think that is what you want!

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 02:49 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Error on Trigger Launch

pyrahna (pltaylor3@gmail.com) writes:
> Select @ecr_id = ecr_id, @r_d_release = rd_approval,
> @production_release = prod_approval
> from inserted


Ohoh, that won't fly. A trigger fires once per statement, so if there
are more that row updated, you will only send mail for one row.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 02:49 PM
pyrahna
 
Posts: n/a
Default Re: Error on Trigger Launch

It was an error message from Microsoft SQL Server Managment Studio
front end....not technically a sql message but i would hope that it
would handle error messages decently.

As for the select statement that has been pointed out by two people
now I don't see it being a problem in the application, but I am
interested in possible solutions and work arounds so that the problem
doesn't exist and so Ii can better at this.
thank you for your time



On Feb 26, 5:48 pm, Hugo Kornelis
<h...@perFact.REMOVETHIS.info.INVALID> wrote:
> On Tue, 26 Feb 2008 06:34:02 -0800 (PST), pyrahna wrote:
> >I get the following error when i try to update a table with the a
> >trigger (trigger script below),
> >"Error Message: The row value(s) updated or deleted either do not make
> >the row unique or they alter multiple rows(3 rows)"

>
> Hi pyrahna,
>
> That does not sound like a SQL Server error message. But rather like a
> message generated by your front end. Many front end programs can get
> confused when triggers modify data. This message sounds like such an
> issue.
>
> However, I saw a different problem with your trigger:
>
> (snip)
>
> > Select @ecr_id = ecr_id, @r_d_release = rd_approval,
> > @production_release = prod_approval
> > from inserted

>
> If an UPDATE statement affects multiple rows, this will cause one (more
> or less at random) of them to be processed. The others will be ignored
> by your trigger. I don't think that is what you want!
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog:http://sqlblog.com/blogs/hugo_kornelis


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-01-2008, 02:49 PM
Hugo Kornelis
 
Posts: n/a
Default Re: Error on Trigger Launch

On Wed, 27 Feb 2008 03:57:04 -0800 (PST), pyrahna wrote:

>It was an error message from Microsoft SQL Server Managment Studio
>front end....not technically a sql message but i would hope that it
>would handle error messages decently.


Hi pyrahna,

I only now see your own follow up message about the SET NOCOUNT. Yes,
omitting this command at the beginning of a trigger or stored procedure
results in extra results being sent to the client, which can cause all
kind of problems.

>As for the select statement that has been pointed out by two people
>now I don't see it being a problem in the application, but I am
>interested in possible solutions and work arounds so that the problem
>doesn't exist and so Ii can better at this.


The ideal method would be to replace the entire trigger with a single
set-based logic. However, the trigger code is too complicated and too
long for me to try this from here and in my spare time. So either you do
that yourself, or you take a shortcut and simply use a cursor to process
all rows in the "inserted" pseudo table one by one.

Note that a cursor in a trigger is NOT generally recommended since it
hurts performance, but each rule has its exceptions.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-01-2008, 02:49 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Error on Trigger Launch

pyrahna (pltaylor3@gmail.com) writes:
> It was an error message from Microsoft SQL Server Managment Studio
> front end....not technically a sql message but i would hope that it
> would handle error messages decently.


The Open Table thing in Mgmt Studio is fairly crappy in my opinion. I never
use it myself.

> As for the select statement that has been pointed out by two people
> now I don't see it being a problem in the application, but I am
> interested in possible solutions and work arounds so that the problem
> doesn't exist and so Ii can better at this.
> thank you for your time


You could set up a cursor that loops over inserted, if you think that
multi-row updates are rare.

Or you can be this brutal and add this first in the trigger:

IF @@rowcount > 1
BEGIN
ROLLBACK TRANSACTION
RAISERROR('Multi-row updates are not permitted on this table', 16, 1)
RETURN
END



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:39 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562