Solved

IF THEN Continued

Posted on 2011-02-21
4
272 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

829 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