Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

optimized update procedure. 1

Status
Not open for further replies.

N3XuS

Programmer
Mar 1, 2002
339
BE
I have a procedure to update some values in a table. My question is if it's best to check if a value has changed before updating it, or if SQL already does that by itself. How does MS SQL go about updating a lot of values even if they didn't change.
I'm just using this atm..
Code:
ALTER PROCEDURE dbo.updnavigation
 @id int,
 @parent int,
 @text int,
 @name nvarchar(50)
AS
		if exists(select * from tlkpnavigation where id = @id)
            Begin
                            UPDATE tlkpnavigation SET parent = @parent, text = @text, name = @name
                            WHERE (id = @id)
            End 
	RETURN

where if I don't have to check for changes in the parameters I can just do a count * instead of a select to save recources ???

Many thx
 
You could do
UPDATE tlkpnavigation SET parent = @parent, text = @text, name = @name
WHERE (id = @id)
(and name <> @name
or parent <> @parent
or text <> @text)



“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" --
 
that'ld be an improvement but can still possibly update 2 values that didn't have to be changed :)
 
when you update 1 value or 5 values doesn't matter a delete and an insert happens
The whole row in the DB will be 'updated'with all the fields

“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" --
 
ok well that solves it :) thanks Denis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top