Solved

IF THEN Continued

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

728 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