Oracle userenv() alterative for snowflake - snowflake-cloud-data-platform

In oracle userenv('LANG') is used to get the language but not sure how to achieve the same in snowflake. tried select SYS_CONTEXT('USERENV', 'LANGUAGE') as well not working. please help me with this

I am not sure if it is the same thing as in Oracle, but Snowflake has a session parameter "LANGUAGE" applied to the current user's session.
You can get it as:
show parameters like 'LANGUAGE' in session;
Output:
if you need to extract just a value, you need to use the result_scan method to select the desired column:
select $2 as LANGUAGE from table(result_scan(last_query_id()));
Output:

Related

Can I look up the query id from a SnowSql session in the Snowflake Interface?

Is it possible to do the same thing using the SnowSql command line interface (CLI)? I'd like to view the SQL code for a particular query, as specified by its query ID, using the CLI.
When using the web console, one may go on the History tab and filter by "Query ID" e.g. "xxx-xxxxx" to view the SQL code and error messages (if any) for that particular query.
You can use LAST_QUERY_ID to retrieve the query IDs for queries in your session.
select last_query_id(); Gets the most Recent Query ID
select last_query_id(1); Gets the first Query ID of the session
select last_query_id(-2); Gets the Query ID from two queries ago.
etc.
Then you can use a query like this to get your actual Query Text if you need it.
SELECT QUERY_TEXT
FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY_BY_SESSION(RESULT_LIMIT => 10))
WHERE QUERY_ID = '018cde0c-0077-09ee-0000-001812d26346'
;
If you need to retrieve query information outside of your Session, I believe you can use ACCOUNT_USAGE if that works for you.
SELECT * FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY WHERE QUERY_ID = '018cde0c-0077-09ee-0000-001812d26346';
Please try this using QUERY_HISTORY* Tables:
SELECT query_id, query_text,user_name,session_id,role_name,warehouse_name,
execution_status,error_code,error_message,start_time,end_time
FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY(DATEADD('HOURS',-1,CURRENT_TIMESTAMP()),CURRENT_TIMESTAMP()))
ORDER BY start_time;
Reference : https://docs.snowflake.net/manuals/sql-reference/functions/query_history.html

Which Admin table has databse names in it in snowflake

I understand that we can do "show databases" in snowflake to get the details of databases. But is there any admin table which i can query.
Actually in my result set I need only database names.
There is an option to use RESULT_SCAN but i am trying to avoid the same.
please advise.
You can use INFORMATION_SCHEMA , and in particularINFORMATION_SCHEMA.DATABASES`, e.g.
SELECT * FROM INFORMATION_SCHEMA.DATABASES

Invalid object name / column after re-opening

I'm trying to get into SQL using Microsoft SQL Server Management Studio. I've added a database and I want to make a query:
This works and I can execute the code, but after I saved my query and closed the program it doesn't work anymore when I open the program and try to execute the query again. It can't find the terms I'm relating to:
I don't know why this occurs or how I can solve it, it seems that the connection between the query and the database is gone... Can someone help me on this?
You're attempting to execute the query against the master database but that's not where your tables are. Three ways to handle this:
Use the drop-down in the toolbar to switch to the dbArtemis database
Fully-qualify your table names. dbArtemis.dbo.Klantnummer for example
Execute use dbArtemis; in your query window before the query itself.
Just add before your query the name of your database:
USE dbArtemi
GO
SELECT Naam
FROM tblklaten
WHERE klatenummer =
(SELECT DISTINCT klatnummer FROM tblorders where (orderID = 11013));
GO

Calling oracle package from SSRS

I want to send the date range and employee ids from SSRS to Oracle package. Does anyone knows how to do it? Even if i try to declare an oracle package with three in parameters from_date, to_date and employee_id it works fine with one employee id. But fails if i select multiple employees in SSRS web interface saying wrong number of parameters. Does anyone know how to handle this?
Normally with SQL and SSRS you would do something like
Select * From Table where Table.Field in #Param
Oracle is different of course and depends on the Connection Type you are using.
ODBC connection you would use something like:
Select * From Table where Table.Field = ?
Order of the parameters is important here so be careful.
OLEDB connection you would use something like:
Select * From Table where Table.Field = :Param
However, none of the above work with multi selecting of the Parameters. You could do this:
=”Select * From Table where Table.Field in (‘” + Join(Parameters!parameter.Value,”‘, ‘”) + “‘)”
or
=”Select * From Table where Table.Field in(” + Join(Parameters!parameter.Value,”, “) + “)” if the values or your field is only numeric.
Here are a couple of good articles that you can look at. It has a better explanation than I can give personally without more information; Details of your Oracle Environment, Connection Type, etc.
Quick Reference:
Be sure your Oracle version is 9 or greater, none of this works on
versions 8 or older.
When using parameters with Oracle, use a : instead of an # sign –
:param instead of #param.
Add an ‘ALL’ option to your datasets that supply values for
multivalued drop down parameters.
Check for the ALL in your where clause by using “where ( field1 in (
:prmField1 ) or ‘ALL’ in ( :prmField1 ) )” syntax.
You can execute your query from the dataset window, but can only
supply 1 value. However that value can be ‘ALL’.
Educate your users on ‘ALL’ versus ‘(select all)’ .
Another good article about Multiple Parameters in SSRS with Oracle: Davos Collective Which is referenced in the top portion.
Hope this helps!

How to Pass output of one query as input to another query using SQL

Really appreciate if I can get any help on this
I have one sql query i.e query1 which displays the list of users who are not logged in to our application and another query "query2" that uses the ouput of query 1 and disables those user. Until now I had to manually run the two query individually, but now I have to automate the process.
My question is how can I pass the output of query1 as input to query2 without any manually work. I did some research and found out that this can be done using procedures and I need to create cursors as well. If this is true please let me know the steps on how to proceed further
I have Oracle database on the backend for our application. and we use SQL to retrieve the data
Thanks
victor
I'm not an Oracle guy, but this should work:
update tbl1
set IsEnabled = 1
where UserId in (select UserId from tbl2 where IsLoggedIn = 0)

Resources