PL / SQL帮助检查特定表中的特定值

时间:2012-11-13 23:11:51

标签: sql sql-server

我有查询。我有1列user_auto,其值为234。 我想找到哪个表正在使用此列并具有特定值。

我已经完成了使用此列的表数。

SELECT t.name AS table_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%user_auto%'
ORDER BY  table_name;

现在我想要user_auto = 234这个表更改值。 表示User_auto值是234.想要了解该用户的所有细节吗?

1 个答案:

答案 0 :(得分:0)

你想要这样的东西吗?

SQL> desc foo1
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 USER_AUTO                                          NUMBER

SQL> desc foo2
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 USER_AUTO                                          NUMBER

SQL> select * From foo1;

 USER_AUTO
----------
       123
      1234

SQL> select * From foo2;

 USER_AUTO
----------
       234

SQL> set serverout on
SQL> declare
  2    v_val varchar2(3) := '234';
  3    v_cnt pls_integer;
  4  begin
  5    for r_tab in (select table_name, column_name
  6                    from all_tab_columns
  7                   where column_name = 'USER_AUTO'
  8                   order by 1)
  9    loop
 10      dbms_output.put_line('Found table ' || r_tab.table_name || ' with column '||r_tab.column_name);
 11      execute immediate 'begin select count(*) into :b0 from '
 12                        || r_tab.table_name
 13                        || ' where rownum = 1 and ' || r_tab.column_name || ' = :b1;'
 14                        || 'end;' using out v_cnt, v_val;
 15      if (v_cnt = 1)
 16      then
 17        dbms_output.put_line(chr(9)||'row found for ' || v_val);
 18      else
 19        dbms_output.put_line(chr(9)||'no row found for ' || v_val);
 20      end if;
 21    end loop;
 22  end;
 23  /
Found table FOO1 with column USER_AUTO
        no row found for 234
Found table FOO2 with column USER_AUTO
        row found for 234

PL/SQL procedure successfully completed.