简体   繁体   English

使用命令sql从另一个表插入表数据

[英]Table data insert from another table with an order sql

In my project I need to insert a table data from another table. 在我的项目中,我需要从另一个表插入一个表数据。 I trying to write this sql code. 我试图编写此sql代码。 But this order by option is not working while inserting the data. 但是,在插入数据时,此按选项排序不起作用。 Here is my code: 这是我的代码:

INSERT INTO StudentInfo_MeritPosition
       ( ID,
         Name,
         MeritPosition,
         SSC,
         HSC,
         First,
         Second,
         Third,
         Fourth,
         Fifth
       )
       SELECT ID,
              Name,
              MeritPosition,
              SSC,
              HSC,
              First,
              Second,
              Third,
              Fourth,
              Fifth
       FROM StudentInfo
       ORDER BY MeritPosition

The above code inserting data into database. 上面的代码将数据插入数据库。 But not in the order format. 但不是订单格式。 I need to know if there any way our for this problem. 我需要知道我们是否可以解决此问题。 Thank you. 谢谢。

SQL tables represent unordered sets. SQL表表示无序集。 When you retrieve data from the table, the data has no particular order, unless you specify order by . 从表中检索数据时,数据没有特定的顺序,除非您通过指定order by So, you can just retrieve the data as: 因此,您可以按以下方式检索数据:

select mp.*
from StudentInfo_MeritPosition mp
order by mp.MeritPosition;

You can make this query more efficient by adding an index on StudentInfo_MeritPosition(MeritPosition) . 您可以通过在StudentInfo_MeritPosition(MeritPosition)上添加索引来提高此查询的效率。

You can use a temp table to order in any way you want. 您可以使用临时表以所需的任何方式进行排序。 In my opinion it's easier to assemble a temp table first, then order those results and select them into the table you're trying to populate in a given order. 在我看来,先组装一个临时表,然后对这些结果进行排序,然后将它们选择到要按给定顺序填充的表中,会更容易。 This way you can translate it to a stored procedure and feed it the parameter of "column name" and "ASC or DESC." 这样,您可以将其转换为存储过程,并向其提供“列名”和“ ASC或DESC”的参数。 The temp table will take a bit longer to work with since you're selecting, ordering, reselecting, and inserting. 由于您正在选择,排序,重新选择和插入,因此临时表的处理时间会更长一些。 However, the end result is much more robust than a 1 time query allowing you to use any column name, and ASC or DESC. 但是,最终结果比1次查询更健壮,它允许您使用任何列名以及ASC或DESC。 Just remember that when you do select the results into your permanent table, you leave out the primary key (typically [P_ID]) column form your select into statement. 只需记住,当您确实将结果选择到永久表中时,就没有了select into语句中的主键(通常为[P_ID])列。

So, to improve on Gordon's answer, you could write something like what follows: 因此,为了改善戈登的答案,您可以编写如下内容:

 DECLARE @fromTbl, @sortCol, @orderByCol VARCHAR(50)
 EXEC('
 select mp.*
 from /* StudentInfo_MeritPosition* / ' + @fromTbl + 'mp
 order by /* mp.MeritPosition */ mp.' + @orderByCol + ' ' + @sortOrder;'

   /* If you wanted to debug it and make sure your parameters are being
   generated correctly, you can use the PRINT function instead of 
   Exec('Your statement above') */

Then, if you turn it into a SP you can pass in the three parameters table, order by column, and sort order (ASC|DESC) and bypass the temp table creation process I mentioned previously. 然后,如果将其转换为SP,则可以传入三个参数表,按列顺序和排序顺序(ASC | DESC),并绕开我前面提到的临时表创建过程。

Try this. 尝试这个。

INSERT 

/*+append*/

INTO StudentInfo_MeritPosition
       ( ID,
         Name,
         MeritPosition,
         SSC,
         HSC,
         First,
         Second,
         Third,
         Fourth,
         Fifth
       )
SELECT *
FROM ( 
      SELECT ID,
             Name,
             MeritPosition,
             SSC,
             HSC,
             First,
             Second,
             Third,
             Fourth,
             Fifth
      FROM StudentInfo
      ORDER BY MeritPosition );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM