Set Datefirst in read.jdbc SparkR - r

I'm querying an Azure-SQL-database from Databricks using SparkR's read.jdbc function.
I have no issues sending queries to the database, but, I would like to set the beginning of the week on Sunday using SET DATEFIRST 7; and I cannot find a way to do so.
I have read the answers to this question and I could use some of them as a workaround. However, I would like to know if there is a way to set the DATEFIRST from Databricks (I don't mind using python or Scala to do it).
My piece of code looks like:
query <- "SET DATEFIRST 7;
(SELECT
DATEPART(yyyy,[calday]) * 100 + DATEPART(WEEK,[calday]) as calyearweek
FROM [dbo].[table]) out"
table <-
read.jdbc(
url = jdbcUrl,
database = jdbcDatabase ,
tableName = query,
user = user,
password = password
)
If I erase SET DATEFIRST 7; I get the query results.
If I keep it I receive the following error:
Error in jdbc : com.microsoft.sqlserver.jdbc.SQLServerException:
Incorrect syntax near the keyword 'SET'

7 is the default in SQL Azure as you can confirm with ##DATEFIRST function. You don't have to set DATEFIRST to 7.
SELECT ##DATEFIRST;
If you need set different values for DATEFIRST try to set it from inside a stored procedure.

Related

Need to get data from a table using database link where database name is dynamic

I am working on a system where I need to create a view.I have two databases
1.CDR_DB
2.EMS_DB
I want to create the view on the EMS_DB using table from CDR_DB. This I am trying to do via dblink.
The dblink is created at the runtime, i.e. DB Name is decided at the time user installs the database, based on the dbname dblink is decided.
My issue is I am trying to create a query like below to create a view from a table which name is decided at run time. Please see below query :
select count(*)
from (SELECT CONCAT('cdr_log#', alias) db_name
FROM ems_dbs a,
cdr_manager b
WHERE a.db_type = 'CDR'
and a.ems_db_id = b.cdr_db_id
and b.op_state = 4 ) db_name;
In this query cdr_log#"db_name" is the runtime table name(db_name get's created at runtime).
When I'm trying to run above query, I'm not getting the desired result. The result of the above query is '1'.
When running only the sub-query from the above query :
SELECT CONCAT('cdr_log#', alias) db_name
FROM ems_dbs a,
cdr_manager b
WHERE a.db_type = 'CDR'
and a.ems_db_id = b.cdr_db_id
and b.op_state = 4;
i'm getting the desired result, i.e. cdr_log#cdrdb01
but when i'm trying to run the full query, getting result as '1'.
Also, when i'm trying to run as
select count(*) from cdr_log#cdrdb01;
I'm getting the result as '24' which is correct.
Expected Result is that I should get the same output similar to the query :
select count(*) from cdr_log#cdrdb01;
---24
But the desired result is coming as '1' using the full query mentioned initially.
Please let me know a way to solve the above problem. I found a way to do it via a procedure, but i'm not sure how can I invoke this procedure.
Can this be done as part of sub query as I have used above?
You're not going to be able to create a view that will dynamically reference an object over a database link unless you do something like create a pipelined table function that builds the SQL dynamically.
If the database link is created and named dynamically at installation time, it would probably make the most sense to create any objects that depend on the database link (such as the view) at installation time too. Dynamic SQL tends to be much harder to write, maintain, and debug than static SQL so it would make sense to minimize the amount of dynamic SQL you need. If you can dynamically create the view at installation time, that's likely the easiest option. Even better than directly referencing the remote object in the view, particularly if there are multiple objects that need to reference the remote object, would probably be to have the view reference a synonym and create the synonym at install time. Something like
create synonym cdr_log_remote
for cdr#<<dblink name>>
create or replace view view_name
as
select *
from cdr_log_remote;
If you don't want to create the synonym/ view at installation time, you'd need to use dynamic SQL to reference the remote object. You can't use dynamic SQL as the SELECT statement in a view so you'd need to do something like have a view reference a pipelined table function that invokes dynamic SQL to call the remote object. That's a fair amount of work but it would look something like this
-- Define an object that has the same set of columns as the remote object
create type typ_cdr_log as object (
col1 number,
col2 varchar2(100)
);
create type tbl_cdr_log as table of typ_cdr_log;
create or replace function getAllCDRLog
return tbl_cdr_log
pipelined
is
l_rows typ_cdr_log;
l_sql varchar(1000);
l_dblink_name varchar(100);
begin
SELECT alias db_name
INTO l_dblink_name
FROM ems_dbs a,
cdr_manager b
WHERE a.db_type = 'CDR'
and a.ems_db_id = b.cdr_db_id
and b.op_state = 4;
l_sql := 'SELECT col1, col2 FROM cdr_log#' || l_dblink_name;
execute immediate l_sql
bulk collect into l_rows;
for i in 1 .. l_rows.count
loop
pipe row( l_rows(i) );
end loop;
return;
end;
create or replace view view_name
as
select *
from table( getAllCDRLog );
Note that this will not be a particularly efficient way to structure things if there are a large number of rows in the remote table since it reads all the rows into memory before starting to return them back to the caller. There are plenty of ways to make the pipelined table function more efficient but they'll tend to make the code more complicated.

MYSQL syntax for if with data stored in the database

i want to create in mysql an event schedule every day that check
if the current date is greater than a date stored in the database table and then call some store procedures.
Reading my WRONG mysql code you will understand that i want to do:
delimiter $
set global event_scheduler = on$
create event if not exists `end_qualifications`
on schedule
every day
do
begin
if curdate() >= (select `end_date` from `round` where `nome` =
"qualifications")
then
/* call myprocedure(params); */
end if;
end $
delimiter ;
I found here If-statement in the MySQL stored procedure for selecting data something similar, but there is not way my code work as i want.
I'm a beginner with mysql so it's possible that what i want to do can't be done.
Anyone knows how to make my code work?
I'm using MySQL client version: 5.7.25
EDITED: this is the error i get when i try to run the query
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'do
begin
if curdate() >= (select `end_date` from `round` where `nome` =
"q' at line 5
I don't know if it matters, but the database is empty for now.

ODBC Error "Column x in table y has value exceeding its max length or precision"

I get this error from Progress database when running the following query using ODBC:
SELECT distinct Table.column,
{ fn CONVERT(SUBSTRING(Table.ProblematicColumn, 1, 60), SQL_VARCHAR)} as test
FROM PUB.Table
WHERE ( Table.id IN (
SELECT Table.id
FROM PUB.Table
) )
I know it's possible to fix it using the DBTools. However, I run queries against multiple Progress databases of multiple clients, so it's not practical to do this every time.
Also, for some reason, the ODBC client I'm using (PHP), doesn't show any error when this happens. Instead, it returns an empty result.
The convert I did to a VAR_CHAR of 60 character did help until I added the sub-query. When the sub-query is there, I get again the same error.
Interestingly enough, when the 'distinct' is not there, it's working. But I do need the distinct.
Edit: The question is how can I execute this query without fixing the width column with DBTool.
It took a few minutes to find an answer. The problem appears to be in the OE10 SQL broker not handling the sub select in the where clause. This alternative using an inner join to a sub select looks to be equivalent to me. I tested it, it does work. Replacing the SQL client will do nothing, the error occurs in the OpenEdge SQL broker: I get the same error using the OpenEdge JDBC driver.
SELECT distinct Table.column,
{ fn CONVERT(SUBSTRING(Table.ProblematicColumn, 1, 60), SQL_VARCHAR)} as test
FROM PUB.Table inner join (select id from PUB.Table) t2 on Table.id = t2.id
Upgrade to OE 11.6.
There are options in 11.6 to automatically and silently truncate the data so that you will not get an error.
"Autonomous Schema Update"
https://community.progress.com/community_groups/openedge_rdbms/f/18/t/19534

asp:SqlDataSource - Debugging a parameterized query

Is there any way to see what the SQL looks like after the parameters are resolved?
For example here is a small part of my SQL:
([Event].[Start_Time] LIKE #StartTimeValue)
And my parm:
SqlDataSourceObject.SelectParameters.Add("StartTimeValue", TypeCode.DateTime, StartTimeValue)
But what does the final SQL look like when the parm #StartTimeValue is replaced with the value in StartTimeValue?
How can I see that?
Thanks for your help.
Do you have access to the database server? From there you could run a tool like SQL Profiler.
Another way is to set a break point just before the query is executed and examine the variables that went in. Usually the issue lies somewhere with the variables you're passing in (they are null, etc) and not with the resolved query itself. You could also set it up in a SQL query window like so:
-- Declare the variable to be used.
DECLARE #StartTimeValue datetime;
-- Initialize the variable.
SET #StartTimeValue = '<PASTE VARIABLE VALUE YOU GOT FROM DEBUGGING HERE>';
SELECT * FROM [Event] WHERE ([Event].[Start_Time] LIKE #StartTimeValue);

Using Limit and offset in Sqlite update statmet

update table set column_name limit 3 offset 2;
The above query is not working.
Throws error
sql error: syntax error near 'limit'.
An UPDATE statement expects a new value after the column_name, like this:
update thetable set column_name = 'some new value'
Furthermore, the documentation mentions that you need to have compiled SQLite with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT option, which is not enabled by default.
Sqlite does not allow the use of LIMIT and OFFSET statements like in MYSQL. You will have to use a nested query to workaround it . Or use two queries.

Resources