Bläddra i källkod

Create basic integer print pgsql function

Can be used to demo the behaviour of nested scopes.
DricomDragon 5 år sedan
förälder
incheckning
90c89020e5
2 ändrade filer med 23 tillägg och 0 borttagningar
  1. 1 0
      SQL/plpgsql/call_scope.sql
  2. 22 0
      SQL/plpgsql/create_scope.sql

+ 1 - 0
SQL/plpgsql/call_scope.sql

@@ -0,0 +1 @@
+SELECT scope();

+ 22 - 0
SQL/plpgsql/create_scope.sql

@@ -0,0 +1,22 @@
+CREATE OR REPLACE FUNCTION scope() RETURNS integer AS $$
+<< outerblock >>
+DECLARE
+    quantity integer := 30;
+BEGIN
+    RAISE NOTICE 'Quantity here is %', quantity;  -- Prints 30
+    quantity := 50;
+    --
+    -- Create a subblock
+    --
+    DECLARE
+        quantity integer := 80;
+    BEGIN
+        RAISE NOTICE 'Quantity here is %', quantity;  -- Prints 80
+        RAISE NOTICE 'Outer quantity here is %', outerblock.quantity;  -- Prints 50
+    END;
+
+    RAISE NOTICE 'Quantity here is %', quantity;  -- Prints 50
+
+    RETURN quantity;
+END;
+$$ LANGUAGE plpgsql;