0

I have a table in database called user which has following entries.

Userid FirstName LastName
1          fa      la
2          fb      lb
3          fc      lc

and another table userform

Userformid Userid  Form   Code modifieddate  Isclosed   IsForm 
1             1     ff     cc     somedate     0           0
2             1     gg     rr     somedate     0           0
3             1     bb     bb     somedate     0           0
4             2     ss     aa     somedate     0           0
5             2     qq     sa     somedate     0           0
6             3     ws     xc     somedate     0           0

Now i have to insert new record for every userid in userform table and only Form and Code column should get copied in inserted row from userform table only by latest modifieddate ( something like:- order by modifieddate desc.)

Output should be in userform table :
    Userformid Userid  Form   Code modifieddate  Isclosed   IsForm 
    1             1     ff     cc     somedate     0           0
    2             1     gg     rr     somedate     0           0
    3             1     bb     bb     somedate     0           0
    4             2     ss     aa     somedate     0           0
    5             2     qq     sa     somedate     0           0
    6             3     ws     xc     somedate     0           0
newly added row
    7             1     bb     bb     newdate     0           0
    8             2     qq     sa     newdate     0           0
    9             3     ws     xc     newdate     0           0

There are 6000 record in user table and userform table.

How can we achieve this using bulk insert feature OR using any other technics in sql server 2005.I don't want to use CURSOR.

1 Answer 1

1

Something like this, making use of ROW_NUMBER should do the trick. Try it first without the INSERT to check the results it returns. If that is then what you want, just uncomment the INSERT line and go ahead.

DECLARE @NewDate DATETIME
SET @NewDate = GETDATE()

--INSERT UserForm (UserId, Form, Code, modifieddate, IsClosed, IsForm)
SELECT UserId, Form, Code, @NewDate, IsClosed, IsForm
FROM
(
    SELECT ROW_NUMBER() OVER (PARTITION BY UserId ORDER BY modifieddate DESC) AS RowNo, 
        UserId, Form, Code, IsClosed, IsForm
    FROM UserForm 
) x
WHERE RowNo = 1
Sign up to request clarification or add additional context in comments.

2 Comments

Hi AdaTheDev! Thanks for the tips.Its really worked for me.I got some record where RowNo = 2.Can u explain me what does that mean?.
@Sukhi - glad I could help. What the ROW_NUMBER with the PARTITION clause is doing, is generating an incrementing row number for each record in the resultset partitioned by user id - that means the row number resets to 1 for each distinct user id so if you have 3 records for a given user id, they will be assigned row numbers 1, 2 and 3 (in order of modified date descending).

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.