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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 update the data field

Author  Topic 

joey ng
Starting Member

22 Posts

Posted - 2008-06-25 : 01:03:07
HI,i am joey and i have question about update trigger.I have 3000 record and I want update some data field, is it have any method to update the data field which have changed only?Because it is take too long time to update the data.

joey

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-25 : 01:17:51
Didint get that. changed when? Do you have a date audit field in your table like datecreated,datemodified etc?
Go to Top of Page

joey ng
Starting Member

22 Posts

Posted - 2008-06-25 : 01:56:20
i am using the instead of update trigger to update the data and the format of the trigger is like below:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER OFF
GO
CREATE TRIGGER [dbo].[T_U_employee] ON [dbo].[employee]
INSTEAD OF UPDATE
AS

DECLARE @lcsql VARCHAR(max), @lddate DATETIME

SET @lcsql = ''

SELECT * INTO #inserted_tmp FROM inserted
CREATE INDEX inserted_tmp ON #inserted_tmp (id)

IF UPDATE(id)
SET @lcsql = @lcsql + ' employee.cnoee = #inserted_tmp.id,'

IF UPDATE(fname)
SET @lcsql = @lcsql + ' employee.fname = #inserted_tmp.fname,'

IF UPDATE(lname)
SET @lcsql = @lcsql + ' employee.lname = #inserted_tmp.lname,'

IF UPDATE(address)
SET @lcsql = @lcsql + ' employee.address = #inserted_tmp.address,'
.
.
.
.
SET @lcsql = 'Update employee SET ' + left(@lcsql, len(@lcsql) -1) + ' from employee, #inserted_tmp where employee.id=#inserted_tmp.id

EXEC (@lcsql)


With this trigger,if i just want to update the address, it also will run the other data field althought the other data field didnt need to update.So, is it have any method to update the changed data field?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-25 : 01:59:10
quote:
Originally posted by joey ng

i am using the instead of update trigger to update the data and the format of the trigger is like below:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER OFF
GO
CREATE TRIGGER [dbo].[T_U_employee] ON [dbo].[employee]
INSTEAD OF UPDATE
AS

DECLARE @lcsql VARCHAR(max), @lddate DATETIME

SET @lcsql = ''

SELECT * INTO #inserted_tmp FROM inserted
CREATE INDEX inserted_tmp ON #inserted_tmp (id)

IF UPDATE(id)
SET @lcsql = @lcsql + ' employee.cnoee = #inserted_tmp.id,'

IF UPDATE(fname)
SET @lcsql = @lcsql + ' employee.fname = #inserted_tmp.fname,'

IF UPDATE(lname)
SET @lcsql = @lcsql + ' employee.lname = #inserted_tmp.lname,'

IF UPDATE(address)
SET @lcsql = @lcsql + ' employee.address = #inserted_tmp.address,'
.
.
.
.
SET @lcsql = 'Update employee SET ' + left(@lcsql, len(@lcsql) -1) + ' from employee, #inserted_tmp where employee.id=#inserted_tmp.id

EXEC (@lcsql)


With this trigger,if i just want to update the address, it also will run the other data field althought the other data field didnt need to update.So, is it have any method to update the changed data field?



Didnt understand the need of a trigger for this. Can you specify what is your full requirement?
Go to Top of Page

joey ng
Starting Member

22 Posts

Posted - 2008-06-25 : 02:11:47
ok..when we update the data is using the application which inner connect to the database,so after we compute the record, it will update and insert the new record into the particular table.So we need to use the update and insert trigger to update and insert the data.But we realise the update data was spend many time to update record and it is impact the performance...now i am finding some method to solve this problem...







Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-25 : 02:17:46
quote:
Originally posted by joey ng

ok..when we update the data is using the application which inner connect to the database,so after we compute the record, it will update and insert the new record into the particular table.So we need to use the update and insert trigger to update and insert the data.But we realise the update data was spend many time to update record and it is impact the performance...now i am finding some method to solve this problem...










Are you trying to make some changes to data inserted/updated by application?The use of trigger itself will have a impact on performance. Thats why i asked if there's a need for use of trigger here? I'm still unclear what exactly your requirement is. It would be better if you can explain it with some sample data from your tables.
Go to Top of Page

joey ng
Starting Member

22 Posts

Posted - 2008-06-25 : 02:30:26
yup,we must use the update trigger to update the record and use the application to insert and update data.For the example i hav a table "employee" ,inside the employee have fname,lname,id,address.But the application maybe will update the "id" data field and the other data field's value is no changed but the application also will update all the data field.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-25 : 02:31:54
quote:
Originally posted by joey ng

yup,we must use the update trigger to update the record and use the application to insert and update data.For the example i hav a table "employee" ,inside the employee have fname,lname,id,address.But the application maybe will update the "id" data field and the other data field's value is no changed.



So you will update them with some hardcoded value? else, how will you decide what value should be populated to field?
Go to Top of Page

joey ng
Starting Member

22 Posts

Posted - 2008-06-25 : 02:34:23
yup, we use the hardcoded value to update.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-25 : 02:42:17
quote:
Originally posted by joey ng

yup, we use the hardcoded value to update.




Then what you need is this in trigger

UPDATE t
SET t.fname=Yourfnamevalue,
t.lname=yourlnamevalue,
t.address=youraddressvalue
FROM YourTable t
INNER JOIN INSERTED i
ON i.id=t.id


b/w if it was just insert happening you could have just created three default constraints on fname,lname & address fields so that it will autoinsert these values when application puts the id values without the need of a trigger.
Go to Top of Page

joey ng
Starting Member

22 Posts

Posted - 2008-06-25 : 02:48:06
ok..thanks..if we use the other way to update the record such as the value is retrieve from the screen then any method to solve the same problem?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-25 : 03:28:38
quote:
Originally posted by joey ng

ok..thanks..if we use the other way to update the record such as the value is retrieve from the screen then any method to solve the same problem?


retrive from screen? didnt understand what you meant by that.
Go to Top of Page

joey ng
Starting Member

22 Posts

Posted - 2008-06-25 : 04:07:11
what i mean is the value is retrieve from screen which user key in.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-25 : 04:12:26
quote:
Originally posted by joey ng

what i mean is the value is retrieve from screen which user key in.


You can make SQl prompt for values from user. This needs to be done at your front end through application and you need to grab the values entered by user in application through varaibles and pass it down as a parameter to SQL stored procedure or query and do the update like

UPDATE t
SET t.fname=@fname,
t.lname=@lname,
t.address=@address
FROM YourTable t
WHERE t.id=@id


where @fname,@lname... are parameters containing user entered values for updation of record with id value @id
Go to Top of Page

joey ng
Starting Member

22 Posts

Posted - 2008-06-25 : 06:00:03
Finally I solve it...Thanks visakh16...you really help me a lot..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-25 : 06:01:30
quote:
Originally posted by joey ng

Finally I solve it...Thanks visakh16...you really help me a lot..


you're welcome
Go to Top of Page

joey ng
Starting Member

22 Posts

Posted - 2008-06-25 : 21:13:51
After test from screen was fail again...the below is the trigger command which i write

set ANSI_NULLS ON
set QUOTED_IDENTIFIER OFF
GO
ALTER TRIGGER [dbo].[T_U_employee] ON [employeee]
INSTEAD OF UPDATE
AS

DECLARE @id VARCHAR(128)
DECLARE @fname VARCHAR(128)
DECLARE @salary VARCHAR(128)

Update Dxpayenc

SET employee.id =@id,

employee.fname=@fname,

employee.salary=@salary

from employee
where employee.id=@id
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-26 : 00:18:42
quote:
Originally posted by joey ng

After test from screen was fail again...the below is the trigger command which i write

set ANSI_NULLS ON
set QUOTED_IDENTIFIER OFF
GO
ALTER TRIGGER [dbo].[T_U_employee] ON [employeee]
INSTEAD OF UPDATE
AS

DECLARE @id VARCHAR(128)
DECLARE @fname VARCHAR(128)
DECLARE @salary VARCHAR(128)

Update Dxpayenc

SET employee.id =@id,

employee.fname=@fname,

employee.salary=@salary

from employee
where employee.id=@id



where do you get value of id from? Also i didnt understand why you're updating Dxpaync in instead of trigger for employee. Can you explain what you're trying to do here?
Go to Top of Page

joey ng
Starting Member

22 Posts

Posted - 2008-06-26 : 00:27:52
set ANSI_NULLS ON
set QUOTED_IDENTIFIER OFF
GO
ALTER TRIGGER [dbo].[T_U_employee] ON [employeee]
INSTEAD OF UPDATE
AS

DECLARE @id VARCHAR(128)
DECLARE @fname VARCHAR(128)
DECLARE @salary VARCHAR(128)

Update employee

SET employee.id =@id,

employee.fname=@fname,

employee.salary=@salary

from employee
where employee.id=@id

sorry,the dxpayenc is i type wrongly....now i correct it,the value of id is user key in de..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-26 : 01:17:38
quote:
Originally posted by joey ng

set ANSI_NULLS ON
set QUOTED_IDENTIFIER OFF
GO
ALTER TRIGGER [dbo].[T_U_employee] ON [employeee]
INSTEAD OF UPDATE
AS

DECLARE @id VARCHAR(128)
DECLARE @fname VARCHAR(128)
DECLARE @salary VARCHAR(128)

Update employee

SET employee.id =@id,

employee.fname=@fname,

employee.salary=@salary

from employee
where employee.id=@id

sorry,the dxpayenc is i type wrongly....now i correct it,the value of id is user key in de..



where will you be getting values of these variables from?
Go to Top of Page

joey ng
Starting Member

22 Posts

Posted - 2008-06-26 : 01:54:16
all variable is user key in de..
Go to Top of Page
    Next Page

- Advertisement -