Oracle多行插入语句

时间:2012-02-20 16:49:17

标签: sql oracle sql-insert

Oracle不支持这种插入的那种愚蠢,我将如何在Oracle中执行此操作?

INSERT INTO WEBSITE_GATEKEEPER_STATE (PRG_CODE, STATE) VALUES("BPA", "AL"), ("BPA", "AK"), ("BPA", "AS"), ("BPA", "AZ"), ("BPA", "AR"), ("BPA", "AF"), 
 ("BPA", "AA"), ("BPA", "AC"), ("BPA", "AE"), ("BPA", "AM"), ("BPA", "AP"), ("BPA", "CA"), ("BPA", "CO"), ("BPA", "CT"), ("BPA", "DE"), ("BPA", "DC"), 
 ("BPA", "FM"), ("BPA", "FL"), ("BPA", "GA"), ("BPA", "GU"), ("BPA", "HI"), ("BPA", "ID"), ("BPA", "IL"), ("BPA", "IN"), ("BPA", "IA"), ("BPA", "KS"),
 ("BPA", "KY"), ("BPA", "LA"), ("BPA", "ME"), ("BPA", "MH"), ("BPA", "MD"), ("BPA", "MA"), ("BPA", "MI"), ("BPA", "MN"), ("BPA", "MS"), ("BPA", "MO"),
 ("BPA", "MT"), ("BPA", "NE"), ("BPA", "NV"), ("BPA", "NH"), ("BPA", "NJ"), ("BPA", "NM"), ("BPA", "NY"), ("BPA", "NC"), ("BPA", "ND"), ("BPA", "MP"),
 ("BPA", "OH"), ("BPA", "OK"), ("BPA", "OR"), ("BPA", "PW"), ("BPA", "PA"), ("BPA", "PR"), ("BPA", "RI"), ("BPA", "SC"), ("BPA", "SD"), ("BPA", "TN"),
 ("BPA", "TX"), ("BPA", "UT"), ("BPA", "VT"), ("BPA", "VI"), ("BPA", "VA"), ("BPA", "WA"), ("BPA", "WV"), ("BPA", "WI"), ("BPA", "WY");

5 个答案:

答案 0 :(得分:3)

答案 1 :(得分:3)

我认为这取决于一个人对“愚蠢”的定义。这件事是我们偶尔应该做的那种数据加载。因此,使用cut'n'paste生成符合可用语法的脚本并不是一件很大的麻烦。

grep。我在编辑器中使用正则表达式将您的代码转换为可行的PL / SQL代码。看看:

declare
    strs dbms_debug_vc2coll;
begin
     strs := dbms_debug_vc2coll ( 'AL', 'AK', 'AS', 'AZ', 'AR', 'AF', 
         'AA', 'AC', 'AE', 'AM', 'AP', 'CA', 'CO', 'CT', 'DE', 'DC', 
         'FM', 'FL', 'GA', 'GU', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS',
         'KY', 'LA', 'ME', 'MH', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO',
         'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'MP',
         'OH', 'OK', 'OR', 'PW', 'PA', 'PR', 'RI', 'SC', 'SD', 'TN',
         'TX', 'UT', 'VT', 'VI', 'VA', 'WA', 'WV', 'WI', 'WY');


     INSERT INTO WEBSITE_GATEKEEPER_STATE (PRG_CODE, STATE) 
     select 'PA', column_value
     from table ( select * from strs );

 end;
 /  

答案 2 :(得分:0)

通过这种方式

DECLARE

var_sql clob;
var_bpa varchar2(3):='BPA';

BEGIN

var_sql := 'insert all ';
var_sql :=var_sql||' into website_gatekeeper_state (prg_code, state) 
values('''||var_bpa||''', ''AL'')';
var_sql :=var_sql||' into website_gatekeeper_state (prg_code, state) 
values('''||var_bpa||''', ''AF'')';
var_sql :=var_sql||' into website_gatekeeper_state (prg_code, state) 
values('''||var_bpa||''', ''AM'')';
var_sql :=var_sql||' into website_gatekeeper_state (prg_code, state) 
values('''||var_bpa||''', ''AP'')';
var_sql :=var_sql||' into website_gatekeeper_state (prg_code, state) 
values('''||var_bpa||''', ''DC'')';
var_sql :=var_sql||' select 1 from dual ';   

EXECUTE IMMEDIATE var_sql; 

END;

答案 3 :(得分:0)

您可以使用& variable_name在运行时输入值,而不是编写每个新语句。

For example -
 INSERT INTO WEBSITE_GATEKEEPER_STATE (PRG_CODE, STATE) 
 VALUES(&PRG_CODE, &STATE);

答案 4 :(得分:0)

INSERT ALL
  INTO mytable (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
  INTO mytable (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
  INTO mytable (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
SELECT * FROM dual;

或......

insert into mytable (column1, column2, .. columnn)
          select value1, value2 ... valuen from dual
union all select value1, value2 ... valuen from dual