Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute large script on SQL server #231

Closed
chbian opened this issue May 7, 2019 · 2 comments
Closed

Execute large script on SQL server #231

chbian opened this issue May 7, 2019 · 2 comments

Comments

@chbian
Copy link

chbian commented May 7, 2019

I was trying to execute a fairly large piece of script on SQL server. The script creates temporary tables and goes like the follows

if OBJECT_ID('tempdb..#table_step1') is null
begin
	select
		o1.*, o2.f3, o2.f4
	into	#table_step1
	from	original1 o1
        join original2 o2
        on o1.f1 = o2.f1
	where o1.f2>o2.f2
          and etc etc
	;
end;

if OBJECT_ID('tempdb..#table_step2') is null
begin
	select
		s1.*, o3.f3, etc
	into	#table_step2
	from	#table_step1 s1
        join original3 o3
        on s1.f1 = o3.f1
	where o1.f2>o2.f2
          and etc etc
	;
end;

/*
more temp tables and steps
*/

drop table if exists #final_table;
select
	etc
into	#final_table
from	#table_stepN sN
where  etc
;

I use ODBC.execute! to send the above script to SQL server. What I found is that, if none of the temp tables #table_stepN exists, the script runs fine. However, if any of the temp tables are already there the script fails to generate the final table. It could be time consuming to create some of the temp tables. So it is economical to leave the temp tables on the server and reuse them.

How should I change the script to make it work? Or maybe I should split the script and execute a smaller piece each time. Any ideas or suggestions? Thank you!

@chbian
Copy link
Author

chbian commented May 7, 2019

By the way, I don't get any error message from julia/ODBC. It seems SQL server simple skips the script without running it if any of the if statement wasn't true.

@quinnj
Copy link
Member

quinnj commented May 23, 2020

This seems like a problem specific to how you're using SQL server; most of the times, databases will support statements like create table if not exists ... to allow running idempotent create table statements.

@quinnj quinnj closed this as completed May 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants