Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

PL/SQL & T-SQL (Oracle and SQL Server) Module 6

Dynamic SQL and triggers in T-SQL interview questions

Interview questions on dynamic SQL and triggers in T-SQL: warm-ups, then what each harder question tests and how a good answer starts.

8 questions: 3 warm-ups and 5 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher to mid

    How do EXEC(@sql) and sp_executesql differ?

    Answered in the lesson Dynamic SQL with sp_executesql, safely

  2. Fresher to mid

    What do the inserted and deleted tables hold?

    Answered in the lesson T-SQL triggers: inserted, deleted and pitfalls

  3. Fresher to mid

    Does an AFTER UPDATE trigger fire when the UPDATE matches no rows?

    Answered in the lesson T-SQL triggers: inserted, deleted and pitfalls

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Mid to senior

    A security test of your stock search reports that the name filter returns every item for a crafted input. The procedure concatenates optional filters for part of the name, the SKU and a maximum price, and sorts by a column and direction the caller names. Explain the weakness, rewrite it safely and describe a test that proves the fix.

    What it tests
    Recognising pasted input, passing every filter value, a LIKE pattern included, as a typed parameter, handling the caller's sort column and direction without pasting text, and proving the fix with a test that fails on the old code.
    First step
    The filters paste the caller's text into the statement, so a crafted name ends the string literal and adds a condition of its own. Keep the optional filters dynamic if that helps the plans, but pass every value as a typed parameter of sp_executesql, the LIKE pattern too.
    Follow-ups

    From the lesson Dynamic SQL with sp_executesql, safely

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  2. Mid to senior

    A report procedure used to read dbo.orders directly. After a change that builds its query as dynamic SQL, so users can choose columns, every report user gets error 229, SELECT permission denied, although they still have EXECUTE on the procedure. Explain why, and fix it without granting the tables to the users.

    What it tests
    Knowing that ownership chaining covers the module's own statements but not dynamic SQL, and choosing a least-privilege fix such as EXECUTE AS a dedicated user or module signing.
    First step
    Before the change the procedure and dbo.orders had the same owner, so the ownership chain spared the users a permission check on the table. Dynamic SQL is checked with the permissions of the user who runs it, and they have none on dbo.orders.
    Follow-ups

    From the lesson Dynamic SQL with sp_executesql, safely

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Mid to senior

    A nightly job loads 10,000 price rows into dbo.stock with one INSERT … SELECT. A trigger refuses prices above a ceiling with THROW, and every few nights one bad row makes the whole load fail. The business wants the good rows loaded and the bad ones reported. Explain why the load fails as a whole, and redesign it.

    What it tests
    Knowing what a trigger's error does to its statement and transaction, and moving validation before the load so that good rows go in and bad rows are reported.
    First step
    Remember that the trigger runs once for the whole INSERT and is part of its transaction. Then decide where the ceiling check can run on the staged set, before any row reaches dbo.stock.
    Follow-ups

    From the lesson T-SQL triggers: inserted, deleted and pitfalls

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  4. Senior to staff

    The finance team wants every past price of every item, with when it was valid, for disputes going back two years. A developer proposes an AFTER UPDATE trigger that copies old rows into dbo.price_history. Compare that design with a system-versioned temporal table, and choose one.

    What it tests
    Weighing a trigger-based audit against a temporal table: completeness, the paths each can miss, the queries the finance team needs, and the cost of the history.
    First step
    Write down what each design records for an UPDATE, a DELETE, a MERGE and a TRUNCATE, and how each answers the question "what was the price of this item on a given day".
    Follow-ups

    From the lesson T-SQL triggers: inserted, deleted and pitfalls

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All PL/SQL & T-SQL (Oracle and SQL Server) interview questions · The PL/SQL & T-SQL (Oracle and SQL Server) track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.