Sql server 如果COL_LENGTH('EMP_NUM','EMPLOYEE')在ORACLE中不为NULL-等效

Sql server 如果COL_LENGTH('EMP_NUM','EMPLOYEE')在ORACLE中不为NULL-等效,sql-server,oracle,tsql,oracle11g,Sql Server,Oracle,Tsql,Oracle11g,我在SQL server中使用下面的脚本来更改列,使其允许null为NOTNULL 在Oracle11g或更高版本中,对于相同的应用程序,等效语法是什么 IF COL_LENGTH('EMP_NUM','EMPLOYEE') IS NOT NULL ALTER TABLE EMPLOYEEALTER COLUMN EMP_NUMnumeric(10,0) NOT NULL; 在Oracle中,必须使用动态SQL,因为不能按原样在PL/SQL中执行DDL。为什么使用PL/SQL?因为IF-

我在SQL server中使用下面的脚本来更改列,使其允许null为NOTNULL

在Oracle11g或更高版本中,对于相同的应用程序,等效语法是什么

IF COL_LENGTH('EMP_NUM','EMPLOYEE') IS NOT NULL
   ALTER TABLE EMPLOYEEALTER COLUMN EMP_NUMnumeric(10,0) NOT NULL;

在Oracle中,必须使用动态SQL,因为不能按原样在PL/SQL中执行DDL。为什么使用PL/SQL?因为IF-THEN-ELSE是PL/SQL,而不是SQL

假设这是PL/SQL过程的一部分,那么

if col_length('EMP_NUM', 'EMPLOYEE') is not null then
  execute immediate 'alter table employee modify emp_num not null';
end if;
那是另一部分。然而,由于Oracle没有COL_LENGTH函数或类似的函数,据我所知,您必须自己做。下面是一个例子:

列允许空值的表:

SQL> create table employee (emp_num number);

Table created.
检查空值?列-空,所以是-它允许空值

SQL> desc employee;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMP_NUM                                            NUMBER
查询数据字典以查找列的长度:

SQL> select data_length from user_tab_columns
  2  where table_name = 'EMPLOYEE'
  3    and column_name = 'EMP_NUM';

DATA_LENGTH
-----------
         22
最后,您未来的PL/SQL过程:

SQL> declare
  2    l_len number;
  3  begin
  4    select data_length
  5      into l_len
  6      from user_tab_columns
  7      where table_name = 'EMPLOYEE'
  8        and column_name = 'EMP_NUM';
  9
 10    if l_len is not null then
 11       execute immediate 'alter table employee modify emp_num not null';
 12    end if;
 13  end;
 14  /

PL/SQL procedure successfully completed.

SQL> desc employee;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMP_NUM                                   NOT NULL NUMBER

SQL>
SQL> create or replace function col_length(par_table in varchar2, par_column in varchar2)
  2    return number
  3  is
  4    retval user_tab_columns.data_length%type;
  5  begin
  6    select data_length
  7      into retval
  8      from user_tab_columns
  9      where table_name = par_table
 10        and column_name = par_column;
 11    return retval;
 12  end;
 13  /

Function created.

SQL> select col_length('EMPLOYEE', 'EMP_NUM') from dual;

COL_LENGTH('EMPLOYEE','EMP_NUM')
--------------------------------
                              22

SQL>
如您所见,空值?列现在包含NOTNULL,这意味着ALTER TABLE已成功执行

此外,您甚至可以创建自己的函数来查询列的数据长度,然后在过程中使用它:

SQL> declare
  2    l_len number;
  3  begin
  4    select data_length
  5      into l_len
  6      from user_tab_columns
  7      where table_name = 'EMPLOYEE'
  8        and column_name = 'EMP_NUM';
  9
 10    if l_len is not null then
 11       execute immediate 'alter table employee modify emp_num not null';
 12    end if;
 13  end;
 14  /

PL/SQL procedure successfully completed.

SQL> desc employee;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMP_NUM                                   NOT NULL NUMBER

SQL>
SQL> create or replace function col_length(par_table in varchar2, par_column in varchar2)
  2    return number
  3  is
  4    retval user_tab_columns.data_length%type;
  5  begin
  6    select data_length
  7      into retval
  8      from user_tab_columns
  9      where table_name = par_table
 10        and column_name = par_column;
 11    return retval;
 12  end;
 13  /

Function created.

SQL> select col_length('EMPLOYEE', 'EMP_NUM') from dual;

COL_LENGTH('EMPLOYEE','EMP_NUM')
--------------------------------
                              22

SQL>
最后:

SQL> drop table employee;

Table dropped.

SQL> create table employee (emp_num number);

Table created.

SQL> begin
  2    if col_length('EMPLOYEE', 'EMP_NUM') is not null then
  3       execute immediate 'alter table employee modify emp_num not null';
  4    end if;
  5  end;
  6  /

PL/SQL procedure successfully completed.

SQL> desc employee;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMP_NUM                                   NOT NULL NUMBER

SQL>

我已经研究过了,没有发现任何东西在我的搜索中看起来很有希望。那篇文章怎么样?你不明白吗?请注意,我从未使用过Oracle,也不知道T-SQL和PL/SQL之间的语法差异。@Larnu如果我在不检查现有列的情况下运行alter命令。我得到的错误如下错误报告-ORA-01442:要修改为非空的列已不为空要修改为非空的列已不为空。。。你不明白吗;我看很清楚。@Larnu脚本中如何检查和跳过?这是我的问题。我不想改变已经不是nul的列,我想改变允许null的列。如何为此编写oracle语句?