Solved

Update Date get updated in all rows

Posted on 2011-03-24
5
150 Views
Last Modified: 2012-05-31
hi,

This is the store proc am using to update and insert my table (credit goes to AngelIII),i have column in my table called updatedDate,when ever the record is get updated it will leave the time stamp there,but whenever the store proc runs it will update all the UpdatedDate column.

I am trying to update the UpdatedDate column only the record gets updated,some where am missing some thing,please correct me.

Thanks in Advance

ALTER PROC [dbo].[InsertPhone]
( @EmployeeNo int
, @PhoneNumber char(24)
)
as
begin
declare @CONTEXT_INFO varchar(100)
select @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)

UPDATE o 
  SET o.employeeno = c.employeeno 
    , o.PhoneNumber =c.PhoneNumber
,o.DateUpdated=getdate()
    , o.CreatedBy=@CONTEXT_INFO
    , O.UpdatedBy=@CONTEXT_INFOT 
 FROM Phone o 
 JOIN ADUser c 
   ON c.EmployeeNo = o.EmployeeNo 
WHERE c.EmployeeNo =  @EmployeeNo

IF @@ROWCOUNT = 0
BEGIN
  INSERT INTO [Phone]
    ( employeeno
    , PhoneNumber
    , CreatedBy
    , UpdatedBy
    )
   SELECT Ad.employeeno
        , Ad.PhoneNo
        , @CONTEXT_INFO
        , @CONTEXT_INFO 
     FROM ADUser AD 
    WHERE Ad.employeeno=@EmployeeNo

END

Open in new window

0
Comment
Question by:Sha1395
  • 2
  • 2
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35205665
I would guess a trigger on the table (which might be coded incorrectly) ?
because the UPDATE in this proc will only change the column value for the rows based on the WHERE + JOIN clause
0
 
LVL 9

Expert Comment

by:kaminda
ID: 35205766
You query looks ok to me. Its either a data issue you are having in your tables or  a something else is running after this (may be a trigger as angelIII mentioned)
0
 

Author Comment

by:Sha1395
ID: 35205813
Thanks for your comment,is this anyway i can check or debug ?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35205828
I would first check if there is a trigger.
other than that, I would set up a test database with just the basic things to clearly identify when/what is happening
0
 

Author Comment

by:Sha1395
ID: 35205856
I will check the trigger and thanks again.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Union 20 44
T-SQL: How to Eliminate a Row Based on a Value in One Field 5 36
SQL Query resolving a string conversion issue 26 37
Sql query 107 22
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

919 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

12 Experts available now in Live!

Get 1:1 Help Now