【问题标题】:How to create table from existing 2 table with default value in oracle如何在 oracle 中使用默认值从现有的 2 个表创建表
【发布时间】:2015-11-17 14:14:54
【问题描述】:

我想从现有的 2 个表选择列创建新表,新列包含“99999999”,如虚拟值。

我试过下面的代码

CREATE TABLE NewTbl1
AS 
    SELECT a.col1, a.col2, a.col3, b.col4, b.col5, dummycol= '99999999'
    FROM tbl1 a  tbl2 b 
    WHERE (a.col1 = b.colNum AND a.col2 = b.colnum1) 

但我收到一个错误,即未找到 FROM 关键字。如果我删除 dummcol='99999999',那么它会正确执行。

查询是对还是错?如有错误请指正

【问题讨论】:

  • '99999999' as dummycol

标签: sql oracle oracle11g ddl


【解决方案1】:

使用正确的join 语法并为别名使用正确的语法:

CREATE TABLE NewTbl1 as 
    SELECT a.col1, a.col2, a.col3, b.col4, b.col5, '99999999' as dummycol
    FROM tbl1 a JOIN
         tbl2 b 
         ON a.col1 = b.colNum AND a.col2 = b.colnum1;

【讨论】:

  • 如何将数据类型赋予 dummycol?因为它的默认值是 characture 但我想要数字
  • 你可以使用 cast - cast(99999999 AS number) as dummycol
  • @dwan 。 . .删除单引号 - ' 不确定它是否会以数字或整数结尾。显式转换可用于更具体地指定类型。
  • @Stawros- 查询运行,但是当我要打开它时,给我一个错误,从“DBNULL”到“integer”的转换无效。
【解决方案2】:
CREATE TABLE NewTbl1
AS 
SELECT a.col1, a.col2, a.col3, b.col4, b.col5, '99999999' as dummycol
FROM tbl1 a, tbl2 b 
WHERE (a.col1 = b.colNum AND a.col2 = b.colnum1) 

【讨论】:

    猜你喜欢
    • 2016-11-05
    • 2020-03-12
    • 2019-04-21
    • 2016-05-22
    • 2010-12-16
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 2013-11-12
    相关资源
    最近更新 更多