snowflake: how to search users whose email is? - snowflake-cloud-data-platform

In snowflake how to select a user with email id
I am trying
show users;
select "email" from table(result_scan(last_query_id())) limit 1;
this shows "test#example.com"
then i try
show users;
select "email" from table(result_scan(last_query_id())) WHERE "email" = "test#example.com" limit 1;
i get
SQL compilation error: error line 1 at position 72 invalid identifier '"test#example.com"'
how to search for the email.. in users in snowflake

Please try with a single quoted value.
show users;
select "email" from table(result_scan(last_query_id())) WHERE "email" = 'test#example.com' limit 1;
Also, if you have access to ACCOUNT_USAGE, you could query the USERS-view there: https://docs.snowflake.com/en/sql-reference/account-usage/users.html

String literals should be wrapped with ':
SELECT "email"
FROM table(result_scan(last_query_id()))
WHERE "email" = 'test#example.com'
LIMIT 1;

Related

Query not running properly

I ran a query using google big query but I get an error as a result. I don't know what could be wrong.
SELECT
usertype,
CONCAT(start_station_name, " to ", end_station_name) AS route,
COUNT(*) as num_trips,
ROUND(AVG(CAST(tripduration AS INT64)/60),2) AS duration
FROM
`bigquery-public-data.new_york_citibike.citibike_stations`
GROUP BY
start_station_name, end_station_name, usertype
ORDER BY
num_trips DESC
LIMIT 10
You must be getting this error: Unrecognized name: usertype . As the error suggests bigquery-public-data.new_york_citibike.citibike_stations dataset doesn't have any column usertype.
As #samuel mentioned, usertype column is present in new_york_citibike.citibike_trips. Do cross check your dataset before querying.

SqlException: The multi-part identifier "chamin#gmail.com" could not be bound

When i try to execute the query in given bellow
is worked correctly and give the result
but when i change id into email then it will raise the exception
employee = connection.Query<Employee>("select id, email as email , password as password, role as role from Employee where email="+"chamin#gmail.com");
how do i fix this problem??
Strings have to be quoted in SQL :
employee = connection.Query<Employee>(
"select id, email as email , password as password, role as role from Employee where email='"+"chamin#gmail.com" + "'"
);
But your code is very permissive to SQL injection. You should use SQL parameters : it will prevent injection and avoid the issue you met.

Getting query results in Workbench , Not in apex class/Script

Background: I need access account records in the apex class where name contains %
For example, the query should return below accounts
abcd10%
cdcd40%abcd
10%volume
When I execute the below query I am getting results in the workbench
select id, name from Account where Name LIKE '%\%%'
the same query If I execute in Anonymous window/apex execute getting no results.
List accList = [select id,name from Account where Name LIKE '%\%%'];
system.debug('accList :::'+accList.size());
If we observe the below image, it is appending extra / in the query and giving wrong results.
Can you please help me how to resolve this?
16:00:21:002 SOQL_EXECUTE_BEGIN [1]|Aggregations:0|SELECT id, name FROM Account WHERE Name LIKE '%\%%'
You could use a bind variable or dynamic SOQL.
String searchText = '%\\%%';
List<Account> accs = [SELECT Id, Name
FROM Account
WHERE Name LIKE :searchText];
System.debug(accs.size());
System.debug(accs);
// Or in dynamic query (looks bit crazier because ' has to be escaped too)
List<Account> accs2 = Database.query('SELECT Id, Name FROM Account WHERE Name LIKE \'%\\%%\'');
System.debug(accs2);

SQL Server: Change the value text on queried results

I'm trying to return and change all entries in column login for each entry that the value = "Change this text" to "Requester". The below works but is only returns 2 values, "User" and "Requester" although there are 7 other entries where login = "Change this text"
declare #MainHospital varchar(50)='Hospital1';
SELECT CASE login
WHEN 'Change this text' THEN 'Requester'
ELSE 'User'
END
FROM Survey
WHERE MainHospital = #MainHospital
GROUP BY MainHospital, login
You are grouping by MainHospital & Login, so per hospital you only handle 2 cases. By grouping all of the results together, you are removing duplicate rows.
If you remove the grouping and it will return 7 rows:
SELECT CASE login
WHEN 'Change this text' THEN 'Requester'
ELSE 'User'
END
FROM Survey
WHERE MainHospital = #MainHospital
Otherwise include another unique column that will prevent the grouping.

Can't select User ID from Salesforce

I'm trying to select user's subordinates from Salesforce, but a simple query
SELECT Id FROM User WHERE ManagerId=xxxxxxxxx
returns bunch of null values, when I run
SELECT Id,Name FROM User WHERE ManagerId=xxxxxxxx
I get the correct names, still no IDs.
Is this a permission issue? I can't find anything when I login to portal.
I'm running the queries via API on Sandbox environment.
Try this (both works for me allways):
Id myId = [Select Id From User Where Username = 'myUserName'].Id;
System.debug('#### myId: ' + myId);
List<User> myIdList = [Select Id From User Where Username = 'myUserName' Limit 1];
System.debug('#### myId from list: ' + myIdList[0].Id);
Portal Licence doesn't allow to query User. However you have still access to the name of the user through OwnerId, CreatedById, LastModifiedById using in an inputfield.
i.e :
If you want to have access to user through the portal you need a custom object and synchronise your records with User by trigger.

Resources