Snowflake: query_history_by_user() does not yield expected result - snowflake-cloud-data-platform

Snowflake provides the QUERY_HISTORY_BY_USER() function to actually see the queries from any user. I'm using this command for querying:
select * from table (
<DATABASE>.<SCHEMA>.query_history_by_user(
USER_NAME=>'myUser',
END_TIME_RANGE_START=>to_timestamp_ltz('2021-11-24 06:00:00 -0100'),
END_TIME_RANGE_END=>to_timestamp_ltz('2021-11-24 17:00:00 -0100'),
RESULT_LIMIT=>100)
);
The problem is that this query does not yield any results. However, if I start the same query without the USER_NAME=>'myUser' statement - which is semantically the same, since the executing user is taken as a default - I do get the expected result. The result for a different user than mine (i.e. USER_NAME=>'differentUser') is also empty. All queries are executed as ACCOUNTADMIN, so rights are presumably not the problem.
So the question is: why does the query work without any user provided but does not work with a user specified?
Any help is really appreciated!

I sugest wrapping user name with ": USER_NAME=>'differentUser' => USER_NAME=>'"differentUser"'
QUERY_HISTORY_BY_USER
A string specifying a user login name or CURRENT_USER. Only queries run by the specified user are returned. Note that the login name must be enclosed in single quotes.
Also, if the login name contains any spaces, mixed-case characters, or special characters, the name must be double-quoted within the single quotes (e.g. '"User 1"' vs 'user1').

Related

SSRS How to see multi value parameter in subscription

I tried to get value from query or to specify values, as soon as the parameter is multi value i can't see the data when i'm trying to make my subscription.
my request looks like :
select id from employee where canal in(#canal)
what should i do, i'm totally stuck,
when i did research i saw data driven subscription but i don't have access to it apparently, don't know if that help
I'll start by saying sorry this isn't a pleasant answer. You've run into a limitation with the built-in functionality. Thankfully there are workarounds.
The problem is that you can only pass 1 value into the data-driven subscription. So you have use a comma-separated list and get the query/report to parse out the values.
If you have or can create a Split function in your database, that is a good option. This would be a table-valued user defined function and there are some easy to find examples already. Also this function is generally good to have for other use cases anyway. With this your SQL would read:
where canal in Split(#canal)
SSRS works really well with SQL Server, but when you use an ODBC connection, the parameter support is limited. You can use the same multi-value parameter workaround that is required in those cases.
In the Dataset properties > parameters tab, use an expression like this to combine the values into a single comma-separated string surrounded by commas.
="," + Join(Parameters!canal.Value, ",") + ","
The SQL would look like this:
where # like '%,' + canal + ',%'
Basically, this searches row-by-row for values that are contained in the string.
In either case, the query in your data-driven subscription settings will need to return the comma-separated string. Then you can select that column in the report parameters value field. Hope this helps!

Replace is not working for weird character

I use UPDATE a SET GR_P = REPLACE(GR_P,'','') FROM mytable a to replace things.
But replace function is not working for below charter:
In Query analyzer it works but when I used SSIS Execute SQL task or OLEDB Source then it is giving me error:
No Connection manager is specified.
In Toad against Oracle (since that's one of your tags), I issued this (pressing ALT-12 to get the female symbol) and got 191 as a result. note selecting it back using CHR(191) shows an upside-down question mark though.
select ascii('♀') from dual;
Given that, this worked but it's Oracle syntax, your mileage may vary.
UPDATE mytable SET GR_P = REPLACE(GR_P, CHR(191));
Note if it does not work, that symbol could be for another control character. You may need to use a regular expression to eliminate all characters not in a-zA-Z0-9, etc. I suspect you'll need to update your tags to get a more accurate answer.
Maybe this info will help anyway. Please post back what you find out.

WHERE clause when defining last logon time with wildcard in SQL

I did search but could not see anything relating to my question:
SELECT
vSMS_R_System.Netbios_Name0 AS 'Name',
vSMS_R_System.Distinguished_Name0 AS 'LDAP',
vSMS_R_System.Operating_System_Name_and0 AS 'OS Version',
vSMS_R_System.Last_Logon_Timestamp0 AS 'Last Logon Time',
vSMS_R_System.Active0 AS 'Active State'
FROM
vSMS_R_System
WHERE
Distinguished_Name0 LIKE '%Site Servers%'
AND Operating_System_Name_and0 LIKE 'Microsoft Windows NT Server 6.%'
AND Last_Logon_Timestamp0 LIKE '%2017-02%'
When executing this query, no results are displayed, however removing the last line will execute the results without issues. It seems to be the wildcard for Last_Logon_Timestamp0 that's an issue here.
I've amended the last line of the query to the below:
AND Last_Logon_Timestamp0 LIKE '%2017%'
This displays the results I need. So it seems to be the hyphen causing problems when declaring the wildcard value in my original query.
Does this information needed to be handled differently? The hyphen doesn't appear to be an illegal character, but it does appear to be preventing the information from being displayed.
Assuming that last_logon_timestamp0 is some sort of datetime, then you should be using date/time functions, NOT string functions. So:
SELECT s.Netbios_Name0 AS Name,
s.Distinguished_Name0 AS LDAP,
s.Operating_System_Name_and0 AS [OS Version],
s.Last_Logon_Timestamp0 AS [Last Logon Time],
s.Active0 AS [Active State]
FROM vSMS_R_System s
WHERE s.Distinguished_Name0 LIKE '%Site Servers%' AND
s.Operating_System_Name_and0 LIKE 'Microsoft Windows NT Server 6.%' AND
s.Last_Logon_Timestamp0 >= '2017-02-01' AND
s.Last_Logon_Timestamp0 < '2017-03-01' ;
Notes:
Your code converts the column to the local date/time representation, using whatever internationalization settings happen to be set.
A time comparison allows the database to use indexes on the column, if they are available.
Table aliases make the query easier to write and to read.
Don't use single quotes for column names, because they are easily confused for string constants. They should only be used for string and date constants.
Try to avoid column aliases with spaces and other non-standard variables, so you don't need to escape the names.

Use String parameter for RegEx in query

In my query (the database is a sql server) I use a RegEx for a select command like this:
SELECT * FROM test WHERE id LIKE '1[2,3]'
(This query is tested and returns the data I want)
I want to use a paramter for this RegEx. For that I definded the Paramter in iReport $P{id} as a string and the value is "1[2,3]".
In my query I use now this parameter like this:
SELECT * FROM test WHERE id LIKE $P{id}
As result I get a blank page. I think the problem is that the value of the parameter is defined with " ". But with ' ' I get a compiler error that the paramter isn't a string.
I hope someone can help me.
LIKE applies to text values, not to numeric values. Since id is numeric use something like this:
SELECT * FROM test WHERE id IN (12, 13)
with the parameter
SELECT * FROM test WHERE id IN ($P!{id_list})
and supply a comma separated list of ids for the parameter. The bang (!) makes sure that the parameter will be inserted as-is, without string delimiters.
Btw: LIKE (Transact-SQL) uses wildcards, not regex.
You can still use LIKE since there exists an implicit conversion from numeric types to text in T-SQL, but this will result in a (table or index) scan, where as the IN clause can take advantage of indexes.
The accepted answer works but it is using String replacement, read more about sql-injection, to understand why this is not good practice.
The correct way to execute this IN query in jasper report (using prepared statement) is:
SELECT * FROM test WHERE $X{IN, id, id_list}
For more information as the use of NOTIN, BETWEEN ecc. see JasperReports sample reference for query

Detect when (Select All) is checked for multi value parameter

I have a report with a multi-valued parameter on it. I'm looking to output the selected values which is accomplished with Join(Parameters!State.Label,",")
Every solution I've found on the web indicates I should use something like the following to detect when the (Select All) "value" is selected.
E.g. expression for the text box on the header should be:
="State: " & IIF(countrows("prc_prompt_state").Equals(Parameters!State.Count),"(All)",join(Parameters!State.Label,","))
CountRows() tells me the total number of parameters available, e.g. 8 states in Australia. Parameters!State.Count is supposed to tell me how many are actually selected by the user. However this always reports the full value (8 in this case) regardless of how many are selected. This is in agreement with the official docs (https://technet.microsoft.com/en-us/library/aa337293(v=sql.100).aspx), but NOT in agreement with every single search result I come up with on how to solve this problem.
So how can I rewrite this expression so I can find out when (Select All) is/isn't checked? I'm using report builder 3, which I believe is based on the 2008 edition - we deploy to Azure, but I haven't got that far yet.
Examples of questions whose answers seem to be wrong:
Displaying Multi-Value Parameters
SSRS: Can I know if user selected "ALL" in multivalued param?
This is old, but google found it for me, and then I figured out an answer on my own that worked. (I was using a list of users.)
I created a separate dataset that returns a count of all available options in the default parameter lookup (username). Then, I assigned that as a default value to an internal parameter. (UserCount) This worked as a text expression:
=Microsoft.VisualBasic.Interaction.IIF(Parameters!username.Count = Parameters!UserCount.Value, "All Selected", Microsoft.VisualBasic.Strings.JOIN(Parameters!username.Value, ", "))

Resources