Learning Journal Week 6
Embedded SQL is pretty neat because it lets you mix SQL commands right into other programming languages like C or COBOL. You start off with EXEC SQL to mark where those commands go. Essentially, you compile your program in two steps: first, a precompiler translates the SQL into the host language, and then the regular compiler turns that into something you can actually run. When it comes to talking to a database, you use connection statements like CONNECT, SET CONNECTION, and DISCONNECT. You can also share variables between SQL and your program by declaring them between BEGIN DECLARE SECTION and END DECLARE SECTION. If you’re running queries that give back more than one row, you’ll want to use cursors to handle the results with commands like DECLARE, OPEN, FETCH, and CLOSE. Want more flexibility? That’s where dynamic SQL comes in, letting you create and run SQL statements on the fly with PREPARE and EXECUTE.
CREATE PROCEDURE, and they can accept parameters that either bring in data (IN), send out data (OUT), or do both (INOUT). They often have a bunch of commands grouped together between BEGIN and END.RETURN. You define them with CREATE FUNCTION, and they take input parameters, specifying what type they’ll return using RETURNS. Think of these like the built-in SQL functions, like COUNT(). While stored procedures can give back multiple values, stored functions are handy for when you need to compute a single value right inside your queries.
Comments
Post a Comment