I have 开发者_Python百科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.
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
精彩评论