查看: 1285|回复: 3
|
SQL 复制特定记录(RESOLVED)
[复制链接]
|
|
我有个DB 的TABLE 叫CURRENT_TBL,里面是COLOMN_ID,COLOMN_A,COLOMN_B,COLOMN_C,COLOMN_D,COLOMN_E。
这个TABLE的资料是不一样的,但COLOMN_B,COLOMN_C的资料会有机会出现一样的时候,如:
ID A B C D E
______________________________________________________
001 aaa bbb ccc ddd eee
002 ppp qqq rrr sss ttt
003 mmm nnn ooo eee yyy
004 www bbb ccc yyy iii
005 ttt qqq rrr ooo lll
006 ggg ccc ccc ccc ccc
007 III bbb ccc ddd eee
008 FFF bbb ccc ddd eee
我想要EXECUTE一个SQL 命令,当超过一个COLOMN_B AND COLOMN_C 的VALUE是一样时,我都会将他们INSERT进一个新的TABLE,叫NEW_TBL,像我的SAMPLE,第1,2,4,5,7,8都会被COPY去新的TABLE;
我的SQL; “INSERT INTO NEW_TBL SELECT * in CURRENT_TBL WHERE....不会了。。。
各位大大帮帮忙。。。
P。S 问题解决了,谢谢海星,ASTRAL。SOLUTION在下面。
[ Last edited by mouyanseng on 25-1-2005 at 08:48 PM ] |
|
|
|
|
|
|
|
发表于 25-1-2005 03:21 PM
|
显示全部楼层
Try this.., my coding quite long.., u can modify by urself.., hope can help u !!
Dim ID As String
Dim a As String
Dim b As String
Dim c As String
Dim d As String
Dim e As String
SQL = "select * From Current_TBL where Trim(COLOMN_B) = Trim(COLOMN_C)"
Set rst1 = New ADODB.Recordset
rst1.Open SQL, AdoConStr, adOpenDynamic, adLockOptimistic, adCmdText
Do While Not rst1.EOF
ID = rst1!Colomn_ID
a = rst1!Colomn_a
b = rst1!Colomn_b
c = rst1!Colomn_c
d = rst1!Colomn_d
e = rst1!Colomn_e
SQL = "select * From New_TBL" 'Where Colomn_ID <> ""
Set rst2 = New ADODB.Recordset
rst2.Open SQL, AdoConStr, adOpenDynamic, adLockOptimistic, adCmdText
If Not rst2.EOF Then
With rst2
.AddNew
!Colomn_ID = ID
!Colomn_a = a
!Colomn_b = b
!Colomn_c = c
!Colomn_d = d
!Colomn_e = e
.Update
End With
End If
rst1.MoveNext
Loop |
|
|
|
|
|
|
|
发表于 25-1-2005 03:51 PM
|
显示全部楼层
在sql server上, 这样应该可以work...不过不知道是不是你要的?
INSERT INTO NEW_TBL
SELECT ID, src.A, src.B, src.C, src. D, src.E
FROM CURRENT_TBL src INNER JOIN
(SELECT t1.B, t1.C FROM CURRENT_TBL t1 GROUP BY t1.B, t1.C HAVING (COUNT(*) > 1)) t2
ON src.B = t2.B AND src.C = t2.C
ORDER BY src.ID
[ Last edited by astral on 25-1-2005 at 03:53 PM ] |
|
|
|
|
|
|
|
楼主 |
发表于 25-1-2005 08:46 PM
|
显示全部楼层
海星姐,ASTRAL哥,多谢回复,用了ASTRAL 的SOLUTION,问题解决了!哈!
SQL里的HAVING还蛮好用的叻。。。
我很幸运,每次的问题都有人会。。。可能我笨吧。。。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|