0

We use (INSERT INTO) to insert a record in the table which creates more than one record when used again. Is there any way to add a record and alternately replacing the prevoius one without adding any new record. I know this would work:

UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';

But what if there is no condition ie. we don't know the record, we only know the column name. Is there any way to fill only one record and alternately replace the previous record without creating 2nd record?

14
  • 1
    Any UPDATE without a condition will update all rows in the database. You could write a procedure. Commented May 12, 2015 at 20:17
  • if you don't have a condition, how do you know what the previous record is? Commented May 12, 2015 at 20:17
  • ...so remove the WHERE line Commented May 12, 2015 at 20:18
  • 1
    @karan, without a condition, how would you know which row to remove/replace? Commented May 12, 2015 at 20:20
  • 2
    or INSERT ... ON DUPLICATE KEY UPDATE dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html Commented May 12, 2015 at 20:21

2 Answers 2

1

OK... updating if a record exists or creating a record if there are zero records is a pretty simple matter and you have a solution for it. That having been said, I would do something different and keep track of my message of the day by date:

-- This is REALLY BASIC, but, just to give you the idea...
CREATE TABLE [dbo].[MessageOfTheDay](
    [MessageDate] [date] not null,
    [MessageContents] [nvarchar](500) not null,
    UNIQUE (MessageDate)
)

declare @MessageContents nvarchar(500), @MessageDate date
set @MessageContents = 'This is the new MOTD!!!'
set @MessageDate = GETDATE()

-- Every day, create a new record and you can keep track of previous MOTD entries...
insert into MessageOfTheDay(MessageDate, MessageContents)
values (@MessageDate, @MessageContents)

-- Get the message for today
select MessageContents from MessageOfTheDay where MessageDate = @MessageDate

-- If you want, you can now create messages for FUTURE days as well:
set @MessageContents = 'This is tomorrow''s MOTD!!!';
set @MessageDate = dateadd(D, 1,GETDATE())

insert into MessageOfTheDay(MessageDate, MessageContents)
values (@MessageDate, @MessageContents)

-- Get tomorrow's message
select MessageContents from MessageOfTheDay where MessageDate = @MessageDate

-- If you aren't necessarily going to have one per day and want to always just show the most recent entry
select MessageContents from MessageOfTheDay order by MessageDate desc limit 1

Anyway, that's just my $.02. At some point I bet you will want to look over the history of your MOTD and when you do, you will be happy that you have that history. Plus, this more accurately models the data you are trying to represent.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you come on team viewer for a minute? @Dean
Is one of those queries giving you problems?
Yes, I'm unable to understand. Are you on facebook or twitter or something like that where I can contact you?
0

I got my answer and It's working now! I used:

INSERT INTO data (a, b, c)
VALUES
('1','2','3')
ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.