Monday, January 2, 2012

Accept argument during .sql file invocation

Hello,
        Some time we have to set the variable of .sql file run time. In that case require to take input and set the variable accordingly.

For eg. There is sql file called 'tablefunc.sql' in which default search_path is set to public but we require it to be dynamic. Now we need to execute the .sql file as

In .sql file
SET search_path = :search_path;

Invoke sql file using system command
config = YAML::load(File.open('config/database.yml'))
pg_schema = 'demo'

system("psql -U #{config[env]['username']} -d #{config[env]['database']} --variable search_path=#{pg_schema} < lib/tablefunc.sql")

No comments:

Post a Comment