Solved

IF THEN Continued

Posted on 2011-02-21
4
262 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:basile
  • 2
4 Comments
 
LVL 40

Expert Comment

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

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 500 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

757 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

20 Experts available now in Live!

Get 1:1 Help Now