Solved

Duplicate row

Posted on 2009-07-15
2
229 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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