How to correctly use in for string list in KQL - kql

the following code works
let names = dynamic(['Windows Installer', 'Software Protection']);
ConfigurationChange
| where Computer like "SRV"
| where SvcPreviousState == "Running"
| where SvcState == "Stopped"
// | where SvcDisplayName in (names)
| order by TimeGenerated
as commented out I would like to only check for a list of SvcDisplayName's.
According to the documentation this should work but does complain
: Failed to resolve table or column or scalar expression named 'names'
How would I correctly use in with a list for SvcDisplayName ?

The blank line is considered separator between queries, unless you select the whole code for execution.
See screenshots below.
Select the whole code for execution.
=> Valid query
Put the cursor on the query for execution.
There is no blank line after the let statement.
=> Valid query
Put the cursor on the query for execution.
There is a blank line after the let statement.
=> Invalid query
Please note how the query is marked by a pale blue color, but not the let statement

Related

table only record one character of my form field

One of my tables from pgsql has a strange behaviour like the title says.
Here are some screenshots:
As you can see, the name zamdam must be record in the column "nom" in pgadmin instead of recording one letter for one column, here is the sql statement + server code :
await client.queryObject("INSERT INTO users(nom,email,password,adresse,prenom) VALUES($1,$2,$3,$4,$5);",
product.fields.nom,
product.fields.email,
product.fields.password,
product.fields.adresse,
product.fields.prenom
);
let nomFormRegister = product.fields.nom;
console.log(nomFormRegister);
It was working well until I started to add some subtable behaviours between tables, but I deleted all of them and wrote new tables so I don't understand why this issue still appears ...
an other example where i logged in vscode all the fields that should be recorded in my table :
Form :
SQL statement + server code :
console.log :
PgAdmin table - fourth line:
Could you try to run queryArray instead of queryObject for insert operation?
await client.queryArray`INSERT INTO users(nom,email,password,adresse,prenom) VALUES(${product.fields.nom}, ${product.fields.email}, ${product.fields.password}, ${product.fields.adresse}, ${product.fields.prenom}`;
I am not sure if it's gonna work, because I've no deno environment to test it.

How do I evaluate value which potentially is not supplied?

I'm trying to make Azure monitor workbook with Azure Resource Graph (ARG) query as backend. There is dropdown presented to use to choose datetime value which is passed as parameter to ARG to filter results. It's expressed as {dateCreated:start} value in query. This works fine as long as there is something selected by user in dropdown, problem is that if nothing is selected in datetime field then condition essentially becomes coalesce(todatetime(),todatetime('2016-11-09T02:53:17.4582226Z')) which obviously fails validation logic since todatetime() expects something being passed to it. Is there something in KQL which will allow to overcome this?
Resources
| where type == 'microsoft.compute/virtualmachines'
| where tags['datecreated'] > coalesce(todatetime({dateCreated:start}),todatetime('2016-11-09T02:53:17.4582226Z') )
iff() and isempty() will do the trick for you:
Resources
| where type == 'microsoft.compute/virtualmachines'
| where tags['datecreated'] > iff(isempty({dateCreated:start}), todatetime('2016-11-09T02:53:17.4582226Z'), todatetime({dateCreated:start}))

In statement with parameters in Crystal Report Command object

I am trying to modify the code of an exsisting SQL report. This report uses a command object. Currently the SQl codes says the following:
WHERE **dept.definition_id ={?Measure}**
AND (({?Period Interval}=2
AND dept_qd_later.sum_facts_id IS NULL
AND dept_qd.DENOMINATOR_QUARTER IS NOT NULL)
OR ({?Period Interval}=3
AND dept_md_later.sum_facts_id IS NULL
AND dept_md.DENOMINATOR_MONTH IS NOT NULL))
LAST
I would like to modify the "Measure Parameter" to accept multiple values. I have updated the command parameter box to accept multiple values. I have tried to modify the SQL to the below in statement:
WHERE **dept.definition_id in({?Measure})**
AND (({?Period Interval}=2
AND dept_qd_later.sum_facts_id IS NULL
AND dept_qd.DENOMINATOR_QUARTER IS NOT NULL)
OR ({?Period Interval}=3
AND dept_md_later.sum_facts_id IS NULL
AND dept_md.DENOMINATOR_MONTH IS NOT NULL)))
LAST
However, when I try to insert multiple measure IDs, the report does not run. Can someone help me figure out what I am doing wrong? I am a Crystal novice so please respond in plain language. Thanks!
I think David is right. I tried something similar with a command object and it works when entering a comma separated list into the parameter prompt. Which means that you don't need to set the "allow multiple values" option on the parameter.
If the field were a string, you would have to enter the values with quotes as if they were part of the SQL statement.

The right way to prepare SQL statements with parametrized text search

Suddenly I've realized that while this works in groovy just like it is expeceted:
Sql.newInstance(connectionParams).rows("SELECT FROM ITEMS WHERE id = ?", [200])
this won't work
Sql.newInstance(connectionParams).rows("SELECT FROM ITEMS WHERE name LIKE '%?%'", ["some"])
All you can get is
Failed to execute: SELECT FROM ITEMS WHERE name LIKE '%?%' because:
The column index is out of range: 1, number of columns: 0.
My questions are:
Is it intentionally implemented this way? I've never needed to have a parametrized text search, so I'm not sure where this behaviour is typical or not.
How can I nevertheless safely parametrize statement with text search in it?
I believe you want to include the %'s in the parameter, like:
Sql.newInstance(connectionParams).rows("SELECT FROM ITEMS WHERE name LIKE ?", ["%some%"])

Printing Django QuerySet SQL with ""

You can print a queryset's SQL as follows:
print str(queryset.query)
however, for some reason this removes quotation marks, so you get:
SELECT `tableA`.`fieldA` FROM `fieldA` WHERE `tableA`.`fieldB` = Foo
instead of:
SELECT `tableA`.`fieldA` FROM `fieldA` WHERE `tableA`.`fieldB` = "Foo"
notice the missing ""
How can this be corrected?
If the underlying database is PostgreSQL you can do:
from django.db import connection
sql, params = queryset.query.sql_with_params()
cursor = connection.cursor()
cursor.mogrify(sql, params)
sql_with_params returns the plain query without any values substituted and the parameters that will be inserted in the query.
It is still not recommended to use .mogrify() for other purposes than debugging because the method may disappear in the future.
If you want to execute the query, you can/should just use .raw().
YourModel.objects.raw(sql, params)
not quite what you want, but if you have DEBUG = True you can use
from django.db import connection
connection.queries
update:
looking at the Queryset __str__ method:
__str__(self)
| Returns the query as a string of SQL with the parameter values
| substituted in.
|
| Parameter values won't necessarily be quoted correctly, since that is
| done by the database interface at execution time.
If this is for debug purpose you should look into django-debug-toolbar that will show you all queries ran for any view you're looking at