Solved

Duplicate row

Posted on 2009-07-15
2
231 Views
Last Modified: 2012-05-07
How do I duplicate a row where I change only value of 2 columns.
For example:

Table:  Id, M(int), N(int), Q(int), D(DateTime)

In the stored procedure I send @M and the new value of M @Mnewvalue
I want to set DateTime to current time in the new row.

For example:

Table (Before):
Id=1, M=5, N=7, Q=9, 2009-07-10...

@M=5
@Mnewvalue=8

Table(After)
Id=1, M=5, N=7, Q=9, 2009-07-10...
Id=2, M=8, N=7,Q=9, 2009-07-15
0
Comment
Question by:johnkainn
[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
2 Comments
 
LVL 31

Accepted Solution

by:
RiteshShah earned 250 total points
ID: 24858076
something like this will work


create proc ins
@m int,
@newM int
as
update table1 set d=getdate() where m=@m
insert into table1 (m,n,q,d)
select @newM,n,q,d from table1 where m=@m
go
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24858115
I assume that ID is identity and don't need to provide in INSERT statement
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 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