Automate SQL Script with Multiple Statement Types - sql

I have a SQL script that works exactly as I want it to when I step through it line by line. At first, this was a viable option, but the script has gotten larger over time and stepping through it now sucks.
I've tried to run the script as part of an SSIS package in Visual Studio, but that gave me weird results.
I have a feeling this is because I have mixed language in the script, (i.e multiple UPDATE, ALTER, SELECT INTO, statements) ... Is there a way to automate this script so it runs line by line as if I were stepping through it?
be kind. and thank you!

Generally speaking. All you need is a semi-colon after each statement, this is regardless of statement types. "Go" is only needed if you want to separate it into batches (although it might not work if you are passing the code through to something else, ie. ADO.NET command object). This is helpful if you want the code to continue running regardless of the success or failure of the code before the "go". If you want the continuation of the code to depend upon the previous code just make sure to end each statement with a ";"

Related

Scripting Language with "edit and continue" or "hot swap" support? ( Maybe possible in Lua?)

I am making my existing .Net Application Scriptable for non programming users. I added lua, it works like a charm. Then I added debug functionality(pause/continue/step) via debug.sethook. It works also like a charm.
Now I realize that my Application needs edit and continue feature like Visual Studio has it. You pause the execution can edit the code and then continue from your current state with changes applied. This feature is very important to me. I thought this would be easy to do for scripting languages.
Everywhere I read that scripting languages can do this. But even after spending hours of searching I haven't found an Lua implementation yet. It hasn't to be Lua but hot swapping code in Lua would be my first choice.
How can the ability for the user be offered to pause and edit the script and than continue the execution with changes applied?
NOTE: It doesn't have to be Lua every scripting language would be okay
Update
#Schollii
Here is an example:
function doOnX()
if getValue() == "200" then
value = getCalculation()
doSomething() -- many function calls, each can take about 2s
doSomething()
doSomething()
print(value)
doX(value)
end
end
doOnX()
Thank you for your suggestions. This is how it might work:
I will use https://github.com/frabert/NetLua Its a very cool, well written 100% C# Lua Interpreter. It is generating an AST tree first and then directly executing it.
The parser needs to be modified. In Parser.cs public Ast.Block ParseString(string Chunk) there is a parseTree generated first. parseTree.tokens[i].locations are containing the exact position of each token. The Irony.Parsing.ParseTree is then parsed again and is converted to a NetLua.Ast.Block but the location information is missed. I will need to change that so later I will know which Statement is in which line.
Now each Statement from the AST tree is directly executed via EvalBlock. Debug functionality (like I have in my C Binding lua Interpreter DynamicLua via debug.setHook) needs to be added. This can be done in LuaInterpreter.cs internal static LuaArguments EvalBlock(`. Pause/continue/step functions should be no problem. I also can now add current line Highlighting because each statement contains position line information.
When the execution is paused and the code was edited the current LuaContxct is saved. It contains all variables. Also the last Statement with the last execution line is saved.
Now the code String is parsed again to a new AST tree. It gets executed. But all statements are skipped until the saved statement with the line statement is reached. The saved LuaContext is restored and execution can continue with all changes applied.
New variables could be also added after the last executed line, because a new NetLua.Ast.Assignment Statement could just add a new variable to the current LuaContext and everything should just work fine.
Will this work?
I think this is quite challenging and triicky to do right.
Probably the only way you could do that is if you recompile the chunk of code completely. In a function this would mean the whole function regardless of where edit is in function. Then call the function again. Clearly the function must be re-entrant else its side effects (like having incremented a global or upvalue) would have to be undone which isn't possible. If it is not reentrant it will still work just not give expected results (for example if the function increments a global variable by 1 calling it again will result in the global variable having been increased by 2 once the function finally returns).
But finding the lines in the script where the chunknstarts and ends would be tricky if truly generic solution. For specific solution you would have to post specific examples of scripts you want to run and examples of lines you would want to edit. If the whole user script gets recompiled and rerun then this is not a problem, but the side effects is still an issue, examples could help there too.

Best way to write new Pig script and debug it along

I usually find it difficult to locate the bug in Pig script, so I don't write bunch of lines and then test them.
While writing a new pig script, I usually keep my grunt shell open and keep executing every statement, finding describe and explain along the way. This gives me idea of what current relation looks like and how to extract/add fields from/in that relation. But this is time consuming, as pig executes from start for Nth statement, and then again for N+1th statement.
I wanted to know how people usually write a new script and debug/execute along the way.
Is there a way where Pig grunt shell simply executes last statement instead of executing from start?
We can't debug while it is running. Pig runs from first command whenever we start the script. That is why we call it as data flow language. It runs every statement sequentially.

How do I compile Oracle SQL scripts?

I have a series of SQL files, and I want to compile all of them. If do them manually by using benthic software, it takes a long time. I tried using TOAD, but I don't like to use cracked software. Can you help me execute a SQL file? I want to write a program to do some things for me.
From sql/plus I tried to create batch file but in some of my sql file developer used "/" and ";" so it caused sql/plus suddenly stopping the compilation. Please advise or recommend free software to help.
"I want apply the sql package , function and if they are invalid compile them again"
I am using oracle 10g.
Thanks
If you search for something like TOAD, try SQL Developer, a free tool from Oracle.
If you want to recompile existing source in your database, you can use dbms_utility.compile_schema.
If you try to install several files in a batch, SQL*Plus should work for you. Make sure that your files have the right format.
It sounds like you need to run a large sql script. Correct? Sql/Plus should work, but if you want a free IDE, I recommend SQL Developer. It isn't perfect, but it is one of the better free solutions.
"in some of my sql file developer used "/" and ";" "
You need to consistently use these to have any hope of using a tool to deploy. You don't want to have to use a GUI to deploy, so SQL*Plus is the standard to aim for. A good Oracle GUI should be able to run a SQL*Plus deployment script.
I generally start with SET DEFINE OFF otherwise unexpected ampersands (&) cause issues.
Do some basic grepping - any script with a CREATE PACKAGE, CREATE PROCEDURE, CREATE TRIGGER or CREATE TYPE (including CREATE OR REPLACE) should have a the "/" to execute that statement. If they don't, fix them.
For a first run though, I'd take them in batches of 10, until I was sure that they executed correctly. I wouldn't worry about compilation errors as long as they load. Afterwards, you can do a recompile of any invalid objects in the schema.
Then check USER_ERRORS to see what is still broken.

Convert vbscript snippets to vb.net

We have a bunch of vbscript snippets that are stored in a database. They are created by our users and are used during some complex calculations.
We are using the microsoft scriptcontrol to execute them. As we are switching to 64bit applications we cannot use the scriptcontrol anymore and therefore we are going to start using CodeDom and vb.net instead.
The problem is that we still need to support all those legacy vbscripts until they have been converted to vb.net scripts.
The scripts only contain simple functions taking arbitary number of parameters and do some caluclations on them. As I'm a C# developer I do not have that much experience with vbscript contra vb.net syntax.
Is it easy to convert vbscript code to vb.net (using regex or similar)? Got any pointers or things that I should think of? Or should I just wait until all scripts have been converted by the users (may take a while)?
If you have Option Explicit Off in VB.Net, quite a lot of vbscript code will be ok, but one problem you'd have is that in VB.Net you can't just execute a script by itself, so even if the code might work without any conversion you might not be able to run them in the same way since you'll need to compile them into executables before you can run them. If each script can be executed indepentenly of each other, then you'll either need to compile one executable per script, or have one master executable with a big Select Case in there to call the relevant code depending on command line parameters.
I'd suggest that it might be worth waiting for the users to convert them though and also keeping Option Explicit On and letting the users go through the scripts and add datatypes and similar, since it's quite possible that might find quite a few bugs in your scripts.

Check sql script valid

As part of a release we run a load of PL/SQL scripts against a database. Recently someone left the ; off the end of a line in one script that was called another script so this meant that script did not get run. Because this did not cause an error, it just didn't get run, it took quite a while to track down what had happened.
I want to check the scripts before they are run for lines in them that are missing either a ; at the end or a / on the line after. This is made more complicated as 'lines' in the script could actually span more than one line if it is statement or block of code.
To me this seems like to do this I'm going to have to parse the scripts then check they meet the above.
I've found ANTLR and wonder if this might be a way to do it since there seem to be existing PL/SQL grammars but looks like that's going to be a step learning curve for what's just a simple check.
Does anyone know an easy way or any other tools, eclipse plugins etc that I can use to check for lines in the scripts that are missing either a ; at the end or a / on the line after?
Update
We already do most of the stuff Tom H suggested. The scripts are run into our test server and we have a version table that gets updated at the end. The problem was that the missing semi-colon in the container script meant one script did not get run but the rest including the one to update the version number ran without errors. Therefore the problem only got picked up quite a way into testing. This needed the database restored before running the scripts with the missing semi-colon added so basically resulted in half a day of testing time being lost. If there was a simple way to check this before running the scripts into the test server it could save quite a bit of time.
I agree with MattH that you may be going about this the wrong way. I would just add an insert statement to the end of all of your scripts which insert a "version" row into a table in the database. At the end of your deployment scripts it's then an easy task to check that the version table has all of the correct rows in it.
Also, you should have all of your release scripts being run exactly as they will be in production against your QA server. That's where all of the testing takes place. You never do anything to the server besides what is in your release steps - you only run the release scripts and if those release scripts are ever changed then you refresh the QA server with them and redo testing.
When you go to production your release process has then been fully tested. As a fail safe measure you can also use tools like Red Gate's SQL Compare and SQL Data Compare to check that production matches the QA server. The data compare would only be against certain tables (look-up tables, etc.). If you have data changes to major tables (1M rows, etc.) then you can right a custom script to check that they are correct.
Even if the scripts are different for every release (and not part of a defined source control structure that creates or replaces database objects) I would adopt a practice of breaking the scripts down into the most fundamental units of work per file and deploying them through Ant with the standard sql task. You probably have these types of scripts:
CREATE or REPLACE dbobject...
SQL DML scripts
Anonymous PL/SQL blocks
If you standardize on a consistent statement delimiter (I suggest using "/" since it works with all of the cases above) and set the deployment to fail on error, then Ant will either deploy all of the files or indicate why it couldn't.
I think it would be very difficult to otherwise parse files of one or more SQL and/or PLSQL statements and find missing delimiters if there are no standards on delimiter choice or statements per file.
Just a thought, but are you going about this the wrong way?
I assume, at the file-level, the lack of a semi colon in the file was not a problem? but it only became a problem when run via the batch processing? If that's the case maybe you can change your batch processing to cope with this.
If it was the file, then testing should have picked it up. You don't want to parse your input files to make sure they compile etc.