Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

IF THEN Continued

Posted on 2011-02-21
4
Medium Priority
?
280 Views
Last Modified: 2012-08-14
ok, one thing about this, it updates everytime as such...... I just want where it's changed, that's the last bit of this. just need that one more bit of help, to make it work properly.

in other words, only where the OtherReIn info has changed, modifly the lastmodified column....
0
Comment
Question by:Auerelio Vasquez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 34947024
Need more details. May be sample data or your query would be helpful.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 34947039
In your UPDATE statement, you can update lastmodifed with GETDATE(). Something like this.

UPDATE your_table
  SET OtherReIn = 'something', lastmodifed = GETDATE()
 WHERE some_condition

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34947297
Do you mean something like this?

Declare @updatedid TABLE (AccountID int primary key)
--update
UPDATE MyTable
SET ModifiedDate = CASE WHEN MyTable.OtherReInfo = MyView.OtherReInfo THEN ModifiedDate ELSE GETDATE() END,
	OtherReInfo = MyView.OtherReInfo
OUTPUT DELETED.AccountID INTO @updatedid
FROM MyView
WHERE MyTable.AccountID = MyView.AccountID
  AND MyTable.OtherReInfo <> MyView.OtherReInfo  -- Only when it's different
--insert
INSERT INTO MyTable (AccountID, OtherReInfo, CreatedDate)
SELECT AccountID, OtherReInfo, GETDATE()
FROM MyView
WHERE AccountID NOT IN (SELECT AccountID FROM @updateid)

Open in new window

0
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 2000 total points
ID: 34948096
Sorry, I was late to explain the thing in your previous question

1. what is "DELETED"
process for SQL to perform update is something like delete current rows, and insert back the rows with new data
that delete/insert is perform by the sql engine. which DELETED will refer to table contain data before update
and INSERTED will refer to table contain data after update.
so, the code will output list of accountid that effect into @updateid table

2. the SET you modified is a bit incorrect
here is from my given example
SET ModifiedDate = CASE WHEN MyTable.OtherReInfo = MyView.OtherReInfo THEN ModifiedDate ELSE GETDATE() END,

above code will set ModifiedDate with
  - if the data of OtherReInfo is the same, the give the current value back to it, which equal to not change
  - if not (the same), the give the current time to ModifiedDate

3. why not put "AND MyTable.OtherReInfo <> MyView.OtherReInfo" under WHERE clause.
   if we put that, the "AccountID" of data which already exists won't go into @updateid table.
   which will cause the insert of exists AccountID into the table.

anyway, here is some code for you to try, I think you will understand this version more that previous given one.

/* As you refer, you already having temp table 
CREATE TABLE #temp_table ...
INSERT INTO #temp_table SELECT ... FROM mm_prod_codes2

Remark : for #temp_table structure, accountid should define as PRIMARY KEY
for ex.
CREATE TABLE #temp_table (AccountID int PRIMARY KEY, .....)
*/
-- 1. delete data which is the same from #temp_table
DELETE #temp_table
FROM mm_other_info
WHERE #temp_table.AccountID = mm_other_info.AccountID
	AND #temp_table.OtherReIn = mm_other_info.OtherReIn

Declare @UpdatedID TABLE (AccountID int primary key)
-- 2. update data, now we can remove compare condition, since data exists in #temp_table is only data not equal
UPDATE mm_other_info
SET ModifiedDate = GETDATE(),
	OtherReIn = #temp_table.OtherReIn
OUTPUT DELETED.AccountID INTO @UpdatedID
FROM #temp_table
WHERE mm_other_info.AccountID = #temp_table.AccountID

-- 3. delete data which already update
DELETE #temp_table
FROM @UpdatedID U
WHERE #temp_table.AccountID = u.AccountID

-- 4. insert the leftover row into table
INSERT INTO mm_other_info (AccountID, OtherReIn, CreatedDate)
SELECT AccountID, OtherReIn, GETDATE()
FROM #temp_table

/*
DROP TABLE #temp_table
*/

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

618 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