Skip to main content
← PL/SQL Topics

SQL Integration

Explain bind variables in dynamic SQL and preventing SQL injection

Bind Variables and SQL Injection

ExplanationOracle Fusion / EBS · Technical · PL/SQLHigh confidenceGenerated

Pass values through USING binds, never by concatenating them into the statement string.

How it works

Concatenating user input into a dynamic statement lets an attacker change its meaning and forces a hard parse per distinct value. Bind them: EXECUTE IMMEDIATE 'SELECT ... WHERE x = :v' USING p_val. Identifiers (table/column names) cannot be bound — validate them against the data dictionary or DBMS_ASSERT.SIMPLE_SQL_NAME / ENQUOTE_NAME, or map through an allow-list. DBMS_ASSERT also offers NOOP, SQL_OBJECT_NAME and SCHEMA_NAME checks.

How it works
CREATE OR REPLACE FUNCTION emp_count(p_col VARCHAR2, p_val VARCHAR2)
  RETURN NUMBER IS
  v_col  VARCHAR2(128) := DBMS_ASSERT.SIMPLE_SQL_NAME(p_col);  -- validate identifier
  v_n    NUMBER;
BEGIN
  EXECUTE IMMEDIATE
    'SELECT COUNT(*) FROM employees WHERE ' || v_col || ' = :v'
    INTO v_n USING p_val;                                       -- bind the value
  RETURN v_n;
END;
/

Related questions

GroundingGenerated

Model-generated, grounded against the curated knowledge layer. Check specifics against your instance.

Have a follow-up, or a different question?

Continue in Ask Oracle AI