Solved

IF THEN Continued

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

776 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