SQL 如何在oracle中将2列合并(组合)为1列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20268273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge (combine) 2 columns into 1 in oracle?
提问by abekmuratov
I have 3 textfields where user types table name and 2 column names which need to be merged.
我有 3 个文本字段,用户在其中键入需要合并的表名和 2 个列名。
How should I merge (combine) 2 column values into 1?
我应该如何将 2 列值合并(组合)为 1?
I use oracle 11g enterprise
我用oracle 11g企业版
回答by Randy
concatenate?
连接?
select col1 || ' ' || col2 from tablex
回答by Drumbeg
This is a very vague requirement. Concatenate the values maybe?
这是一个非常模糊的要求。连接值可能吗?
insert into sometable( Column1 )
values ( Column1 || Column2 );
If you need to specify the table name to INSERT into, then you will need to use dynamic SQL to achieve this. Would you need to specify the target column name as well? This example assumes you would use PL/SQL, which may not be appropriate in your case.
如果您需要指定要插入的表名,那么您将需要使用动态 SQL 来实现这一点。您是否还需要指定目标列名?此示例假设您将使用 PL/SQL,这可能不适合您的情况。
sql_stmt := 'INSERT INTO '|| specified_table || '(' || merge_column || ') VALUES ( :1 )';
EXECUTE IMMEDIATE sql_stmt USING column1 || column2;
http://docs.oracle.com/cd/B13789_01/appdev.101/b10807/13_elems017.htm
http://docs.oracle.com/cd/B13789_01/appdev.101/b10807/13_elems017.htm
回答by user2979622
You could make another column (an auxiliary column ) and replace the other 2 columns with this one.
您可以制作另一列(辅助列)并用此列替换其他 2 列。