How to modify a DB2 view query without dropping and creating view - db2

I need to modify the query of a DB2 view without dropping and creating that view.
I use DB Visualizer and I tried the ALTER VIEW command but I got the error "unexpected token 'VIEW' was found following 'ALTER'.
Any idea on how to resolve this problem?

Check out CREATE or REPLACE view functionaliy - it is also call soft invalidation and desribed in detail on the page referenced.

Here would be a sample script, how to modify a view. It works fine without any error on V11.5 GA, AIX 7.1.
#!/bin/sh
db2 -v "drop db db1"
db2 -v "create db db1"
db2 -v "connect to db1"
db2 -v "create table t1 (c1 int, c2 char(10), c3 char(10))"
db2 -v "insert into t1 values (1, 'aaa', 'AAA')"
db2 -v "insert into t1 values (2, 'bbb', 'BBB')"
db2 -v "create view v1 as select c1, c2 from t1"
db2 -v "select * from v1"
db2 -v "create or replace view v1 as select c1, c2, c3 from t1"
db2 -v "select * from v1"
db2 -v "terminate"
The first select * from v1 returns 2 columns but second one returns 3 columns sice it's replaced with a new v1.
Hope this helps.

Related

PostgreSql , extract schema objects DDL to separate SQL file

I want to export all objects DDL to separate file example (table_a_create.sql, view_b_create.sql, trigger_c_create.sql, table_contraints.sql ...)
I was trying with pg_dump but it only exports to one file for the whole schema.
I read some questions about this on stackoverflow but still not enough for my requirement
Ex: How to dump PostgreSQL database structure (each object in separate file)
Is there any way to do it? I'm using Windows
If you are on the client machine, you can put this in a SQL script (e.g. export_plpgsql.sql) :
\pset tuples_only on
\pset footer off
\set QUIET on
\pset format unaligned
\set QUIET off
SELECT '\echo ''* Export '||(CASE proKind WHEN 'f' THEN 'Function' ELSE 'Procedure' END)||' : '||proName||''''
||chr(10)||'\copy (SELECT pg_get_functiondef('||p.oid||')) TO '''||:'export_path'||'/'||upper(proName)
||(CASE proKind WHEN 'f' THEN '.fct' ELSE '.prc' END)||''' WITH CSV;' as export_routine
FROM pg_proc p
WHERE proNamespace = (SELECT oid FROM pg_namespace WHERE nspName = lower(:'schema_name'))
ORDER BY proName;
and call it using 2 arguments : schema_name and export_path, for example :
psql -U my_ -d my_db -v schema_name=my_schema -v export_path=C:/temp/export_PG -f export_plpgsql.sql > C:\temp\export_plpgsql.gen.sql
This will generate a script containing all the exports command for your plpgsql routines, e.g.
\copy (SELECT pg_get_functiondef(51296)) TO 'C:/temp/export_PG/my_procedure.prc' WITH CSV;
Last step : run the generated script
psql -U my_ -d my_db -f C:\temp\export_plpgsql.gen.sql
It will generate a .prc file for each procedure and a .fct file for each function.
NB: You may have to refine the script as you can have other kind of functions (proKind) in pg_proc view.

how to load timestamp column values to the DATE datatype column in DB2

I need to load the timestamp values "5/14/2013 12:00:00 AM" into the date column .The file I need to load is in CSV format.
I want the result should be like this "5/14/2013".I don't want the time to get loaded into the table.
Could anyone suggest the solution to resolve the issue would be very helpful
The environment am using is LINUX
Here is a sample script.
#!/bin/sh
echo "5/14/2013 12:00:00 AM" > dat.csv
echo "5/15/2013 12:01:00 AM" >> dat.csv
echo "5/16/2013 12:02:00 AM" >> dat.csv
# prepare objects for test
db2 -v "drop db db1"
db2 -v "create db db1"
db2 -v "connect to db1"
db2 -v "create table t1 (c1 date)"
db2 -v "load from dat.csv of del insert into t1"
db2 -v "select c1 from t1"
Load command returns SQL3125W, column "1" truncated warning message but
"select c1 from t1" returns 3 rows as below on Db2 V11.5 + AIX.
C1
----------
05/14/2013
05/15/2013
05/16/2013
3 record(s) selected.
Hope this helps.

postgreSQL backup tables and views

I want to have backup of my database, but using pgadmin III I can only restore tables, but I want my views be restored as well. Is there any way doing that?
tnx
you could do this task with CLI, eg:
-bash-4.2$ pg_dump -s $(psql -c "select string_agg('-t '||relname,' ') from pg_class where relkind='v' and relnamespace='public'::regnamespace" -At) | grep -i create
CREATE VIEW avva AS
CREATE VIEW v AS
Of course without grep to get the full definition.
otherwise you have to repeat for every view
or create in schema backup, DumpOptions #1 choose Only Schema...

echo queries psql from a file as they are run

I have a bash script that opens a file and executes a bunch of psql queries.
I want these queries to be echoed/print as and when they run.
How do I do the same ?
I have tried using \echo for inserts & inside stored procedures too, but it doesn't seem to work. How do I do it ?
Use psql --echo-all.
$ psql --echo-all -c "SELECT 1;"
SELECT 1;
?column?
----------
1
(1 row)
The only way that I know, that you can echo anything during the execution of a PostgreSQL function (named stored procedure), is with raise. This command is used to trown exceptions, but you can throw a NOTICE level exception, that will not interfere on the function execution.
Maybe it is not exactly what you want, but is a good workaround. The way that PostgreSQL execute their procedures, don't allow runtime echos (like Sybase ou Ms SQL Server). See this examples (It will only work inside functions):
raise notice 'Some message';
It will output:
NOTICE: Some message
Or passing vars to the debug:
raise notice 'Inserting '%' in '%'.',var_value,var_table;
When var_table = 'customers' and var_value = 'Joe Doe', it will output:
NOTICE: Inserting 'Joe Doe' in 'customers'
--echo-queries (for shell script)
PGPASS='passwd'
su -c "PGPASSWORD=${PGPASS} psql -d postgres --echo-queries -qc "\pset border 2;" -c "show data_directory;"" postgres
From the Postgres documentation page ( please note that the syntax of psql has remained largely unchanged over versions ), it is clearer with an example of a DDL.
There are several ways to echo. -e to echo just the queries only.
$ psql -ec "create table t1 ( c1 int ) " ;
create table t1 ( c1 int )
CREATE TABLE
If you do not want the "CREATE TABLE" message add a "-q" flag as well
$ psql -eqc "create table t1 ( c1 int ) " ;
create table t1 ( c1 int )
You can add header and footer to your script file:
\set origin_ECHO :ECHO
\set ECHO all
--****** YOUR SCRIPT TEXT *****
--.........
--*****************************
\set ECHO :origin_ECHO

Generate DDL programmatically on Postgresql

How can I generate the DDL of a table programmatically on Postgresql? Is there a system query or command to do it? Googling the issue returned no pointers.
Use pg_dump with this options:
pg_dump -U user_name -h host database -s -t table_or_view_names -f table_or_view_names.sql
Description:
-s or --schema-only : Dump only ddl / the object definitions (schema), without data.
-t or --table Dump : Dump only tables (or views or sequences) matching table
Examples:
-- dump each ddl table elon build.
$ pg_dump -U elon -h localhost -s -t spacex -t tesla -t solarcity -t boring > companies.sql
Sorry if out of topic. Just wanna help who googling "psql dump ddl" and got this thread.
You can use the pg_dump command to dump the contents of the database (both schema and data). The --schema-only switch will dump only the DDL for your table(s).
Why would shelling out to psql not count as "programmatically?" It'll dump the entire schema very nicely.
Anyhow, you can get data types (and much more) from the information_schema (8.4 docs referenced here, but this is not a new feature):
=# select column_name, data_type from information_schema.columns
-# where table_name = 'config';
column_name | data_type
--------------------+-----------
id | integer
default_printer_id | integer
master_host_enable | boolean
(3 rows)
The answer is to check the source code for pg_dump and follow the switches it uses to generate the DDL. Somewhere inside the code there's a number of queries used to retrieve the metadata used to generate the DDL.
Here is a good article on how to get the meta information from information schema,
http://www.alberton.info/postgresql_meta_info.html.
I saved 4 functions to mock up pg_dump -s behaviour partially. Based on \d+ metacommand. The usage would be smth alike:
\pset format unaligned
select get_ddl_t(schemaname,tablename) as "--" from pg_tables where tableowner <> 'postgres';
Of course you have to create functions prior.
Working sample here at rextester