0

我有一个名为的临时表"#Test",其中"T1", "T2", "T3"包含数据列。我有名为“TestTbl”的数据库表,它具有相同的列。我想将数据从#Test表中插入到TestTbl不同的T1列记录中。你知道如何在TestTbl表中插入不同的记录吗?

4

3 回答 3

1
INSERT INTO TestTbl (T1,T2,T3)
SELECT Distinct(T1), T2, T3 FROM #Test

编辑经过进一步解释

INSERT  INTO TestTbl
        ( T1 ,
          T2 ,
          T3
        )
        SELECT  T1 ,
                T2 ,
                T3
        FROM    ( SELECT    T1 ,
                            T2 ,
                            T3 ,
                            Row_Number() OVER ( PARTITION BY T1 ORDER BY T1) AS record
                            -- you need to select the relevant clause here for the order
                            -- do you want first or latest record? 
                  FROM      #Test
                ) tmp
        WHERE   tmp.record = 1 ;
于 2013-05-01T09:23:05.123 回答
1

你可以试试这样......

INSERT INTO TestTbl (T1,T2,T3)  SELECT T1,T2,T3 from
(
Select Row_Number() over(Partition By T1 order By T1) as row,* from #Test
) a
 where a.row=1;
于 2013-05-01T09:32:18.057 回答
0

获取不同的记录

SELECT DISTINCT column_name,column_name
FROM table_name

插入记录

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...)
于 2013-05-01T09:16:29.283 回答