README.execute_statement 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. SQL Language Extension: EXECUTE STATEMENT
  2. Implements capability to take a string which is a valid dynamic SQL
  3. statement and execute it as if it had been submitted to DSQL.
  4. Available in triggers and stored procedures.
  5. Author:
  6. Alex Peshkoff <[email protected]>
  7. Syntax may have three forms.
  8. Syntax 1
  9. ========
  10. EXECUTE STATEMENT <string>;
  11. Description
  12. Executes <string> as SQL operation. It should not return any data rows.
  13. Following types of SQL operators may be executed:
  14. * Insert, Delete and Update.
  15. * Execute Procedure.
  16. * Any DDL (except Create/Drop Database).
  17. Sample:
  18. CREATE PROCEDURE DynamicSampleOne (Pname VARCHAR(100))
  19. AS
  20. DECLARE VARIABLE Sql VARCHAR(1024);
  21. DECLARE VARIABLE Par INT;
  22. BEGIN
  23. SELECT MIN(SomeField) FROM SomeTable INTO :Par;
  24. Sql = 'EXECUTE PROCEDURE ' || Pname || '(';
  25. Sql = Sql || CAST(Par AS VARCHAR(20)) || ')';
  26. EXECUTE STATEMENT Sql;
  27. END
  28. Syntax 2
  29. =========
  30. EXECUTE STATEMENT <string> INTO :var1, ., :varn;
  31. Description
  32. Executes <string> as SQL operation, returning single data row. Only
  33. singleton SELECT operators may be executed with this form of EXECUTE
  34. STATEMENT.
  35. Sample:
  36. CREATE PROCEDURE DynamicSampleTwo (TableName VARCHAR(100))
  37. AS
  38. DECLARE VARIABLE Par INT;
  39. BEGIN
  40. EXECUTE STATEMENT 'SELECT MAX(CheckField) FROM ' || TableName INTO :Par;
  41. IF (Par > 100) THEN
  42. EXCEPTION Ex_Overflow 'Overflow in ' || TableName;
  43. END
  44. Syntax 3
  45. ========
  46. FOR EXECUTE STATEMENT <string> INTO :var1, ., :varn DO
  47. <compound-statement>;
  48. Description
  49. Executes <string> as SQL operation, returning multiple data rows. Any SELECT
  50. operator may be executed with this form of EXECUTE STATEMENT.
  51. Sample:
  52. CREATE PROCEDURE DynamicSampleThree (TextField VARCHAR(100), TableName VARCHAR(100))
  53. RETURNING_VALUES (Line VARCHAR(32000))
  54. AS
  55. DECLARE VARIABLE OneLine VARCHAR(100);
  56. BEGIN
  57. Line = '';
  58. FOR EXECUTE STATEMENT 'SELECT ' || TextField || ' FROM ' || TableName
  59. INTO :OneLine
  60. DO
  61. IF (OneLine IS NOT NULL) THEN
  62. Line = Line || OneLine || ' ';
  63. SUSPEND;
  64. END
  65. N O T E S
  66. =========
  67. I. For all forms of EXECUTE STATEMENT SQL, the DSQL string can not contain
  68. any parameters. All variable substitution into the static part of the SQL
  69. statement should be performed before EXECUTE STATEMENT.
  70. EXECUTE STATEMENT is potentially dangerous, because:
  71. 1. At compile time there is no checking for the correctness of the SQL
  72. statement. No checking of returned values (in syntax forms 2 & 3 )
  73. can be done.
  74. 2. There can be no dependency checks to ensure that objects referred to in
  75. the SQL statement string are not dropped from the database or modified
  76. in a manner that would break your statement. For example, a DROP TABLE
  77. request for the table used in the compiled EXECUTE PROCEDURE statement
  78. will be granted.
  79. 3. In general, EXECUTE STATEMENT operations are rather slow, because the
  80. statement to be executed has to be prepared each time it is executed
  81. by this method.
  82. These don't mean that you should never use this feature. But, please,
  83. take into account the given facts and apply a rule of thumb to use
  84. EXECUTE STATEMENT only when other methods are impossible, or perform even
  85. worse than EXECUTE STATEMENT.
  86. To help (a little) with bugfixing, returned values are strictly checked for
  87. correct datatype. This helps to avoid some errors where unpredictable
  88. type-casting would otherwise cause exceptions in some conditions but not
  89. in others. For example, the string '1234' would convert to an int 1234,
  90. but 'abc' would give a conversion error.
  91. II. If the stored procedure has special privileges on some objects, the
  92. dynamic statement submitted in the EXECUTE STATEMENT string does not
  93. inherit them. Privileges are restricted to those granted to the user who
  94. is executing the procedure.