Query to find all the empty tables in db2 - db2-400

DECLARE #sql NVARCHAR(MAX)
SELECT #sql = ISNULL(#sql + 'UNION ALL', '') + '
SELECT ''' + TABLE_SCHEMA + '.' + TABLE_NAME + '.' + COLUMN_NAME + ''' AS col FROM ' + TABLE_SCHEMA + '.' + TABLE_NAME + ' HAVING COUNT(' + COLUMN_NAME + ') = 0
'
FROM INFORMATION_SCHEMA.COLUMNS
EXEC (#SQL)
The above code works well in sql Server but doesnt work in db2. can somebody help me in this

I'm going to take a guess and say that your declaration is wrong. It's hard to say for sure without more info in the question but your declaration should probably look like so:
DECLARE #sql VARIABLE NVARCHAR(MAX)

Related

Oracle how to uppercase columns name

I would like to create a function who uppercase all columns name, in a oracle db.
I don't use oracle everyday, so I need help.
I would like this, but for Oracle :
CREATE OR REPLACE FUNCTION uppercase_fields(schemaname text) RETURNS void AS $$
DECLARE
r RECORD;
full_table text;
geom_type_count integer;
BEGIN
FOR r IN
EXECUTE 'SELECT table_name, table_schema, column_name FROM information_schema.columns WHERE table_schema = $1 AND column_name <> upper(column_name)'
USING schemaname
LOOP
EXECUTE 'ALTER TABLE "' || r.table_schema || '"."' || r.table_name || '" RENAME "' || r.column_name || '" to "' || upper(r.column_name) || '"';
END LOOP;
END;
$$ LANGUAGE plpgsql;
Thanks
Oracle, as the default functionality, will covert all unquoted table/column identifiers to upper case - so you do not need to use the UPPER function; just leave the identifiers unquoted.
To find the data you require you want the ALL_TAB_COLUMNS or USER_TAB_COLUMNS tables from the data dictionary:
BEGIN
FOR r IN ( SELECT owner, table_name, column_name
FROM ALL_TAB_COLUMNS
WHERE owner IN ( 'your', 'list' , 'of', 'tablespaces' )
AND column_name <> UPPER( column_name )
)
LOOP
EXECUTE 'ALTER TABLE "' || r.owner || '"."' || r.table_name
|| '" RENAME COLUMN "' || r.column_name || '" TO ' || r.column_name;
END LOOP;
END;
/
If your column names are reserved words or otherwise cannot be in an unquoted identifier then you can use:
BEGIN
FOR r IN ( SELECT owner, table_name, column_name
FROM ALL_TAB_COLUMNS
WHERE owner IN ( 'your', 'list' , 'of', 'tablespaces' )
AND column_name <> UPPER( column_name )
)
LOOP
EXECUTE 'ALTER TABLE "' || r.owner || '"."' || r.table_name
|| '" RENAME COLUMN "' || r.column_name || '" TO "' || UPPER(r.column_name) || '"';
END LOOP;
END;
/
For change to uppercase all columns name in Oracle, use this solution:
DECLARE
TARGET_TABLE_NAME VARCHAR2(31) := 'ENTER_YOUR_TARGET_TABLE_NAME_HERE';
BEGIN
FOR I IN (
SELECT
COLUMN_NAME
FROM
ALL_TAB_COLUMNS
WHERE
TABLE_NAME = TARGET_TABLE_NAME
AND
COLUMN_NAME <> UPPER(COLUMN_NAME)
)
LOOP
EXECUTE IMMEDIATE 'ALTER TABLE ' ||
TARGET_TABLE_NAME ||
' RENAME COLUMN "' ||
I.COLUMN_NAME ||
'" TO ' ||
UPPER(I.COLUMN_NAME);
END LOOP;
END;
/
Just replace the name of your target table by ENTER_YOUR_TARGET_TABLE_NAME_HERE

Oracle: update table using dynamic column names

I am using Oracle 11g. My tables include columns like name and l_name (lowercase of name column). I am trying to iterate through all the columns in my table space to set the l_ columns to lowercase of their respective uppercase columns. Here is what I tried:
for i in (select table_name from user_tables) loop
SELECT SUBSTR(column_name,3) bulk collect into my_temp_storage FROM user_tab_columns WHERE table_name = i.table_name and column_name like 'L\_%' escape '\';
for j in (select column_name from user_tab_columns where table_name = i.table_name) loop
for k in 1..my_temp_storage.count
loop
if(j.column_name like 'L\_%' escape '\' and SUBSTR(j.column_name,3) = my_temp_storage(k)) then
DBMS_OUTPUT.PUT_LINE( 'update ' || i.table_name || ' set ' || j.column_name || ' = LOWER(' ||my_temp_storage(k)|| ') where ' || j.column_name || ' is not null');
execute immediate 'update ' || i.table_name || ' set ' || j.column_name || ' = LOWER(' ||my_temp_storage(k)|| ') where ' || j.column_name || ' is not null';
end if;
end loop;
end loop;
end loop;
I am storing all the names of columns in uppercase in my_temp_storage and updating the table with the LOWER value of the columns in my_temp_storage. This gave me an error saying:
Error report -
ORA-00900: invalid SQL statement
ORA-06512: at line 8
00900. 00000 - "invalid SQL statement"
*Cause:
*Action:
But the DBMS output seemed to be fine:
`update EMPLOYEE set L_NAME = LOWER(NAME) where L_NAME is not null`
Could you help me with the way I did or any other way it can be done?
The program could certainly be simplified:
begin
for i in (select table_name, column_name from user_tab_columns
where column_name like 'L\_%' escape '\')
loop
l_sql := 'update ' || i.table_name || ' set ' || i.column_name
|| ' = LOWER(' ||substr(i.columm_name,3)
|| ') where ' || i.column_name || ' is not null';
execute immediate l_sql;
end loop;
end;
It seems an odd database design though. Have you considered virtual columns, and/or function-based indexes, instead of manually maintained columns?

oracle set public synonym all table or other object?

I have productionDB and testDB.ProductionDB have synonym for tables.And now i want to create synonym for all tables for testDB.How i can create synonym all of them. I can get list synonym this query
select *
from all_synonyms s
join all_objects o
on s.table_owner = o.owner
and s.table_name = o.object_name
where s.table_owner = 'XYZ'
Try the following query:
SELECT CAST(dbms_metadata.get_ddl(object_type => 'SYNONYM',
name => a.synonym_name,
SCHEMA => a.owner) AS VARCHAR2(4000))
FROM ALL_SYNONYMS a;
Share and enjoy.
This is a starter script - it writes sql as output. Run it, READ the output first. Uncomment the last line when you think it is correct - it has to be run with privilege.
set pages 0
set feed off
set linesize 180
set trimspool on
spool syn.sql
select 'CREATE PUBLIC SYNONYM ' || SYNONYM_NAME | ' FOR ' ||
TABLE_OWNER || '.' || TABLE_NAME || ';'
from ALL_SYNONYMS
where DB_LINK is NULL;
select 'CREATE PUBLIC SYNONYM ' || SYNONYM_NAME | ' FOR ' ||
TABLE_OWNER || '.' || TABLE_NAME || '#' || DB_LINK || ';'
from ALL_SYNONYMS
where DB_LINK is NOT NULL;
spool off
-- uncomment after it has been tested
--#syn.sql
If testDB was created using a PROD export then why are the synonyms all missing? Something is very wrong here. OR I am missing something.

UPDATE statements in oracle pl/sql loop with tablenames as parameters

I have a requirement where I need to run set of UPDATE statements in a for loop.
In the cursor there is a column called PROPERTY_ID which is a number and there are many tables
that have this number appended.
For ex: SELECT * FROM PC_ORG_EXT_111(where 111 is the property_id)
This is the code and it's throwing error.
Can anyone assist me if I'm missing something here.
SET SERVEROUTPUT ON SIZE 1000000
SET LINESIZE 1000
SET PAGESIZE 0
DECLARE
V_PROP_ID VARCHAR(200);
V_CNT NUMBER(25);
V_SQL_STRING VARCHAR2(500);
CURSOR CUR_CON
IS
SELECT * FROM PRE_CONVERSION_UNMERGE_LIST;
BEGIN
FOR REC_CON IN CUR_CON
LOOP
V_PROP_ID := 'PROPARCH.PC_ORG_EXT_' || REC_CON.PROPERTY_ID;
dbms_output.put_line('Property Table Name ' || V_PROP_ID);
EXECUTE IMMEDIATE 'select COUNT(1) from ' ||V_PROP_ID;
EXECUTE IMMEDIATE '
UPDATE SIEBEL.S_ACCNT_POSTN
SET OU_EXT_ID = ' || REC_CON.VALID_SURVIVIR_REC ||'
WHERE OU_EXT_ID = ' || REC_CON.INVALID_SURVIVOR_REC||'
AND OU_EXT_ID IN (SELECT ISAC_ROW_ID
FROM ' || V_PROP_ID || '
WHERE INTEGRATION_ID = '||REC_CON.DELPHI_ID||')
AND POSITION_ID NOT IN
(SELECT POSITION_ID
FROM SIEBEL.S_ACCNT_POSTN
WHERE OU_EXT_ID = '||REC_CON.VALID_SURVIVIR_REC||')';
END LOOP;
END;
Error says :
ORA-00933: SQL command not properly ended
ORA-06512: at line 20
Also let me know if there's a better way of doing it.
Thanks,
Please try this! - Single Quotes has to be Used with escape characters!
EXECUTE IMMEDIATE '
UPDATE SIEBEL.S_ACCNT_POSTN
SET OU_EXT_ID = ''' || REC_CON.VALID_SURVIVIR_REC ||'''
WHERE OU_EXT_ID = ''' || REC_CON.INVALID_SURVIVOR_REC||'''
AND OU_EXT_ID IN (SELECT ISAC_ROW_ID
FROM ' || V_PROP_ID || '
WHERE INTEGRATION_ID = '''||REC_CON.DELPHI_ID||''')
AND POSITION_ID NOT IN
(SELECT POSITION_ID
FROM SIEBEL.S_ACCNT_POSTN
WHERE OU_EXT_ID = '''||REC_CON.VALID_SURVIVIR_REC||''')';

Replace CHAR with VARCHAR2

How can I replace CHAR with VARCHAR2 in all tables in a schema?
Note: I'm content with a query that returns the ALTER TABLE statements so I can save the script and run it again.
select 'ALTER TABLE "' || owner || '"."' || table_name
|| '" MODIFY ("' || column_name
|| '" VARCHAR2(' || data_length || '));'
from all_tab_columns tc
where data_type = 'CHAR'
and owner = :schemaname
and exists (
select 1
from all_tables t
where tc.owner = t.owner
and tc.table_name = t.table_name
);

Resources