Solved

Duplicate row

Posted on 2009-07-15
2
191 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
  • 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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now