Why order by clause maintains column number values instead of column names

why order by clause maintains column number values instead of column names ?

we can use oder by 1,2 as column number
UGNo one said that it can't be used. What's your point?
To OP: It can be written with the column's 'select list positional number' just because for the support of laziness. :)
There's no difference between 'ORDER BY name, address' and 'ORDER BY 1,2'.

Similar Messages

  • From two given tables, how do you fetch the values from two columns using values from one column(get values from col.A if col.A is not null and get values from col.B if col.A is null)?

    From two given tables, how do you fetch the values from two columns using values from one column(get values from col.A if col.A is not null and get values from col.B if col.A is null)?

    Hi,
    Use NVL or COALESCE:
    NVL (col_a, col_b)
    Returns col_a if col_a is not NULL; otherwise, it returns col_b.
    Col_a and col_b must have similar (if not identical) datatypes; for example, if col_a is a DATE, then col_b can be another DATE or it can be a TIMESTAMP, but it can't be a VARCHAR2.
    For more about NVL and COALESCE, see the SQL Language manual: http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions119.htm#sthref1310
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • How to copy one column BLOB value into another column of another database.

    How to copy one column BLOB value into another column of another database.
    BLOB value contains word document.
    I thought of copy the BLOB value into a text file and then update the new column value by the same text in textfile. Will this work?
    Is there any other better way to do this?

    You're welcome
    BLOB fields contains binary data. I don't think you can do this
    Also if I view the BLOB as text. Can I copy it and insert into the new database.
    I think your options are as I said. Datapump or CTAS
    Best Regards

  • How to pass Temp variable value instead of Table name in From clause.

    Hi,
    I have an requirement to pass the Temperorary variable value instead of Table name in FROM Clause in SQL.
    But I am strugglint to pass this variable value.
    E.g., a Varchar2(5) := 'aa';
    Select * from a;
    Here I come to mention a - 'aa'. But the SQL looks for 'a' as Table. But its should look into 'aa' as Table name.
    Kindly guide me.
    Thanks.

    SQL> declare
      a     varchar2 (5) := 'emp';
      v     varchar2 (100);
      cur   sys_refcursor;
    begin
      open cur for 'Select ename from ' || a;
      fetch cur into v;
      while cur%found
      loop
        dbms_output.put_line (v);
        fetch cur into v;
      end loop;
      close cur;
    end;
    JAMES
    SCOTT
    SMITH
    ALLEN
    WARD
    JONES
    MARTIN
    BLAKE
    CLARK
    KING
    TURNER
    ADAMS
    FORD
    MILLER
    PL/SQL procedure successfully completed.

  • How to use "Order by" clause dynamically on LOV values in 10g r2 forms

    Hi ,
    I have following requirement,please guide me.
    1 Create a List Of Values with 2 fields, Code and Description
    2. Do not use order by clause in record Group Query
    3. Attach this LOV to a field in Form
    4. When user invokes the LOV user will see two fields in LOV with header as Code and Description
    5. Now when user clicks on Column Header “Code” then LOV should be sorted on Code
    6. And if User clicks on Column Header “Description” then LOV should be sorted on Description
    Thanks in Advance.

    Kindly post this problem in this forum ->
    [Forms Forum|http://forums.oracle.com/forums/forum.jspa?forumID=82]
    And, close this thread by marked it as answered. ;)
    Regards.
    Satyaki De.

  • Order By Clause with Empty Field values !

    Hello,
    In Oracle , the order by clause does not return the expected query result, if any of the field value in the order by clause has an empty string.
    Oracle treats the empty string as null value and ORDER BY gives a result with the empty string field values listed at last.
    For example :
    test is a sample table containing "name" field.
    Query: "select name from test order by name"
    In SQL Server/Access
    Result (1)
    NAME
    (blank)
    (blank)
    User1
    User2
    User3
    In Oracle
    Result (2)
    NAME
    User1
    User2
    User3
    (blank)
    (blank)
    I know some of the Work arounds for this as listed below :
    1) To use NVL in Order By Clause.
    i.e., the modified query
    "select name from test order by nvl(name,0)"
    gives the result same as Result (1).
    2) To have single blank space in the field value if it is empty.
    I dont want to use either of these two options b'se it would lead to a mass change in my existing code.
    Is there any way i can do a
    collation order settings in the Oracle databases to get the results as in MS SQL/Access.
    Can Any help me out in Solving this?
    Thanks
    Devi Shankar
    null

    Bharath,
    I am moving this question to the SQL forum.
    Regards,
    Geoff

  • Order by clause  based on parameter value

    hi,
    i am using reports 6i with 10 g db
    I have to create a report sorted in order of four fields. there is a parameter for selecting which field should come first in order by clause.
    ie the user has provision to view the report in selected sorted order
    suppose the four fields are emp_id, name, dept_id, desig .
    if the user select dept_id , then the order by clause should be in order of dept_id,emp_id,name,desig
    Please help to solve this scenario.
    Thanks

    Hi Rinz,
    Use the query as follows :
    select column_name1, column_name1, ....
    from  table_name
    &order_by_clauseNow in the AFTER-PARAMETER-FORM trigger set the order by clause as below depending upon requirements :
    if :sort_column = dept_id then
      :order_by_clause := 'order by dept_id,emp_id,name,desig';
    else
      :order_by_clause := 'order by emp_id, dept_id, name,desig';
    end if;
    Here sort_column is User parameter which will be selected by user, provide list of Values for it.
    Hope this helps
    Best Regards
    Arif Khadas

  • How do I know the Column number after shifting table columns?

    I create a table with column name "a", "b", "c"
    Then I shift those column around..
    Is there a way Java can tell me the column number of heading "b"? (For example)
    Thanks!

    As mentioned above you generally don't need to worry about columns being moved but, from the JTable API, this may be what you are looking for:
    int convertColumnIndexToModel(int viewColumnIndex)
    int convertColumnIndexToView(int modelColumnIndex)

  • Search one column for value in another column

    Hello,
    I am trying to take the whole value from one column, and see if that string appears anywhere in another colmn. So if one row in one column has the value "cat", I want to see if that string appears anywhere in any row in another column: so a row with the value "The cat sat on the mat" would return true.
    I have tried:
    select term from pic_lib_keywords, csv
    where contains (csv.nhm_field_asset_description,pic_lib_keywords.term) >0;
    But I get the error:
    ORA-20000: Oracle Text error:
    DRG-50901: text query parser syntax error on line 1, column 32
    20000. 00000 - "%s"
    *Cause:    The stored procedure 'raise_application_error'
    was called which causes this error to be generated.
    *Action:   Correct the problem as described in the error message or contact
    the application administrator or DBA for more information.
    Can you use 2 columns in this way, or does the search term have to be a literal? Any suggestions would be appreciated.
    Thank you.

    It works for me, as shown below.
    SCOTT@orcl_11gR2> select * from v$version
      2  /
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
    PL/SQL Release 11.2.0.1.0 - Production
    CORE     11.2.0.1.0     Production
    TNS for 64-bit Windows: Version 11.2.0.1.0 - Production
    NLSRTL Version 11.2.0.1.0 - Production
    5 rows selected.
    SCOTT@orcl_11gR2> create table csv
      2    (nhm_field_asset_description  clob)
      3  /
    Table created.
    SCOTT@orcl_11gR2> insert all
      2  into csv values ('The cat sat on the mat.')
      3  into csv values ('The dog sat on the mat.')
      4  select * from dual
      5  /
    2 rows created.
    SCOTT@orcl_11gR2> create table pic_lib_keywords
      2    (term  varchar2(30))
      3  /
    Table created.
    SCOTT@orcl_11gR2> insert all
      2  into pic_lib_keywords values ('cat')
      3  into pic_lib_keywords values ('mouse')
      4  select * from dual
      5  /
    2 rows created.
    SCOTT@orcl_11gR2> create index test_idx
      2  on csv (nhm_field_asset_description)
      3  indextype is ctxsys.context
      4  /
    Index created.
    SCOTT@orcl_11gR2> select term
      2  from   pic_lib_keywords, csv
      3  where  contains
      4             (csv.nhm_field_asset_description,
      5              pic_lib_keywords.term) >0
      6  /
    TERM
    cat
    1 row selected.
    SCOTT@orcl_11gR2>

  • Filling a column with value from other columns

    Hello all!
    Suppose i have the following table:
    TableA(col_one int, col_two int, result int);I want to insert value to col_one and col_two, and result will be filled with the multiplication product of col_one and col_two.
    ok firstly i know this is very redundant way of storing data and generally should not be used, but one case when this might come in handy is when we want to store a total payment amount of product price and quantity. this can be useful since product price can change overtime and we need to record the data as the transaction happens to avoid change in total payment over time (as the price drops or rises).
    now with the problem:
    I tried this in mysql and it works:
    INSERT INTO TableA (col_one, col_two, result) VALUES (30,  20, col_one * col_two);but not in oracle, which output the following error:
    >
    ora 00984 column not allowed here
    any idea on how to do it in oracle?
    thank you very much :)
    ps, thank you for the fast replies on my previous topic.

    If the insert is done inside PL/SQL you can return the primary key for the row (assuming the table has a primary key?!?) and then update based on that key, but this still introduces two statements where a single statement can suffice.
    Why are you thinking so complicated? ;-)
    If it is done in PL/SQL the you can do this:
    create procedure addRow(p_value1 number,p_value2 number) is
    p_result number;
    begin
    p_result:=p_value1*p_value2;
    insert into table1 (col_one,col_two,result) values(p_value1,p_value2,p_result);
    commit;
    end;If you want to do this automatically when someone inserts a row (without providing the result) you have to use an insert trigger.
    Dim

  • Display multiple values in a column in ALV

    I need to display plant order number from AFPO and PLAF table in reference to Goods Recepient(WEMPF) field.
    now in final internal table I assign AFPO--> Plant number value to PLNUM column of ALV.
    how do I add value to same column based on WEMPF common value for both table and append value to PLNUM column below value fetched from AFPO table.
    code for getting value from PLAF table.
    SELECT WEMPF PLNUM
    FROM PLAF
    INTO TABLE IST_PLAF
    FOR ALL ENTRIES IN IST_AFPO
    WHERE PLNNO = IST_AFPO-WEMPF
    AND  ......
    Point is for some WEMPF field entried their will be number of entries in AFPO and PLAF table (in PLNUM column)  so how I do displaying entries from both tables in single row??

    Hi Ankitkumar Dineshkumar Gautam,
    SAP would say: "This is a consulting issue". I would say: What's your problem? Look at some samle programs.
    Regards,
    Clemens

  • Database Settings for order By Clause

    Hi,
    In oracle if we do a "order by" in SQL statement for field values whose values are empty, then they would be listed at the last if we dont specify any NVL() for it.
    Is there any other option by which the funcionality of Order by clause with Empty field values can be changed.
    Is there any settings that can be made in the existing database , so that the Order by clause does not list the empty field values at the bottom.
    Thanx in advance.
    Bharath

    you can do wonders with decode and nvl
    for instance:
    order by decode(nullable_field, null, 1, 2)would make all your null fields come first, whereas
    order by nvl(nullable_field, 'M')would make them appear in the middle of an alphabetical list.
    Basically NULLs are not something. You have to give the ORDER BY something to replace the null.
    null

  • Long run time with ORDER by clause

    Hi,
    I am having a query which is executing fine(in 2 mins) but when i am using order by clause in it, its taking around 13 mins.
    Can anyone suggest what could be th reason and how to execute the same query are get the ordered record?
    thanks.

    Sorry for not providing complet details.
    Database version id 10g.
    Below is the execution plan when using with order by clause.
    Execution Plan
    | Id  | Operation                              | Name              | Rows  | Bytes | Cost  |
    |   0 | SELECT STATEMENT                       |                   |     1 |   118 |  1538 |
    |   1 |  FILTER                                |                   |       |       |       |
    |   2 |   HASH JOIN                            |                   | 16657 |  1089K|    56 |
    |   3 |    TABLE ACCESS FULL                   | FILETYP           |     6 |    60 |     3 |
    |   4 |    TABLE ACCESS FULL                   | FILETYPSOURCEID   | 16657 |   927K|    52 |
    |   5 |   NESTED LOOPS                         |                   |     1 |    35 |     3 |
    |   6 |    TABLE ACCESS BY INDEX ROWID         | FILEWAREHOUSE     |     1 |    25 |     2 |
    |   7 |     INDEX RANGE SCAN                   | FILEWAREHOUSE_DX2 |     1 |       |     2 |
    |   8 |    TABLE ACCESS BY INDEX ROWID         | EXTFILE           |     1 |    10 |     1 |
    |   9 |     INDEX UNIQUE SCAN                  | PK_EXTFILE        |     1 |       |     1 |
    |  10 |  SORT ORDER BY                         |                   |     1 |   118 |  1538 |
    |  11 |   NESTED LOOPS                         |                   |     1 |   118 |  1534 |
    |  12 |    NESTED LOOPS OUTER                  |                   |     1 |   100 |  1533 |
    |  13 |     NESTED LOOPS OUTER                 |                   |     1 |    96 |  1532 |
    |  14 |      NESTED LOOPS                      |                   |     1 |    88 |  1531 |
    |  15 |       HASH JOIN                        |                   |     5 |   360 |  1524 |
    |  16 |        TABLE ACCESS BY INDEX ROWID     | RTXN              |     1 |    22 |     1 |
    |  17 |         NESTED LOOPS                   |                   |   117 |  5148 |  1518 |
    |  18 |          VIEW                          | VW_SQ_1           | 20869 |   448K|  1441 |
    |  19 |           HASH GROUP BY                |                   | 20869 |   427K|  1441 |
    |  20 |            FILTER                      |                   |       |       |       |
    |  21 |             TABLE ACCESS BY INDEX ROWID| RTXNSTATHIST      | 20869 |   427K|  1304 |
    |  22 |              INDEX RANGE SCAN          | RTXNSTATHIST_DX2  | 20869 |       |    29 |
    |  23 |          INDEX RANGE SCAN              | PK_RTXN           |     1 |       |     1 |
    |  24 |           NESTED LOOPS                 |                   |     1 |    24 |     3 |
    |  25 |            TABLE ACCESS BY INDEX ROWID | FILEWAREHOUSE     |     1 |    14 |     2 |
    |  26 |             INDEX RANGE SCAN           | FILEWAREHOUSE_DX2 |     1 |       |     2 |
    |  27 |            TABLE ACCESS BY INDEX ROWID | EXTFILE           |     1 |    10 |     1 |
    |  28 |             INDEX UNIQUE SCAN          | PK_EXTFILE        |     1 |       |     1 |
    |  29 |        TABLE ACCESS FULL               | RTXNTYP           |     1 |    28 |     5 |
    |  30 |       TABLE ACCESS BY INDEX ROWID      | RTXNSTATHIST      |     1 |    16 |     2 |
    |  31 |        INDEX RANGE SCAN                | PK_RTXNSTATHIST   |     1 |       |     1 |
    |  32 |      TABLE ACCESS BY INDEX ROWID       | NTWKNODE          |     1 |     8 |     1 |
    |  33 |       INDEX UNIQUE SCAN                | PK_NTWKNODE       |     1 |       |     1 |
    |  34 |     INDEX UNIQUE SCAN                  | PK_ORG            |     1 |     4 |     1 |
    |  35 |    TABLE ACCESS BY INDEX ROWID         | ACCT              |     1 |    18 |     1 |
    |  36 |     INDEX UNIQUE SCAN                  | PK_ACCT           |     1 |       |     1 |
    Below is the execution plan when running without ORDER BY clause...
    Execution Plan
    | Id  | Operation                             | Name              | Rows  | Bytes | Cost  |
    |   0 | SELECT STATEMENT                      |                   |     1 |   135 |  1537 |
    |   1 |  FILTER                               |                   |       |       |       |
    |   2 |   HASH JOIN                           |                   | 16657 |  1089K|    56 |
    |   3 |    TABLE ACCESS FULL                  | FILETYP           |     6 |    60 |     3 |
    |   4 |    TABLE ACCESS FULL                  | FILETYPSOURCEID   | 16657 |   927K|    52 |
    |   5 |   NESTED LOOPS                        |                   |     1 |    35 |     3 |
    |   6 |    TABLE ACCESS BY INDEX ROWID        | FILEWAREHOUSE     |     1 |    25 |     2 |
    |   7 |     INDEX RANGE SCAN                  | FILEWAREHOUSE_DX2 |     1 |       |     2 |
    |   8 |    TABLE ACCESS BY INDEX ROWID        | EXTFILE           |     1 |    10 |     1 |
    |   9 |     INDEX UNIQUE SCAN                 | PK_EXTFILE        |     1 |       |     1 |
    |  10 |  NESTED LOOPS                         |                   |     1 |   135 |  1534 |
    |  11 |   NESTED LOOPS OUTER                  |                   |     1 |   117 |  1533 |
    |  12 |    NESTED LOOPS OUTER                 |                   |     1 |   113 |  1532 |
    |  13 |     NESTED LOOPS                      |                   |     1 |   105 |  1531 |
    |  14 |      HASH JOIN                        |                   |     5 |   445 |  1524 |
    |  15 |       TABLE ACCESS FULL               | RTXNTYP           |     1 |    28 |     5 |
    |  16 |       TABLE ACCESS BY INDEX ROWID     | RTXN              |     1 |    22 |     1 |
    |  17 |        NESTED LOOPS                   |                   |   117 |  7137 |  1518 |
    |  18 |         VIEW                          | VW_SQ_1           | 20869 |   794K|  1441 |
    |  19 |          HASH GROUP BY                |                   | 20869 |   427K|  1441 |
    |  20 |           FILTER                      |                   |       |       |       |
    |  21 |            TABLE ACCESS BY INDEX ROWID| RTXNSTATHIST      | 20869 |   427K|  1304 |
    |  22 |             INDEX RANGE SCAN          | RTXNSTATHIST_DX2  | 20869 |       |    29 |
    |  23 |         INDEX RANGE SCAN              | PK_RTXN           |     1 |       |     1 |
    |  24 |          NESTED LOOPS                 |                   |     1 |    24 |     3 |
    |  25 |           TABLE ACCESS BY INDEX ROWID | FILEWAREHOUSE     |     1 |    14 |     2 |
    |  26 |            INDEX RANGE SCAN           | FILEWAREHOUSE_DX2 |     1 |       |     2 |
    |  27 |           TABLE ACCESS BY INDEX ROWID | EXTFILE           |     1 |    10 |     1 |
    |  28 |            INDEX UNIQUE SCAN          | PK_EXTFILE        |     1 |       |     1 |
    |  29 |      TABLE ACCESS BY INDEX ROWID      | RTXNSTATHIST      |     1 |    16 |     2 |
    |  30 |       INDEX RANGE SCAN                | PK_RTXNSTATHIST   |     1 |       |     1 |
    |  31 |     TABLE ACCESS BY INDEX ROWID       | NTWKNODE          |     1 |     8 |     1 |
    |  32 |      INDEX UNIQUE SCAN                | PK_NTWKNODE       |     1 |       |     1 |
    |  33 |    INDEX UNIQUE SCAN                  | PK_ORG            |     1 |     4 |     1 |
    |  34 |   TABLE ACCESS BY INDEX ROWID         | ACCT              |     1 |    18 |     1 |
    |  35 |    INDEX UNIQUE SCAN                  | PK_ACCT           |     1 |       |     1 |
    -------------------------------------------------------------------------------------------Edited by: user10754555 on Feb 5, 2010 6:02 PM

  • There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

    I have been looking at my code for hours now and cant figure out what is wrong. The error has kept popping up even when i thought i had fixed everything. maybe a second set of eyes could help me out here. If you could help me out it would be much appreciated.
    USE [ConorsSetKits]
    GO
    CREATE TABLE Customers (
    CustomerID Int NOT NULL IDENTITY(1000,1),
    LastName NChar(30) NOT NULL,
    FirstName NChar(30) NOT NULL,
    Address NChar(50) NOT NULL,
    City NChar(30) NOT NULL,
    State NChar(2) NOT NULL,
    Zip Numeric(5) NOT NULL,
    Email NVarChar(50) NOT NULL,
    DateOfBirth Numeric (4) NOT NULL,
    CONSTRAINT CustomersPK PRIMARY KEY(CustomerID),
    CONSTRAINT ValidZip
    CHECK ( [Zip] LIKE '[0-9][0-9][0-9][0-9][0-9]' ),
    CONSTRAINT ValidState
    CHECK( LEN([State]) = 2),
    CONSTRAINT DateOfBirth
    CHECK ([DateOfBirth] BETWEEN '1920-01-01' AND getdate() - 5844));
    CREATE TABLE Sets (
    SetID Int NOT NULL IDENTITY(1000,1) Primary Key,
    SetName NChar(20) NOT NULL,
    SetType NChar (20) NOT NULL,
    Price Numeric (20) NOT NULL,
    Quantity Numeric (50) NOT NULL,
    CONSTRAINT SetTypeCheck
    CHECK (SetType IN ('Planes','Tanks','Robots','Cars','Helicopters','Boats','Trains','Motorcycles','Jets')),
    CONSTRAINT ValidQuantity 
    CHECK (Quantity >= 0)
    CREATE TABLE Orders (
    OrderID Int NOT NULL IDENTITY(1000,1),
    CustomerID Int NOT NULL,
    OrderDate Date NOT NULL,
    CONSTRAINT CAIntPK PRIMARY KEY(OrderID, CustomerID),
    SET IDENTITY_INSERT dbo.CUSTOMER OFF
    SET IDENTITY_INSERT dbo.Sets OFF
    SET IDENTITY_INSERT dbo.Orders OFF
    SET IDENTITY_INSERT dbo.CUSTOMER ON
    INSERT INTO Customers
    (CustomerID, LastName, FirstName, Address, City, State, Zip,
    Email, DateOfBirth)
    VALUES (
    1000, 'Janes', 'Jeffrey', '123 W. Elm St', 'Renton', 'WA', '98055',
    '[email protected]',1985);
    INSERT INTO Customers
    (CustomerID, LastName, FirstName, Address, City, State, Zip,
    Email, DateOfBirth)
    VALUES (
    1001, 'Smith', 'David', '813 Tumbleweed Lane', 'Loveland', 'CO', '81201', 
    '[email protected]',1992);
    INSERT INTO Customers
    (CustomerID, LastName, FirstName, Address, City, State, Zip,
    Email, DateOfBirth)
    VALUES (
    1015, 'Twilight', 'Tiffany', '88 1st Avenue', 'Langley', 'WA', '98260',
    '[email protected]',1972);
    INSERT INTO Customers
    (CustomerID, LastName, FirstName, Address, City, State, Zip,
    Email, DateOfBirth)
    VALUES (
    1033, 'Smathers', 'Fred', '10899 88th Ave', 'Bainbridge Island', 'WA', '98110',
    '[email protected]',1980);
    INSERT INTO Customers
    (CustomerID, LastName, FirstName, Address, City, State, Zip,
    Email, DateOfBirth)
    VALUES (
    1034, 'Frederickson', 'Mary Beth', '25 South Lafayette', 'Denver', 'CO', '80201',
    '[email protected]',1970);
    INSERT INTO Customers
    (CustomerID, LastName, FirstName, Address, City, State, Zip,
    Email, DateOfBirth)
    VALUES (
    1036, 'Warning', 'Selma', '205 Burnaby', 'Vancouver', 'BC', '80201',
    '[email protected]',1981);
    INSERT INTO Customers
    (CustomerID, LastName, FirstName, Address, City, State, Zip,
    Email, DateOfBirth)
    VALUES (
    1037, 'Wu', 'Susan', '105 Locust Ave', 'Atlanta', 'GA', '30322',
    '404', '653-3465', '[email protected]',1971);
    INSERT INTO Customers
    (CustomerID, LastName, FirstName, Address, City, State, Zip,
    Email, DateOfBirth)
    VALUES (
    1040, 'Gray', 'Donald','55 Bodega Ave', 'Bodega Bay', 'CA', '94923',
    '[email protected]',1985);
    INSERT INTO Customers
    (CustomerID, LastName, FirstName, Address, City, State, Zip,
    Email, DateOfBirth)
    VALUES (
    1041, 'Johnson', 'Lynda', '117 C Street', 'Washington', 'DC', '20003',
    '[email protected]',1969);
    INSERT INTO Customers
    (CustomerID, LastName, FirstName, Address, City, State, Zip,
    Email, DateOfBirth)
    VALUES (
    1051, 'Wilkens', 'Chris', '87 Highland Drive', 'Olympia', 'WA', '98508',
    '[email protected]',1994); 
    SET IDENTITY_INSERT dbo.CUSTOMER OFF
    SET IDENTITY_INSERT dbo.Sets ON
    INSERT INTO Sets
    (SetID, SetName, SetType, Price, Quanity)
    VALUES (
    1000, 'MysterySet1','Planes',$29.99,10);
    INSERT INTO Sets
    (SetID, SetName, SetType, Price, Quanity)
    VALUES (
    1001, 'MysterySet2','Planes',$19.99,10);
    INSERT INTO Sets
    (SetID, SetName, SetType, Price, Quanity)
    VALUES (
    1002, 'MysterySet4','Tanks',$39.99,10);
    INSERT INTO Sets
    (SetID, SetName, SetType, Price, Quanity)
    VALUES (
    1003, 'MysterySet3','Robots',$19.99,10);
    INSERT INTO Sets
    (SetID, SetName, SetType, Price, Quanity)
    VALUES (
    1004, 'MysterySet5','Cars',$29.99,10);
    INSERT INTO Sets
    (SetID, SetName, SetType, Price, Quanity)
    VALUES (
    1005, 'MysterySet6','Boats',$29.99,10);
    INSERT INTO Sets
    (SetID, SetName, SetType, Price, Quanity)
    VALUES (
    1006, 'MysterySet7','Trains',$39.99,10);
    INSERT INTO Sets
    (SetID, SetName, SetType, Price, Quanity)
    VALUES (
    1007, 'MysterySet8','Motorcycles',$9.99,10);
    INSERT INTO Sets
    (SetID, SetName, SetType, Price, Quanity)
    VALUES (
    1008, 'MysterySet9','Helicopters',$29.99,10);
    INSERT INTO Sets
    (SetID, SetName, SetType, Price, Quanity)
    VALUES (
    1009, 'MysterySet10','Jets',$29.99,10);
    SET IDENTITY_INSERT dbo.Sets OFF
    SET IDENTITY_INSERT dbo.Orders ON
    INSERT INTO Orders 
    (OrderID, OrderDate)
    VALUES 
    (1000, '2012-12-12');
    INSERT INTO Orders 
    (OrderID, OrderDate)
    VALUES 
    (1000, '2013-12-30');
    INSERT INTO Orders 
    (OrderID, OrderDate)
    VALUES 
    (1005, '2013-08-30');
    INSERT INTO Orders 
    (OrderID, OrderDate)
    VALUES 
    (1004, '2013-12-30');
    INSERT INTO Orders 
    (OrderID, OrderDate)
    VALUES 
    (1004, '2013-08-31');
    INSERT INTO Orders 
    (OrderID, OrderDate)
    VALUES 
    (1004, '2014-03-25');
    INSERT INTO Orders 
    (OrderID, OrderDate)
    VALUES 
    (1002, '2012-11-14');
    INSERT INTO Orders 
    (OrderID, OrderDate)
    VALUES 
    (1001, '2012-11-14');
    INSERT INTO Orders 
    (OrderID, OrderDate)
    VALUES 
    (1001, '2013-01-05');
    INSERT INTO Orders 
    (OrderID, OrderDate)
    VALUES 
    (1006, '2012-06-22');
    SET IDENTITY_INSERT dbo.Orders OFF

    Price Numeric (20) NOT NULL,
    I do not think you understand this datatype?  Go look at the values you are attempting to insert into this column.  For example, "$29.99".  After the insert, what value is actually stored in that column? 
    DateOfBirth Numeric (4) NOT NULL,
    No. Just No.  Either change the column name to faithfully represent what values you intend to store (and apply the appropriate constraints) or change the datatype and the values that you intend to store to match the column name.  And the constraint
    that you do have:
     CONSTRAINT DateOfBirth
     CHECK ([DateOfBirth] BETWEEN '1920-01-01' AND getdate() - 5844));
    is both logically suspect and syntactically wrong. First, the upper boundary will be computed as 1999-04-29 15:50:21.670.  But, of course, your column is a whole number of no more than 4 digits.  The same issue applies to your lower boundary. 
    A simple select statement will show you those errors
    select cast('1920-01-01' as numeric(4)), getdate() - 5844, cast(getdate() - 5844 as numeric(4));
    An insert statement fails with the same error.  Because, of course, your boundary values must actually be converted to numeric(4) in order to compare them against the value contained in the column.   Generally, you can convert from
    string to numeric - IF the content of the string actually contains a value that is numeric and can be converted into the specific type needed.  Not so in this case.  Is this 16 year period (5844 days) significant for some reason?  You should
    document it here in your DDL with a comment, because it is unlikely to be documented (and kept current) anywhere else. 
    And lastly, your script has:
    SET IDENTITY_INSERT dbo.CUSTOMER OFF
    That is not the name of your table, and therefore that statement also fails.  The table name you used is plural. 

  • Why I cannot use RowID in where clause but can use it in order by clause

    I am on SQL Server 2008.
    1. If I use
    SELECT (ROW_NUMBER()  over
    (order by ImportId, ScenarioId, SiteID, AssetID, LocalSKUID, WEEKID, MonthID)) RowID, * 
      FROM [JnJ_Version1].[dbo].[td_Production_Week]
      order by RowID
    Statement works
    But
    2. If I use
    SELECT (ROW_NUMBER()  over
    (order by ImportId, ScenarioId, SiteID, AssetID, LocalSKUID, WEEKID, MonthID)) RowID, * 
      FROM [JnJ_Version1].[dbo].[td_Production_Week]
      where  RowID > 10000
    I get error, RowID is an invalid column Name why? How to correct query 2.

    This is due to the logical evaluation order of a SELECT statement. Logically, a SELECT statement is computed in the order:
    FROM (which includes JOIN)
    WHERE
    GROUP BY
    HAVING
    SELECT
    ORDER BY
    OFFSET
    Thus, you can use what is defined in the SELECT list in the ORDER BY clause, but not in the WHERE clause.
    In the case of row_number(), this has immediate repurcussions. row_number() is computed from the rows as they arrive the SELECT clause, and if you then you would filter on the value in the WHERE clause you would be going round in circles.
    To do what you are looking for, you use a nested table, for instance with a CTE:
    WITH numbering AS (
       SELECT (ROW_NUMBER()  over
    (order by ImportId, ScenarioId, SiteID, AssetID, LocalSKUID, WEEKID, MonthID)) RowID, * 
      FROM [JnJ_Version1].[dbo].[td_Production_Week]
    SELECT *
    FROM   numbering
    WHERE  RowID > 10000
    Erland Sommarskog, SQL Server MVP, [email protected]

Maybe you are looking for