SQL Insert from "Select Query" Not Work when use Order By

Hai every body...
I have a problem with T-SQL. I use SQL Server 2012 Express with SP2 on Windows 7 32-bit SP1.
This is the problem
-- first, create table
create table dtoth.tableA (
id_data TINYINT PRIMARY KEY,
kd_data VARCHAR(20),
nm_data VARCHAR(200)
-- then, insert values
INSERT INTO dtoth.tableA VALUES (0,'100.001','KAS');
INSERT INTO dtoth.tableA VALUES (1,'110.001','BANK');
INSERT INTO dtoth.tableA VALUES (2,'120.001','PIUTANG DAGANG');
INSERT INTO dtoth.tableA VALUES (3,'121.001','PIUTANG GIRO');
INSERT INTO dtoth.tableA VALUES (4,'130.001','PERSEDIAAN BARANG DAGANGAN');
-- then, i create a temporary table
create table dtoth.temp_tableA (
kd_data VARCHAR(20),
nm_data VARCHAR(200)
-- then, i create a store procedure to call data from temporary table
CREATE procedure dtoth.report
AS
BEGIN
DELETE FROM dtoth.temp_tableA;
INSERT INTO dtoth.temp_tableA SELECT kd_data, nm_data FROM dtoth.tableA ORDER BY kd_data desc;
SELECT * FROM dtoth.temp_tableA;
END
GO
When i execute the the store procedure with
EXEC dtoth.report;
the result is not accurate like this (kd_data not sorted desc):
I want the column "kd_data" sort descending because i use ORDER BY kd_data DESC in insert statement on the store procedure.
By the way, if i execute code like :
SELECT kd_data, nm_data FROM dtoth.tableA ORDER BY kd_data desc;
the result is correct like this
So, what solution for my code in the store procedure ?
Thanks.

to get the data sorted, you should order by in your select in the stored procedure
sorting while inserting does not guarenatee order..
remove the order by in insert statement and put that in the select statement
so, your procedure should be 
-- then, i create a store procedure to call data from temporary table
CREATE procedure dtoth.report
AS
BEGIN
DELETE FROM dtoth.temp_tableA;
INSERT INTO dtoth.temp_tableA SELECT kd_data, nm_data FROM dtoth.tableA;
SELECT * FROM dtoth.temp_tableA ORDER BY kd_data desc;
END
GO
Hope it Helps!!

Similar Messages

  • Photoshop CC. Colour selection not working when using alt and brush.

    Hi, Photoshop CC. Windows 8.1 64 bit. Colour selection not working when using alt and brush. I have tried rebooting and other things like closing swatch panel etc.
    The alt key is otherwise OK.  It's not the recent upgrade from Win 8 to 8.1 (5 days ago) because the colour selection facility was working yesterday. Any ideas.

    I'd say things sound desperate enough to try resetting Preferences
    Reset Preferences
    Windows — Hold down Shift Ctrl Alt immediately after starting Photoshop
    Mac — Hold down Shift Cmd Opt immediately after starting Photoshop
    If that fails, we need to know your operating system?
    Do you have a full CC subscription, or by apps do you mean Photoshop, Bridge and Lightroom?
    Another other information that might pertain ?

  • How do I set up a mail group on mac mail?  All the advise on line seems to refer to 'address book' and I only have 'contacts'.  The guidance does not work when using 'contacts' - can anyone help me?

    How do I set up a mail group on mac mail?  All the advise on line seems to refer to 'address book' and I only have 'contacts'.  The guidance does not work when using 'contacts' - can anyone help me?

    Create a group and send mail
    http://www.dummies.com/how-to/content/how-to-create-a-basic-contact-group-in-mac -os-x-li.html
    http://www.macworld.com/article/1165582/how_to_email_groups_with_mail.html
    Best.

  • Siri does not work when used for calling a contact function

    Siri does not work when used for calling a contact function

    Very strange, try to turn off/on your iPhone. Or make a reset (home+power button)...
    If it continues try to restore it.

  • HT204168 touch screen function not working when using facetime

    Why is my iPad touch screen function not working when using FaceTime?

    Hi megascones,
    I apologize, I'm a bit unclear on the exact nature or scope of the issue you are describing. If you are having issues with the touch screen on your iPad, you may find the troubleshooting steps outlined in the following article helpful:
    If the screen on your iPhone, iPad, or iPod touch doesn't respond to touch - Apple Support
    Regards,
    - Brenden

  • Why is my touch screen function not working when using facetime

    Why is my touch screen function not working when using facetime

    Hello megascones,
    After reviewing your post, it sounds like the screen is not responding to touch in one app. I would recommend that you read this article, it may be able to help the issue.
    If the screen on your iPhone, iPad, or iPod touch doesn't respond to touch - Apple Support
    Restart your device. If you can't restart, reset your device.
    Thanks for using Apple Support Communities.
    Have a nice day,
    Mario

  • Lenovo S8-50LC Wlan(WiFi) Does not work when use Sim Cart(3G)

    Hi
    Tablet Lenovo S8-50LC
    Wlan(WiFi) Does not work when use Sim Cart(3G).
    What is the solution for this problem?
    Build Number : TABS8-50LC_S000200_150121_ROW
    SoftWarw Version : TABS8-50LC_150121

    Right, but if you have the SIM in the tablet and you put the tablet in airplane mode, does wifi work then? If so, it may be a bad SIM. Do you have another SIM you can try?

  • Why is my selection tool not working when trying to rotate or scale?

    I cant get my selection tool to work when I'm trying to scale or rotate an image. It only works to select and move.

    emain,
    Maybe View>Show Bounding Box.

  • Why does diadem intellisense not work when using scriptinclude or for global object

    I have a script with a different classes in it (scripted 1).  When I create an object of a class in the script 1 intellisense works.  Intellisense is when you type name of the object created from a class it shows all the possible variables and functions contained in that object.  But when I write another script and use scriptinclude(scripted 1) the intellisense does not work.  I also tried a global object and intesllisense does not work.  Is there a way to get intellisense to work when using scriptinclude?  I am working with version 2012. 

    Here is more information on the issue.
    I have a script with a different classes in it (scripted 1).  When I create an object of a class in the script 1 intellisense works.  Intellisense is when you type name of the object created from a class it shows all the possible variables and functions contained in that object.  But when I write another script and use scriptinclude(scripted 1) the intellisense does not work.  I also tried a global object and intesllisense does not work.  Is there a way to get intellisense to work when using scriptinclude?  I am working with version 2012. 
    'Test.vbs
    class test
        public function test1()
             msgbox("test1")
        end function
        public function test2()
             msgbox("test2")
        end function
    end class
    'now create a object of the class
    dim oTest
    set oTest=new Test
    'now when you type oTest. you get the following functions to choose from
    oTest.test1
            .test2
    Now if you remove the object from Test.vbs and create a main program with scriptinclude(Test.vbs) it will not show the two functions
    'Main.vbs
    Option Explicit  'Forces the explicit declaration of all the variables in a script.
    call scriptinclude("c:\1atmp\Test.vbs")
    dim oTest
    set oTest=new test
    'now when you type oTest. it shows none of the functions in the object oTest
    oTest.

  • Keyboard backlighting not working when using Windows in Boot Camp

    Hi, I'm posting this for a friend with a new Macbook Air. He says that his keyboard backlighting does not work when he is using it for Windows (don't know which, but probably Vista). Any ideas why? Thanks. Gary

    Install all the drivers needed when running boot camp - try for example inserting a DVD - then press the eject key - See if it comes out - If it doesn't install all the drivers - try Control Panel - If not try re-installing your version of Windows.

  • Elements 11 - Brush tool not working when using layer mask

    My brush tool won't work when using layer mask in PS Elements 11. I also have Elements version 10 installed and there it works fine. If I do the exact same proces in version 11 nothing happens.
    Is this a bug or has something changed in the proces?
    Please help.
    Thanks.

      Make sure you have the mask thumbnail selected - it will be highlighted in blue.

  • Microphone not working when using a phone call, Microphone not working when using a phone call

    Microphone is not working when i use my iphone 4, first day when i installed a iOS 7 , was all good, second day this happened. Please help me. When i use a Viber microphone working. I think this is not a hardwear problem.

    Hi Mike
    That was very helpful thank you.
    I did as you suggested and found the "switch" was already turned on green. I swiched it off waited a while then turned it back on and microphone was back in use in viber. I have a sneaky feeling I may have to do this again in the future.  I am not aware that I did anything that led to the microphone going off, but having said that I have only had the phone for a few days so may have done something when checking it out.
    The phone has been a lot of fun after replacing my ancient Nokia with it. That Nokia worked in Morse code!
    Best wishes
    Rob

  • Enter Key Not Working When Using Text Tool

    For some reason, my enter key has stopped working when using the text tool.  Instead of making a soft return to a new line, it simply commits the type. What is going on here?
    I'm using a desktop and CS3.

    there are 2 enter keys in key board
    if u enter main key board enter, sentence will go to next line
    In case u enter the key  available in  rightside of the key board , in the cluster of numericals, the type tool is committed.

  • It is not working when use trigger to check data from other table.

    Please help me with this, I have put a trigger on a table but it can not work as I expect.
    case study: one class has many students, only one of them goes to match.
    The purpose of this trigger is to check when choose a student goes to match, this student must in his class where he belongs to.
    Version of Oracle is 10.2.0.1.0.
    --table:
    DROP TABLE STU;
    DROP TABLE CLASS;
    create table CLASS(
    CID     VARCHAR2(5)   PRIMARY KEY,
    CNAME   VARCHAR2(20)  NOT NULL,
    SCHOSEN VARCHAR2(5));
    create table STU(
    SID     VARCHAR2(5)   PRIMARY KEY,
    SNAME   VARCHAR2(20)  NOT NULL,
    CID     VARCHAR2(5)   NOT NULL REFERENCES CLASS(CID) ON DELETE CASCADE);
    --data:
    --CLASS
    INSERT INTO CLASS(CID,CNAME) VALUES(1,'SUN');
    INSERT INTO CLASS(CID,CNAME) VALUES(2,'MOON');
    INSERT INTO CLASS(CID,CNAME) VALUES(3,'EARTH');
    --STU
    INSERT INTO STU VALUES(1,'JACK',1);
    INSERT INTO STU VALUES(2,'TOM',1);
    INSERT INTO STU VALUES(3,'LILY',2);
    INSERT INTO STU VALUES(4,'DUSTIN',3);
    --TRIGGER
    CREATE OR REPLACE TRIGGER CHECK_SCHOSEN
    BEFORE INSERT OR UPDATE OF SCHOSEN ON CLASS
    FOR EACH ROW WHEN (NEW.SCHOSEN IS NOT NULL)
    DECLARE
    DUMMY INTEGER;
    INVALID_STU EXCEPTION;
    VALID_STU EXCEPTION;
    MUTATING_TABLE EXCEPTION;
    PRAGMA EXCEPTION_INIT(MUTATING_TABLE, -4091);
    CURSOR DUMMY_CURSOR (ST VARCHAR2, CL VARCHAR2) IS
      SELECT SID FROM STU, CLASS
      WHERE STU.SID=ST AND STU.CID=CLASS.CID AND CLASS.CID=CL
        FOR UPDATE OF CLASS.SCHOSEN;
    BEGIN
      OPEN DUMMY_CURSOR(:NEW.SCHOSEN, :NEW.CID);
      FETCH DUMMY_CURSOR INTO DUMMY;
      IF DUMMY_CURSOR%NOTFOUND THEN
        RAISE INVALID_STU;
      ELSE
        RAISE VALID_STU;
      END IF;
      CLOSE DUMMY_CURSOR;
    EXCEPTION
      WHEN INVALID_STU THEN
        CLOSE DUMMY_CURSOR;
        DBMS_OUTPUT.PUT_LINE('PLEASE RE-ENTER CLASS ID AND STUDENT ID AS CLASS OR STUDENT IS NOT VALID.');
      WHEN VALID_STU THEN
        CLOSE DUMMY_CURSOR;
        DBMS_OUTPUT.PUT_LINE('STUDENT CHOOSE SUCCEFULLY!');
      WHEN MUTATING_TABLE THEN
        NULL;
    END;
    /Just copy and paste above and try to run following:
    UPDATE CLASS
    SET SCHOSEN=3
    WHERE CID=1;
    Clearly,you can not choose student which is 3 as match member of class 1. Please help me. Thanks.
    Edited by: 991096 on 01-Mar-2013 02:36
    Edited by: 991096 on 01-Mar-2013 03:03
    Edited by: 991096 on 01-Mar-2013 03:11

    Hi,
    Welcome to the forum!
    Like the others, I can't see a business purpose for this trigger. I assume it's just an experiment to learn how triggers work.
    991096 wrote:
    Please help me with this, I have put a trigger on a table but it can not be triggered.Sorry, I don't understand what you want help with.
    Are you saying the trigger doesn't fire? It does fire; it just raises the mutating table error, so you don't see anything.
    case study: one class has many students, only one of them goes to match.What do you mean when you say "goes to match"?
    --table:
    DROP TABLE STU;
    DROP TABLE CLASS;
    create table CLASS(
    CID VARCHAR2(5) PRIMARY KEY,
    CNAME VARCHAR2(20) NOT NULL,
    SCHOSEN VARCHAR2(5));
    create table STU(
    SID VARCHAR2(5) PRIMARY KEY,
    SNAME VARCHAR2(20) NOT NULL,
    CID VARCHAR2(5) NOT NULL REFERENCES CLASS(CID) ON DELETE CASCADE);
    --data:
    --CLASS
    INSERT INTO CLASS(CID,CNAME) VALUES(1,'SUN');
    INSERT INTO CLASS(CID,CNAME) VALUES(2,'MOON');
    INSERT INTO CLASS(CID,CNAME) VALUES(3,'EARTH');
    --STU
    INSERT INTO STU VALUES(1,'JACK',1);
    INSERT INTO STU VALUES(2,'TOM',1);
    INSERT INTO STU VALUES(3,'LILY',2);
    INSERT INTO STU VALUES(4,'DUSTIN',3);
    --TRIGGER
    CREATE OR REPLACE TRIGGER CHECK_SCHOSEN
    BEFORE INSERT OR UPDATE OF SCHOSEN ON CLASS
    FOR EACH ROW WHEN (NEW.SCHOSEN IS NOT NULL)
    DECLARE
    DUMMY INTEGER;
    INVALID_STU EXCEPTION;
    VALID_STU EXCEPTION;
    MUTATING_TABLE EXCEPTION;
    PRAGMA EXCEPTION_INIT(MUTATING_TABLE, -4091);
    CURSOR DUMMY_CURSOR (ST VARCHAR2, CL VARCHAR2) IS
    SELECT SID FROM STU, CLASS
    WHERE STU.SID=ST AND STU.CID=CLASS.CID AND CLASS.CID=CL
    FOR UPDATE OF CLASS.SCHOSEN;
    BEGIN
    OPEN DUMMY_CURSOR(:NEW.SCHOSEN, :NEW.CID);
    FETCH DUMMY_CURSOR INTO DUMMY;
    IF DUMMY_CURSOR%NOTFOUND THEN
    RAISE INVALID_STU;
    ELSE
    RAISE VALID_STU;
    END IF;
    CLOSE DUMMY_CURSOR;
    EXCEPTION
    WHEN INVALID_STU THEN
    CLOSE DUMMY_CURSOR;
    DBMS_OUTPUT.PUT_LINE('PLEASE RE-ENTER CLASS ID AND STUDENT ID AS CLASS OR STUDENT IS NOT VALID.');
    WHEN VALID_STU THEN
    CLOSE DUMMY_CURSOR;
    DBMS_OUTPUT.PUT_LINE('STUDENT CHOOSE SUCCEFULLY!');
    WHEN MUTATING_TABLE THEN
    NULL;
    END;
    /Thanks for posting the CREATE TABLE, INSERT and CREATE TRIGGER statements; that really helps!
    See the forum FAQ {message:id=9360002} for other helpful tips, such as how to use \ tags to post formatted code.
    Just copy and paste above and try to run following:
    UPDATE CLASS
    SET SCHOSEN=3
    WHERE CID=1;
    Clearly,you can not choose student which is 3 as match member of class 1. The trigger did not be triggered. Please help me. Thanks.You can't do DML, or even query, the class table from a FOR EACH ROW trigger on the same class table.
    Add 3 more calls to put_line to see this:CREATE OR REPLACE TRIGGER CHECK_SCHOSEN
    BEFORE INSERT OR UPDATE OF SCHOSEN ON CLASS
    FOR EACH ROW WHEN (NEW.SCHOSEN IS NOT NULL)
    DECLARE
    DUMMY          INTEGER;
    INVALID_STU      EXCEPTION;
    VALID_STU          EXCEPTION;
    MUTATING_TABLE      EXCEPTION;
    PRAGMA           EXCEPTION_INIT (MUTATING_TABLE, -4091);
    CURSOR DUMMY_CURSOR (ST VARCHAR2, CL VARCHAR2) IS
    SELECT SID
         FROM      STU
         ,     CLASS
         WHERE     STU.SID          = ST
         AND     STU.CID          = CLASS.CID
         AND     CLASS.CID     = CL
         FOR UPDATE OF           CLASS.SCHOSEN;
    BEGIN
    dbms_output.put_line (:NEW.schosen || ' = schosen entering check_schosen');     -- ***** NEW *****
    OPEN DUMMY_CURSOR (:NEW.SCHOSEN, :NEW.CID);
    dbms_output.put_line ('Cursor is open now.');     -- ***** NEW *****
    FETCH DUMMY_CURSOR INTO DUMMY;
    IF DUMMY_CURSOR%NOTFOUND THEN
    RAISE INVALID_STU;
    ELSE
    RAISE VALID_STU;
    END IF;
    CLOSE DUMMY_CURSOR;
    EXCEPTION
    WHEN INVALID_STU THEN
    CLOSE DUMMY_CURSOR;
         DBMS_OUTPUT.PUT_LINE ('PLEASE RE-ENTER CLASS ID AND STUDENT ID AS CLASS OR STUDENT IS NOT VALID.');
    WHEN VALID_STU THEN
         CLOSE DUMMY_CURSOR;
         DBMS_OUTPUT.PUT_LINE ('STUDENT CHOOSE SUCCEFULLY!');
    WHEN MUTATING_TABLE THEN
    DBMS_OUTPUT.PUT_LINE ('MUTATING TABLE');     -- ***** NEW *****
         NULL;
    END;
    Output, when trying to UPDATE class:3 = schosen entering check_schosen
    MUTATING TABLE
    1 row updated.
    Obviously, the trigger fired, since you see the message "3 = schosen entering check_schosen"
    Obviously, the OPEN statement raised an error, since you don't see the message 'Cursor is open now.'
    Obviously, the mutating table error was raised, since you see the message 'MUTATING TABLE'.
    So the trigger fired, and went to the EXCEPTION handler almost immediately.  The EXCEPTION handler printed a message, as instructed, and the trigger ended.  Then the actual UPDATE took place.
    I hope this answers your question.
    If not, what is your question?  It may be very clear to you, but a complete mystery to others.  Ask clear questions, such as "Why did ... happen?  I thought ... would happen, be ... as the PL/SQL manual says at ...".                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Drag and relate from User query not working as expected

    Hi All,
    Could anyone shed some light on why using drag and relate from a user query is not working for me?
    Drag and relate works well from all system generated queries, but not from my user queries.
    For example:
    - System query/report - running a system stock report and dragging the 'Item number' field result to a D&R Sales Order generates a report of all Sales Orders for the selected Item Number. As expected - success.
    - User Query - running a item lookup report from a user generated query and then D&R to Sales order for the selected Item Number does not work. Error = 'No matching records found (ODBC -2028) [message 131-183]
    Any ideas?
    Best regards,
    John

    Hi John,
    I believe Drag and relate only works for system form.  The whole function is hard coded.  It will not work for any queries including system queries.
    Thanks,
    Gordon

Maybe you are looking for

  • Jabber Phone Control When Making Calls

    All, CUCM 9.1 with 9.1 CUPS.  Jabber contacts have the full phone number which seems to be auto-populated.  Where can I go to change the phone number of the contact to their extension instead of the DID? Thanks in advance.  All replies rated.        

  • ICal Mavericks update seeing start time in month view

    Is there any way to, in the Mavericks' version of iCal, get back the option to see the starting time of events in the month view?

  • My Computer Name Keeps Changing

    I system preferences sharing where you go to set your computer name I am noticing that mine says "Rob's iMac (3)" instead of "Rob's iMac".   My network hasn't changed, haven't added or removed anything.   I am using an airport extreme.    One thing I

  • Cisco prime infrastructure 1.3 to 2.0

    Hello Guys I have an NCS (cisco prime) 1.3 in appliance model (IBM server) and we are wondering if we could upgrade it to 2.0 (with out virtualization) The 1.3 covers the 2.0 requirements. The reason for the upgrade is that we are hitting the cscud26

  • Duties in ARE1

    Hi All, Scenario is A-Sold to Party (India) B- Ship to Party (Thailand) Customer Invoice is for A-Sold to Party Excise Invoice is for B-Ship to Party We create customer Invoice in VF01 of Value 771,516. Excise Invoice get automatic created. But the a