using search * operator in combination with 'database' function - azure-data-explorer

I have a source database & a target database. I want to execute .append command against the target database but source tables are in the source database. I know database('SourceDB') function we can use from the target database but I don't know how to use this in combination of search *. So what I need will look something like this:-
.set target_table <| search database("SourceDB").* | ..some logic
This is not working. Is there any workaround for this?

would something like this work?
.set target_table <| union database("SourceDB").* | distinct colA, colB, colC.
BTW, syntax-wise - using search (if you still require that) could look like:
search in (database("SourceDB").*) "something you're searching for"

Related

Sqlite: conditionally query two tables, one will not exist

I am sure I can solve this programmatically, but I am curious if I can do this in one query.
Context:
I will be querying multiple databases, some will have a table; 'table', others will instead have the table; 'table_v2'. I want to run the same SELECT statement on the table that exists. I know I can check if a table exists, but I want to know if I can do it all in one statement.
psuedo code summary of what I want to do in one statement:
if 'SELECT name FROM sqlite_master WHERE type='table' AND name=''table'; is not empty:
SELECT * FROM table;
else
SELECT * FROM table_v2;
I am beholden to constraints out of my control.
Thoughts:
Could I have the table name be a regex?
Can I run both SELECTS, ignore the failed result, and just return the success?
Generally, you can't do either. The query planner in SQLite needs to know the name of the table beforehand, and it must be valid so it can determine which paths to take.
You can use the loadable extension eval to build up the SQL query based off of the schema. Though, this exposes a variant of the same issue, since the query planner needs the table name, you need to build up the entire SQL statement, then run it, so you'll need two eval calls.
SELECT EVAL(
'SELECT * FROM ''' ||
EVAL('SELECT name FROM sqlite_master WHERE type=''table'' AND name IN (''table'', ''table_v2'');') ||
''';'
);
To use the eval function, you'll need to either build and load the extension as a library, or build it into your own custom build of SQLite itself.
Of course, I can't answer if you should do this.

Kusto command for generating create table & function script

Kusto explorer does allow scripting out functions and tables using the UI option "make command script". Is there any way we can use some sort of Kusto command to generate exactly the same output? Basically looking for command counterpart for clicking "make command script".
Continuing Yoni's answer, this should give you an executable command
.show table MyTable cslschema | project strcat(".create table ",TableName," (", Schema , ")")
project lets you select what to output (same as SELECT in sql)
strcat lets you concat string
.show table T cslschema (doc) should bring you close to that

Finding rows that start with a and b in SQLITE?

Im trying to find all rows that start with J or M in sqlite. For example in Microsoft Access I know you can use :
SELECT *
FROM Customers
WHERE cust_name LIKE '[JM]%';
However Im not sure what the equivalent of [] is in sqlite. I have
tried:
SELECT *
FROM Customers
WHERE cust_name LIKE 'J%' OR 'M%'
ORDER BY cust_contact;
But this only returns rows that start with J :/ Any help would be appreciated.
Or use GLOB instead of LIKE, but it is case sensitive and uses a different syntax.
WHERE cust_name GLOB '[JjMm]*'
LIKE is case sensitive unless options have been set otherwise, like PRAGMA case_sensitive_like = boolean;. Although case sensitivity can be set for LIKE, GLOB is always case sensitive.
Try the following code
SELECT * FROM Customers
WHERE cust_name LIKE 'J%' OR
cust_name LIKE 'M%'
ORDER BY cust_contact;

SQlite3 bound params and LIKE

Sqlite3 provides the sqlite3_bind_* functions which allow one to do parameter substitution into a SQL query. My question is: what is the right way to combine this with LIKE queries? For example, I might want to do:
SELECT * FROM thing WHERE name LIKE '%?'
but that doesn't work at all. Is the best way really just:
SELECT * FROM thing WHERE name LIKE ?
and then put the pattern characters into the actual string value to be substituted?
To concatenate strings, use the || operator:
SELECT * FROM thing WHERE name LIKE '%' || ?

preg_replace for changing sql query string

I have problems with sqlite3 and "num_rows". I cannot ask for num_rows of a result like mysql.Now i wan't to automate my SELECT query to ask for the rowcount.
Is there a way to replace all between two patterns?
Like
SELECT date,name,etc FROM myTable WHERE gender=1
// to
SELECT count(*) FROM myTable WHERE gender=1
I must replace all between SELECT and FROM with count(*).
Can i do this this preg_replace?
For your example provided it's simple, but I guess it won't work for all query syntaxes. But have it a try:
PHP code (don't know language you do use):
$count_sql = preg_replace('/^(SELECT)\\s+.*?\\s+(FROM.*)$/s', '$1 count(*) $2', $select_sql);
But I guess, there is a more elegant way to find the num_rows thing. You could use a subquery, like:
SELECT COUNT(*) FROM ( *** original query here *** )
It's not very efficient, but you don't have to deal with the query syntax.

Resources