简体   繁体   English

如何将 sql 查询的结果保存到变量中,然后在脚本中使用它?

[英]How to save result from sql query to a variable and later use it in the script?

I have the following script:我有以下脚本:

    cl scr;
    variable l number;
    begin
      select max(length(name)) into :l from goodcustomer;
    end;
    /
    --print :l;
    alter table goodcustomer modify name varchar2(:l);

I am trying to modify the length of name attribute to the maximum length of name (which is 17) that currently exists in the table.我正在尝试将名称属性的长度修改为表中当前存在的名称的最大长度(即 17)。 The code above is giving me the following error in sql developer:上面的代码在 sql 开发人员中给了我以下错误:

    Error report -
    SQL Error: ORA-00910: specified length too long for its datatype
    00910. 00000 -  "specified length too long for its datatype"
    *Cause:    for datatypes CHAR and RAW, the length specified was > 2000;
               otherwise, the length specified was > 4000.
    *Action:   use a shorter length or switch to a datatype permitting a
               longer length such as a VARCHAR2, LONG CHAR, or LONG RAW 

What am I doing wrong here?我在这里做错了什么? Isn't la number that can be used for giving size of varchar2?不是可以用于给出varchar2大小的数字吗? Any other way to achieve the same will also be appreciated?任何其他实现相同目标的方法也将受到赞赏?

Maybe you can use dynamic SQL .也许您可以使用动态 SQL Example (Oracle 18c):示例(Oracle 18c):

Table桌子

SQL> create table goodcustomer ( name varchar2( 4000 ) ) ;

Table created.

SQL> 
SQL> describe goodcustomer 
Name   Null?   Type             
NAME           VARCHAR2(4000)

INSERT some names插入一些名字

SQL> begin
  2    insert into goodcustomer values( 'shortestname' ) ;
  3    insert into goodcustomer values( 'l o n g e s tname' ) ;
  4  end ;
  5  /

PL/SQL procedure successfully completed.

SQL> select max( length( name ) ) from goodcustomer ;
  MAX(LENGTH(NAME)) 
                 17 

ALTER TABLE... MODIFY改变表...修改

SQL> declare
  2    l number := 0 ;
  3    sqlstr varchar2( 4000 ) := '' ;
  4  begin
  5    select max( length( name ) ) into l from goodcustomer ;
  6    sqlstr := 'alter table goodcustomer modify name varchar2( ' 
  7           || to_char( l ) 
  8           || ')' ;
  9    execute immediate sqlstr ;
 10  end ;
 11  /

PL/SQL procedure successfully completed.

-- The NAME column now: VARCHAR2( 17 )
SQL> describe goodcustomer
Name   Null?   Type           
NAME           VARCHAR2(17) 

Additional reading, which may clarify things for you (substitution versus bind variables) see here .附加阅读,可能会为您澄清一些事情(替换与绑定变量),请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将SQL结果存储为变量,并在PostgreSQL的后续SQL命令中使用它 - How to store a result of SQL as an variable and use it in the later SQL commands in PostgreSQL 如何在Powershell字符串变量中保存SQL查询的结果? - How to save result from SQL query in Powershell string variable? 如何将 SQL 查询结果存储到变量中以包含在稍后的 SELECT 语句中 - How to store SQL query result into variable to include in a later SELECT statement 如何将SQL查询的结果保存到VBA中的变量中? - How to save the result of a SQL query into a variable in VBA? sql 将结果保存在变量中,稍后用于更新 - sql save results in variable and later use it for update 将查询的 Count 结果保存到变量中以用于 Access SQL 中的算术 - Save the Count result of a query into a variable to use for arithmetic in Access SQL 如何在C#.net中的变量中保存SQL查询的结果? - How to save the result of a SQL query in a variable in C#.net? 如何将来自 SQL 查询的信息保存到变量中 - How to save information from an SQL query to a variable 存储查询结果,将多个结果作为列表变量以便稍后使用 - store query result, with multiple results as list variable to use later 将Select查询的结果存储在SQL文件PSQL中以供以后使用 - Store result of Select query to use later in sql file PSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM