Operations `CREATE TRIGGER` and `CREATE PROCEDURE` in ANSI SQL - stored-procedures

I need to know a syntax of CREATE TRIGGER and CREATE PROCEDURE operations in ANSI SQL standard. But as I know each databases use a custom SQL syntax in some details, so I don't know that is from all variants is ANSI SQL. And I can't find ANSI SQL standard docs (in any case in public access).
Can someone tells me where I can find it? Or which database uses a correct syntax to read its docs?
P.S. I am a nobie in SQL just I need it for my exams. At work I used PostgreSQL always but I noticed that it is not same like ANSI SQL.

Related

Reader macro vs a normal parser

I am in the process of implementing a program that applies some transformations to SQL code.
When it comes to parsing said code, I thought about two approaches.
Implementing a "standard" parser using ordinary functions
Implementing a Reader macro that does such interpretation at read-time.
I'd like to know if implementing a reader macro is valid for this case or I'm better off writing it with usual functions and avoid killing a insect with a bazooka.
You can use a reader macro as a way to inline SQL code if you want, like
#[SQL code here]
having that call a function like (sql "SQL code here"), but you won't be able to do something as complex as writing a full SQL interpreter with reader macros. Besides, how could you access the database at runtime (when you will probably need it) if you did all that at read time?
Another approach is to create a lispy SQL DSL, where you could use regular functions and macros.

Reading sql query and executing from file in db2 stored procedures

I am new to db2 stored procedures.
I am looking to read a SQL file as a parameter and looking to execute that SQL query to get the records.
Can anyone help me with this?
thanks in advance.
Use utl_file procedure to read file and build sql string. Use dynamic sql to execute sql string. Check following links for more details.
utl_file:
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.7.0/com.ibm.db2.luw.sql.rtn.doc/doc/r0053697.html
dynamic sql:
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.7.0/com.ibm.db2.luw.apdv.plsql.doc/doc/c0053894.html

Server opens Stored Procedure like Unicode

As you can see in the image bellow, my SQL Stored Procedures, somehow, my SQL Server opens Procedures like Unicode SPs.
That was not the case before, and I have no idea how this apeared now.
I have around 5.000 stored procedures so there is no chance I can edit it manualy.
My SPs starts from ALTER PROCEDURE , everything before that is somehow added.
Your SP's will still work just fine. This is just the way SQL Server Management Studio scripts the objects when you want to generate ALTER- or CREATE-statements.
To change this behavior, go to Tools > Options > SQL Server Object Explorer > Scripting
Set the option "Include IF NOT EXISTS clause" to "False".
(In other versions of SQL Server Management Studio the option might be called something like "Check for object existence")
As previously answered, changing the scripting option "Include IF NOT EXISTS clause" to false solves the problem. To add context, if this value is true, it has to be scripted by putting the entire Alter Procedure statement in an #statement variable, because the conditional logic associated with the check for existence requires the ALTER PROCEDURE statement be inside a BEGIN/END block, which is not allowed. So Microsoft's workaround is to put the entire ALTER PROCEDURE statement in the #statement variable, which is executed conditionally inside a BEGIN/END block.

View formatted sqlite results

So I code using ruby on rails and the default database is sqlite, which is amazing for development. there is no setup time, no need for connection etc. What really sucks is the output on the command line. when i do a query to list all the contents of a table for example. I just get a dump of text, unlike when you use MySql, the CLI formats the results of the queries nicely (with headers in a table). I am also aware of "headers on" and the commands you can type to format the results of sqlite but those are temporary and I am looking for a more permanent way to format the results so i do not have to do it every-time.
It appears that you can specify SQL statements and metadata statements in an init file and pass this file as the -init parameter.

Do ADO components, especially TADOCommand, work more reliably with un-named, or named parameters, in some cases?

There are several questions on StackOverflow that seem to have answers that contradict each other on the subject of ADO/OleDB, delphi TADOQuery/TADOCommand and the subject of parameters.
Parameters can be introduced two ways, in the CommandText or SQL property of an ADO component. Named parameters, which work most of the time for me, are introduced with colons:
select a, b, c from bar where bat = :baz
This works, 99% of the time for me, just fine. Every now and then I find that ADO or Delphi's wrappers around it, won't accept ":baz" and requires that I write this instead:
select f, g, h from bar where bat = ?
This results in an unnamed parameter, instead of a named parameter. When an ADO Query or ADO Command contains only one parameter, this isn't a big deal. But that's not when ADO acts up on me. Yesterday it acted one way, and today, a different way with a dual-command in a single TADOCommand object, like this, with two commands in one CommandText string:
delete from bar where id = :id1
delete from bat where id = :id2
I had to change it to this:
delete from bar where id = ?
delete from bat where id = ?
It worked all day yesterday. Today, I had to change it back to the first version, to get it to work. The symptom was that the ADO parameters disappeared and would not come back, and when I try to execute the command I get an error, index out of range, when I try to access Parameters[0]. Nothing gives me any warning that the parameters are going away. It seems that a few connections to the ADO dataset, at designtime, jogs the TADOCommand component, in particular, and it "just breaks on me". It is particularly maddening when you're trying to write a query or a command, and you know it works, but the ADO component has decided not to accept "?" or ":x" right now. You can get around its total inability to function by switching from one to the other. But it frustrates me, and probably actually completely blocks other people. I know some people always dynamically build their SQL in code, and avoid using Parameters, and maybe this is why.
Possible answers to my question that I'm anticipating are:
ADO doesn't support multiple commands, or at least Delphi's wrappers don't. Or maybe TADOCommand just doesn't work reliably here.
Parameters are a buggy area in all of ADO, or all of Delphi's ADO wrappers?
You're doing it wrong.
I'm using Delphi XE2, but I've seen similarly dodgy behaviour in 2007, 2009, 2010, and XE.
I'm using Microsoft OLEDB Provider for SQL Server as my OLEDB Provider.
Named parameters with :? I always used it with #, even on Visual Studio (ADO.NET).
And in T-SQL parameters and variable are prefixed with #.
Do not remember having problems with that... Are you sure you doesn't choose Native Client
(installed with an SQL server client install) instead of OLEDB Provider for SQL Server (which
comes with Windows)?
Unfortunately, I've not used Delphi in awhile, so, and I don't have the means to validate this answer from the Delphi perspective.
This is the first time I've seen named parameters prefixed with a colon (:). Usually, in ADODB, the named parameters are prefixed with an at (#) and, yes, unnamed parameters are given with a question mark (?).
One significant advantage of named parameters is their ability to be reused, i.e.
INSERT INTO TABLE T VALUES (#id, #id, 'Hello World');
At the ADODB level. Once you used parameters, either named, or unnamed, you can use CommandText.Parameters.Refresh as a fast means of creating the parameters.
Yes there are some cases where parameters with ? fail. I have found sometimes I need to use :named parameters. Named parameters have an advantage for working with the DB Parameter values, since having the Name property set makes debugging the ADO query or dataset or table easier as well.
I do not understand why. If you have this problem, first check you are using the correct OLEDB provider, and check what version. Also check for potential parsing errors caused by bad SQL generated by you.
I suspect that an internal behaviour inside the OLEDB provider in code that I do not have source code for is to blame for this quirk. The Delphi ADO class wrappers are translators from Delphi's database component layer architecture to ADO's core query/table/dataset APIs all of which are under the hood wrappers around a set of COM objects that deal with ADO RecordSets.

Resources