2016-07-25 26 views
0

我想查看DB2數據庫查詢中的所有列,但是我想在前面放置某個列。如何在DB2查詢中選擇特定的字段和星號?

例如從我的SQL知識,我會然而上面的查詢返回錯誤象下面

SQL State: 42601 
Vendor Code: -104 
Message: [SQL0104] Token * was not valid. Valid tokens: (+ - ? : DAY INF NAN RID ROW RRN CASE CAST CHAR DATE DAYS HASH. Cause . . . . . : A syntax error was detected at token *. 
Token * is not a valid token. A partial list of valid tokens is (+ - ? : DAY INF NAN RID ROW RRN CASE CAST CHAR DATE DAYS HASH. 
This list assumes that the statement is correct up to the token. 
The error may be earlier in the statement, but the syntax of the statement appears to be valid up to this point. Recovery . . . : 
Do one or more of the following and try the request again: -- Verify the SQL statement in the area of the token *. 
Correct the statement. The error could be a missing comma or quotation mark, it could be a misspelled word, or it could be related to the order of clauses. 
-- If the error token is <END-OF-STATEMENT>, correct the SQL statement because it does not end with a valid clause. 

Processing ended because the highlighted statement did not complete successfully 

回答

2

爲此,您可以使用相關的名稱,像這樣:

SELECT Field1, tbl.* FROM YourTable tbl 

顯然,tbl.*返回所有列,所以Field1將兩次結果出現。

+0

謝謝@mustaccio,這工作完美。 –

0

恐怕喜歡寫東西

SELECT Field2, * FROM Table 

,你就必須選擇每列分開。像

select field_you_want_to_appear_first, field2, filed3.. from yourtable 
1

您可以使用下面的視圖;

[email protected]:/home/db2inst1:>db2 "select * from mytable" 

VAL NEW_VAL 
--- ------- 
5 -  
6 -  
A -  

    3 record(s) selected. 

[email protected]:/home/db2inst1:>db2 "CREATE VIEW MYTABLEVW AS SELECT NEW_VAL,VAL FROM MYTABLE" 
DB20000I The SQL command completed successfully. 

[email protected]:/home/db2inst1:>db2 "select * from mytablevw" 

NEW_VAL VAL 
------- --- 
-  5 
-  6 
-  A 

    3 record(s) selected. 
+1

我明白你要去哪裏了。它會工作,但會增加我需要的一些開銷。請參閱@ mustaccio的答案,這對我來說有點簡單。 –

相關問題