简体   繁体   中英

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. 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. When you retrieve data from the table, the data has no particular order, unless you specify 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) .

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." 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. 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.

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.

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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