SQLite given a statement it is possible to know what table will be touched [duplicate] - sql

I am using sqlite in a C++ project and I would like to be able to get the table names involved in a query.
Ex:
SELECT * FROM Employee
should return Employee
Now I use successfully qlite3_column_table_name (doc) for this kind of queries but for aggregate queries, the function returns null as the result does not belong to a table directly.
ex:
SELECT SUM(salary) AS total FROM Employee
Surely, when sqlite compiles the statement, the "Employee" keyword is recognised as a table. Do you know aby way to have access to this?
I tried to step through the code of the parser without success...

An authorizer callback allows you to detect which tables are actually accessed by a query.

Related

Get query used to create a table

We use snowflake at work to store data, and for one of the tables, I dont have the SQL query used to create the table. Is there a way to see the query used to make that table?
I tried using the following
get_ddl('table', 'db.table', true)
but this gives me an output like-
This doesnt give me any information about the sql query that was used. How do I get that in snowflake?
If get_ddl() is not enough you may use INFORMATION_SCHEMA.
To get more information you have 2 options:
Use the QUERY_HISTORY() table functions: https://docs.snowflake.com/en/sql-reference/functions/query_history.html
Use the QUERY_HISTORY() view: https://docs.snowflake.com/en/sql-reference/account-usage/query_history.html
If you use the funtions/view above and filter all the records by QUERY_TEXT, maybe you get more information about the exact SQL that was used to create your table.

How do I generate a table name that contains today's date?

It may seem a little strange, but there are already tables with names for each date.
In my project, I have tables for each date to make statistics easier to handle.
Of course, I don't think this is always the best way, but this is the table structure for my project.
(It's a common technique in Google BigQuery and Amazon Athena. This question is about Google BigQuery)
So to get the data, I want to generate today's date. If I use TODAY, I can get the data of the latest day without rewriting the code even if it is the next day.
I tried, but the code didn't work.
Not work 1:
CONCAT in FROM
SELECT
*
FROM
CONCAT('foo_', FORMAT_TIMESTAMP('%Y%m%d', CURRENT_TIMESTAMP(), 'Asia/Tokyo'))
Error:
Table-valued function not found: CONCAT at [4:3]
Not work 2:
create temporary function:
create temporary function getTableName() as (CONCAT('foo_', FORMAT_TIMESTAMP('%Y%m%d', CURRENT_TIMESTAMP(), 'Asia/Tokyo')));
Error:
CREATE TEMPORARY FUNCTION statements must be followed by an actual query.
Question
How do I generate a table name that contains TODAY's date?
In this case, I would recommend you to use Wild tables in BigQuery, which allows you to use some features in Standard SQL.
With Wild Tables you can use _TABLE_SUFFIX, it grants you the ability to filter/scan tables containing this parameter. The syntax would be as follows:
SELECT *
FROM `test-proj-261014.sample.test_*`
where _TABLE_SUFFIX = FORMAT_DATE('%Y%m%d', CURRENT_DATE)
I hope it helps.
Your first query should go like this:
select CONCAT('foo_', FORMAT_TIMESTAMP('%Y%m%d', CURRENT_TIMESTAMP(), 'Asia/Tokyo'))
For creating temporary function, use the below code:
create temp function getTableName() as
((select CONCAT('foo_', FORMAT_TIMESTAMP('%Y%m%d', CURRENT_TIMESTAMP(), 'Asia/Tokyo'))
));
select getTableName()
The error "CREATE TEMPORARY FUNCTION statements must be followed by an actual query." is because once the temporary functions are defined then you have to use the actual query to use that function and then the validity of function dies out. To define persistent UDFs and use them in multiple queries please go through the link to define permanent functions.You can reuse persistent UDFs across multiple queries, whereas you can only use temporary UDFs in a single query.

Listagg Redshift DDL

I am trying to retrieve the DDL for a table in Redshift. I found this view where I can easily select the definition for any table. However I need this information in one line, and I know that there is this Listagg function, but if I try to do this:
select listagg(ddl, ' ')
from admin.v_generate_tbl_ddl
where schemaname = 'schema'
and tablename = 'orders'
It's giving me this error:
Query execution failed
Reason: SQL Error [XX000]: ERROR: One or more of the used functions
must be applied on at least one user created tables. Examples of user
table only functions are LISTAGG, MEDIAN, PERCENTILE_CONT, etc
Can You please help me on how can I achieve this?
listagg function is a compute-node only function.
But the query you run to get the table ddl runs only on leader because it only specifies pg_* tables.
According to AWS documentation
A query that references only catalog tables (tables with a PG prefix, such as PG_TABLE_DEF) or that does not reference any tables, runs exclusively on the leader node.
If a query that uses a compute-node function doesn't reference a user-defined table or Amazon Redshift system table returns the following error.
[Amazon] (500310) Invalid operation: One or more of the used functions must be applied on at least one user created table.
https://docs.aws.amazon.com/redshift/latest/dg/c_SQL_functions_compute_node_only.html
To sum up, since your query does not reference any user created table, you can not use listagg

Check whether field exists in SQLite without fetching them all

I am writing a database abstraction layer that also abstracts some of the different query types. One of them is called "field_exists" - its purpose should be pretty self-explanatory.
And I want to implement that for SQLite.
The problem I am having is that I need to use one query that either returns a row confirming that the field exists or none if it doesn't. Thus, I cannot use the PRAGMA approach.
So, what query can I use to check whether a field exists in SQLite, that fulfills the above criteria?
EDIT: I should add that the query needs to be able to run in PHP code (using PDO).
Also, the query should look something like this (which only works with MySQL):
SHOW COLUMNS FROM table LIKE 'field'
Trying to select a field that doesn't exist will return an exception, then you can catch it and return nothing.
Use the .schema TABLENAME command. It will tell you the command that was issued to create the table. For more info chekcout the SQLite command shell documentation.
If you don't have access to the sqlite command line, you can always query the sqlite_master table. Let's say you want to know the command used to create the table MyTable. You'd issue this:
select sql from sqlite_master where name='MyTable';
This then gives you the sql command that was used to create the table. Then just grep through that output and see if the column you're looking for is in the command used to create the table.
UPDATE 2:
Actually better than the sql I posted above, you can use this:
PRAGMA table_info(*table_name*)
This will show you all the columns in a given table along with their types and other info.

how to select a particular table from a storedprocedure which returns multiple table?

I have a storedproc which has multiple select statement.
When it is executed in sql server , it returns multiple tables.
I need a query which will select a particular table from the storedproc e.g. sp_help.
Please help.
If i have a look at this link, it seems that it is not possible.
Astander is right. From the number of tables available in ur SP, it is not possible directly.
However, you can apply some trick to accomplish your work. I am giving an example here. May be you can generate some idea based on this line.
SELECT * FROM sys.Tables where name =
'my_tbl'
As you can make out that I am filtering out the query by a table among all the tables available in my database.
Something of this sort may help you.
Else, if can get the dataset and then from that get the needed datatable from ur frontend code.