Back to SQL Notes
Topic #65

SQL Parameters

Parameterised queries separate data from SQL code, neutralising injection attacks at the source. This is the secure way every modern application talks to a database.


SQL Parameters - Prevent SQL Injection

SQL parameters (Parameterized Queries) can be used to protect a web site from SQL injections.

A parameterized query is a SQL statement that uses placeholders instead of directly adding the input values into the query text. The placeholders get replaced with the actual values when the query executes. This makes the queries more safe and more reusable.

Most databases support parameterized queries, but the syntax varies:

  • MySQL use ? for parameters
  • SQL Server uses @ for parameters
  • PostgreSQL uses $ for parameters

SQL parameters are added to an SQL query at execution time, in a controlled manner.

ASP.NET Razor Example

  userid = getRequestString("UserId");
query = "SELECT *
FROM Users WHERE UserId = @userid";
db.Execute(query, userid);

Note that parameters in SQL Server are presented by a @ marker.

The SQL engine checks each parameter to ensure that it is correct for its column and are treated literally, and not as part of the SQL to be executed.

Another Example

  cname = getRequestString("CustomerName");
caddress = getRequestString("Address");

  ccity = getRequestString("City");

  query = "INSERT INTO Customers (CustomerName, Address, City) Values(@cname, @caddress, @ccity)";
db.Execute(query,
  cname, caddress, ccity);

Examples

The following examples shows how to build parameterized queries in some common web languages.

SELECT STATEMENT IN ASP.NET:

  userid = getRequestString("UserId");

  query = "SELECT * FROM Customers WHERE CustomerId = @userid";
cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@userid",
  userid);

  cmd.ExecuteReader();

INSERT INTO STATEMENT IN ASP.NET:

  cname = getRequestString("CustomerName");
caddress = getRequestString("Address");

  ccity = getRequestString("City");

  query = "INSERT INTO Customers (CustomerName, Address, City) Values(@cname, @caddress, @ccity)";
cmd = new SqlCommand(query);

  cmd.Parameters.AddWithValue("@cname", cname);

  cmd.Parameters.AddWithValue("@caddress", caddress);

 cmd.Parameters.AddWithValue("@ccity",
  ccity);
cmd.ExecuteNonQuery();

Want to go beyond the notes?

Join Coding Now Tech Institute's SQL course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

SQL Parameters – FAQs

Quick answers about learning SQL Parameters in SQL.

This free note from Coding Now Tech Institute explains SQL Parameters in SQL — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every SQL topic on Coding Now Tech Institute, including SQL Parameters, is 100% free with no signup required.
With focused practice, most students grasp SQL Parameters in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now