I have databases
DEV_a TST_a REAL_a
DEV_b TST_b REAL_b
DEV_c TST_c REAL_c
These can be on same server or on different server. Now I'm writing a script/transaction that's relevant only for DEV_a, TST_a and REAL_c.
Is there any way I can limit script to run only on those databases?
I thought this might work:
USE [DEV_a]
but that would force it to run only on one database, but I have a list.
Is this doable?
Extra question: if it is doable, can I run parts of the script for DEV_a and TST_a but different part of script for REAL_c?
Consider using DB_NAME(), it should be available in SQL 2005.
SELECT * FROM dbo.SomeTableName
WHERE DB_NAME() IN ('DEV_a', 'TST_a', 'REAL_c')
As for the extra question, I think DB_NAME() and IF...ELSE will work
Related
We have about 200 user define functions in DB2. These UDF are generated by datastudio into a single script file.
When we create a new DB, we need to run the script file several times because some UDF are dependent on other UDF and cannot be create until the precedent functions are created first.
Is there a way to generate a script file so that the order they are deployed take into account this dependency. Or is there some other technique to arrange the order efficiently?
Many thanks in advance.
That problem should only happen if the setting of auto_reval is not correct. See "Creating and maintaining database objects" for details.
Db2 allows to create objects in an "unsorted" order. Only when the object is used (accessed), the objects and its depending objects are checked. The behavior was introduced a long time ago. Only some old, migrated databases keep auto_reval=disabled. Some environments might set it based on some configuration scripts.
if you still run into issues, try setting auto_reval=DEFERRED_FORCE.
The db2look system command can generate DDL by by object creation time with the -ct option, so that can help if you don't want to use the auto_reval method.
I see how to debug queries stored as Functions in the database. But my problem is with an external QGIS plugin that connects to my Postgres 10.4 via network and does a complex query and calculations, and stores the results back into PostGIS tables:
FOR r IN c LOOP
SELECT
(1 - ST_LineLocatePoint(path.geom, ST_Intersection(r.geom, path.geom))) * ST_Length(path.geom)
INTO
station
(continues ...)
When it errors, it just returns that line number as the failing location, but no clue where it was in the loop through hundreds of features. (And any features it has processed are not stored to the output tables when it fails.) I totally don't know enough about the plugin and about SQL to hack the external query, and I suspect if it was a reasonable task the plugin author would have included more revealing debug messages.
So is there some way I could use pgAdmin4 (or anything) from the server side to watch the query process? Even being able to see if it fails the first time through the loop or later would help immensely. Knowing the loop count at failure would point me to the exact problem feature. Being able to see "station" or "r.geom" would make it even easier.
Perfectly fine if the process is miserably slow or interferes with other queries, I'm the only user on this server.
This is not actually a way to watch the RiverGIS query in action, but it is the best I have found. It extracts the failing ST_Intersects() call from the RiverGIS code and runs it under your control, where you can display any clues you want.
When you're totally mystified where the RiverGIS problem might be, run this SQL query:
SELECT
xs."XsecID" AS "XsecID",
xs."ReachID" AS "ReachID",
xs."Station" AS "Station",
xs."RiverCode" AS "RiverCode",
xs."ReachCode" AS "ReachCode",
ST_Intersection(xs.geom, riv.geom) AS "Fraction"
FROM
"<your project name>"."StreamCenterlines" AS riv,
"<your project name>"."XSCutLines" AS xs
WHERE
ST_Intersects(xs.geom, riv.geom)
ORDER BY xs."ReachID" ASC, xs."Station" DESC
Obviously replace <your project name> with the QGIS project name.
Also works for the BankLines step if you replace "StreamCenterlines" with "BankLines". Probably could be adapted to other situations where ST_Intersects() fails without a clue.
You'll get a listing with shorter geometry strings for good cross sections and double-length strings for bad ones. Probably need to widen your display column a lot to see this.
Works for me in pgAdmn4, or in QGIS3 -> Database -> DB Manager -> (click the wrench icon). You could select only bad lines, but I find the background info helpful.
I'm very new to OSQuery and i'd like to execute a query (e.g. SELECT * FROM last) every 5 minutes. Is there any chance, to define a script, which executes this routine in within a crontab or something else like this?
Probably it should be enough to execute the script with the query as parameter, but there is nothing in the documentation, so i guess, it won't be supported yet.
I checked their Community and also their FAQ but haven't found something relating to my problem.
OSQuery is currently on the latest version (1.7.3), self compiled, running on Ubuntu Server, 64 bit 15.10.
If you need more information to help me, just let me know.
The recommended method is using scheduled queries. You create a 'pack' like one of these GitHub link which includes the queries and frequencies. Then update the osqueryd config to include the pack.
After even more documentation and different sites, i have found a pretty cool snippet, which allows, to send the query as parameter, by calling the osqueryi process.
/path/to/osqueryi --json "YOUR QUERY"
And this returns the result in your terminal - JSON Format. So it's pretty easy to write a script (any language), execute the snippet above and parse the content. This script can be a cron as well.
Maybe you could write a script (or a C program) to perform your query.
And then use the cron to run your program every 5 minutes.
I need access to the complete source code of objects in order to automate certain tasks. For example: complete source of view is the view itself, it's rules, triggers, privileges...
By using different PostgreSQL tools like PgAdmin, pg_dump, psql, this can easily be fetched, but I need to be able to access it through a (sql/plpgsql) function call.
It's not too difficult to implement API looking like this: getFunctionSource, getTableSource, getFUnctionSource. However, it looks like this code would need a lot of maintenance along different versions of database.
Is there officially maintained or well tested extension, API, pg_dump wrapper or whatever I can use?
If you run psql -E, you'll see hidden queries that get run by Postgres to output data definitions.
A function's raw source, for instance, can be found by running \df foo, reading the query, and subsequently trying:
select prosrc from pg_proc where proname = 'foo'
\sf foo doesn't yield the relevant functions using that approach, but a cursory peek at the docs on system information functions (of which there are many) should suggest that it's just a wrapper around:
select pg_get_functiondef('foo'::regproc);
A few views to get you started, if you go the route of posting your stuff on github:
https://gist.github.com/ddebernardy/7893922
(You'll want to create a "system" schema before running the file using \i in psql.)
Is there a way to show the SQL query executed by phpPgAdmin as the way phpMyAdmin does?
For example, if I modify a column, it should show the ALTER command being executed.
If this is not possible, what other interface could I use to get this feature?
It's not possible with any currently released version of phpPgAdmin, although the feature could probably be added. You'd need to intercept the SQL being sent to the back-end, and then display this back out to the user. SQL execution is pretty well centralized, and if you look at the "history" feature you will see a way to trap/show queries, so munging those bits together would probably get you what you want. HTH, if someone implements this, please send a pull request!
As a quick dirty hack you could alter sources a bit to enable sql logging:
In classes/database/ADODB_base.php in
function execute($sql) {
...
}
add these lines at the beginning:
global $misc;
$misc->saveScriptHistory($sql);
This worked in my 5.0.3 version.