In another question, it was asked how to query a postgresql runtime parameter (e.g. SHOW search_path;) using a SELECT query. In the answer, it was suggested to use
SELECT * FROM pg_settings WHERE name = 'search_path';
This works great, but how can this be done for custom parameters defined in an extension? (See docs about Customized Options).
Example:
SET abc.my_var = 1;
SHOW abc.my_var;
outputs
1
but
SELECT * FROM pg_settings WHERE name = 'abc.my_var';
returns no rows. Is there some other table/view I can query for my custom parameter using a SELECT statement?
Use the function current_setting()
SELECT current_setting('abc.my_var');
http://www.postgresql.org/docs/current/static/functions-admin.html#FUNCTIONS-ADMIN-SET
Related
In BigQuery, I have a query and its result is like:
myQueryValue
select * from 'some path'
I'd like to use it directly in new query.
SELECT someValue
FROM
(
select * from 'some path' <- How can I replace this to myQueryValue?
)
How can I use the result value of some queries like EVAL?
----------------EDITED AT 14th Oct.----------------
Thanks for all answer but I need to explain more what I want.
If I have a 'queryTable' like
col
'select * from tableA'
The result of 'select * from tableA' is
foo
bar
When I only know about 'queryTable', how can I get the this result?
foo
bar
I'd like to refer 'queryTable', and get the final result of its.
You can use sub queries, its a query inside the from clause.
Here is an example code:
SELECT * FROM (SELECT ID FROM CUSTOMERS WHERE SALARY > 4500)
A Subquery or Inner query or a Nested query is a query within another
SQL query and embedded within the WHERE clause.
A subquery is used to return data that will be used in the main query
as a condition to further restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE
statements along with the operators like =, <, >, >=, <=, IN, BETWEEN,
etc.
There are a few rules that subqueries must follow −
Subqueries must be enclosed within parentheses.
A subquery can have only one column in the SELECT clause, unless
multiple > >columns are in the main query for the subquery to compare
its selected > >columns.
An ORDER BY command cannot be used in a subquery, although the main
query >can use an ORDER BY. The GROUP BY command can be used to
perform the same >function as the ORDER BY in a subquery.
Subqueries that return more than one row can only be used with
multiple >value operators such as the IN operator.
The SELECT list cannot include any references to values that evaluate
to a >BLOB, ARRAY, CLOB, or NCLOB.
A subquery cannot be immediately enclosed in a set function.
The BETWEEN operator cannot be used with a subquery. However, the
BETWEEN >operator can be used within the subquery.
click here for more information about sub queries.
Below is example of how easy to achieve this
DECLARE myQueryValue STRING;
SET myQueryValue = "select * from your_table";
EXECUTE IMMEDIATE '''
SELECT someValue
FROM ( ''' || myQueryValue || ''' )''';
As you didn't provide additional information I'm going to elaborate my comment.
In comment I've proposed that you can use Declare with Set. Good differences between those two are presented in this stackoverflow thread.
DECLARE does not initialize the variable. When you declare it you declare the variable name, the type, and a default value, which could be an expression.
SET is for initializing the variable you declared previously, and you cannot SET the variable until you DECLARE it.
One of the examples has been provided in #Mikhail Berlyant answer in this thread.
However, more detailed information with more examples are mentioned in GCP Set Reference.
Sets a variable to have the value of the provided expression, or sets multiple variables at the same time based on the result of multiple expressions.
The SET statement may appear anywhere within the body of a script.
This is the easiest way to achieve this.
Another common way you could do this is to use SubQuery/Nested Query, it's also well described in the GCP BigQuery Reference.
In GCP doc you can also find example which uses Set, Declare and subquery:
DECLARE target_word STRING DEFAULT 'methinks';
DECLARE corpus_count, word_count INT64;
SET (corpus_count, word_count) = (
SELECT AS STRUCT COUNT(DISTINCT corpus), SUM(word_count)
FROM `bigquery-public-data`.samples.shakespeare
WHERE LOWER(word) = target_word
);
SELECT
FORMAT('Found %d occurrences of "%s" across %d Shakespeare works',
word_count, target_word, corpus_count) AS result;
Output:
Found 151 occurrences of "methinks" across 38 Shakespeare works
I am trying to search for information on this topic in sql but search is not showing me results.
SELECT * FROM TABLE(package/procedure/function);
Which topic does the above sql statement come under? Can I have a link to a doc?
The TABLE collection expression is described here in the Oracle documentation.
In short, it is used to convert a collection or pipelined function into a table that can be queried by a SELECT statement.
Typically, the collection must be of a datatype that is defined at the database level (i.e. a datatype that was created by a create or replace type ... statement).
e.g.
select *
from table(sample_pkg.func_that_rtrns_array);
I am trying to create an SQL which adds custom text on both sides of the result of an SQL query in Postgres. I have many site domains stored in a database and I would like to copy them into an HTML file with the domain names inside an iframe. So I would like the query to return my domains within <iframe></iframe> tag. I found the following from the Postgresql documentation.
SELECT textcat(textcat(first_name,text ' '),last_name) from table;
But what I am looking for is different. My simple select SQL would be
select site_domain from site where status = "active".
Is there any function that allows me to append custom text on both sides of my result.
Do you mean something like this?
select '<iframe>'|| site_domain||'</iframe>'
from site where status = "active"
An alternative to Mark's answer would be to use the CONCAT function:
SELECT CONCAT('<iframe>', site_domain, '</iframe>') AS site_domain
FROM site
WHERE status = 'active'
Did you try:
SELECT textcat(textcat('<iframe>',site_domain),'</iframe>')
from site
where status = 'active';
I need to get the final SQL query that is sent to the database from NamedParameterJdbcTemplate.
For example:
SELECT * FROM tbl WHERE name = :name;
I need something like this:
SELECT * FROM tbl WHERE name ='mark';
Thanks a lot.
A quick look at the source code of the NamedParameterJdbcTemplate will show that all the queries get through getParsedSql() method and the NamedParameterUtils for parsing.
SELECT * FROM tbl WHERE name = :name;
Will probably get translated to something like
SELECT * FROM tbl WHERE name = ?;
And the parameter will be provided as a separate object, because that's how JDBC works.
If you just want to inspect the statements you can add some breakpoints and take a look. If you want to actually obtain the values you can change the code, to make it accessible, either by reflection or by actually using your own version of NamedparameterJdbcTemplate.
Note that you won't probably see what you expect.
I'm building SQL queries on the fly and am a bit of a beginner with them.
How would I do something like:
Select * from X where Type = *Any*
Basically, I want to select all of them. I know I could not include the where but often times the Type variable does in fact have values. I want to be able to replace the "Any" part with something else on the fly rather than a whole different expression...
I think this is what you are looking for:
--please change the datatype int to match your datatype for type column
DECLARE #type int = 1;
SELECT * FROM X
WHERE x.[Type] = COALESCE(#type,x.[type]);
If you don't pass a value in variable #type, it will default to your type and return everything. Otherwise, it will supply the variable value as needed. Hope this helps!
This kind of depends on what your SQL implementation is. That being said the two concepts you are missing are using the keyword "LIKE" instead of "=", and using wildcards. I advise going through W3Schools SQL section to get started, or better yet buy some introduction to SQL book. http://www.w3schools.com/sql/default.asp
SELECT * FROM X
WHERE Type LIKE '%';
select * from X where Type Like '%';
If you want everything it is simple:
SELECT *
FROM my_table
There is no where clause , in this case. You usually want to first try something like this , though:
SELECT *
FROM my_table
WHERE rownum < 2
But rownum is an Oracle-specific column. You need to look it up for your specific database .