Code Simplifier | SQL

Parameterized Query with Input Validation

This code snippet demonstrates the use of parameterized queries and input validation to prevent SQL injection attacks. It also includes error handling and testing of different input values.


Empty image or helper icon

This Query related with thread "Guidelines for Working with Dynamic SQL Queries"

Prompt

-- Ensure input validation and sanitization

DECLARE @inputValue NVARCHAR(50);
SET @inputValue = 'Robert''; DROP TABLE Student;--';

-- Use parameterized query

DECLARE @sqlQuery NVARCHAR(MAX);
SET @sqlQuery = N'SELECT * FROM Students WHERE Name = @name';

EXEC sp_executesql @sqlQuery, N'@name NVARCHAR(50)', @name = @inputValue;

-- Error handling

BEGIN TRY
    -- Execute dynamic SQL
    EXEC sp_executesql @sqlQuery, N'@name NVARCHAR(50)', @name = @inputValue;
END TRY
BEGIN CATCH
    -- Log and handle the error
    SELECT ERROR_NUMBER(), ERROR_MESSAGE(), GETDATE();
END CATCH

-- Testing and validation

DECLARE @parameterValue NVARCHAR(50);
SET @parameterValue = 'John';

SET @sqlQuery = N'SELECT * FROM Students WHERE Name = @name';

EXEC sp_executesql @sqlQuery, N'@name NVARCHAR(50)', @name = @parameterValue;

-- Perform testing with different input values and edge cases

Answer

-- Use parameterized query and input validation
DECLARE @inputValue NVARCHAR(50);
SET @inputValue = 'Robert''; DROP TABLE Student;--';

DECLARE @sqlQuery NVARCHAR(MAX);
SET @sqlQuery = N'SELECT * FROM Students WHERE Name = @name';

-- Execute dynamic SQL with parameterized query
EXEC sp_executesql @sqlQuery, N'@name NVARCHAR(50)', @name = @inputValue;

-- Error handling
BEGIN TRY
    EXEC sp_executesql @sqlQuery, N'@name NVARCHAR(50)', @name = @inputValue;
END TRY
BEGIN CATCH
    -- Log and handle the error
    SELECT ERROR_NUMBER(), ERROR_MESSAGE(), GETDATE();
END CATCH

-- Perform testing with different input values and edge cases
DECLARE @parameterValue NVARCHAR(50);
SET @parameterValue = 'John';

SET @sqlQuery = N'SELECT * FROM Students WHERE Name = @name';

EXEC sp_executesql @sqlQuery, N'@name NVARCHAR(50)', @name = @parameterValue;

This code snippet performs the following steps:

  1. Declares and sets the input value as 'Robert''; DROP TABLE Student;--' to test for SQL injection vulnerability.
  2. Declares a dynamic SQL query to select records from the Students table where the Name column matches the input value.
  3. Executes the dynamic SQL query using the sp_executesql system stored procedure with a parameterized query to prevent SQL injection.
  4. Wraps the execution of the dynamic SQL query inside a TRY...CATCH block for error handling.
  5. If an error occurs, it logs the error number, error message, and the current date.
  6. Sets a new parameter value 'John' for testing purposes.
  7. Executes the dynamic SQL query with the new parameter value.

To validate and test this code snippet, you can perform the following steps:

  • Run the code snippet as is to test for SQL injection vulnerability using the input value 'Robert''; DROP TABLE Student;--'.
  • Check if the record is selected correctly for the input value 'John'.
  • Test with different input values and edge cases to ensure the code handles them properly.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

The code snippet begins by declaring and setting an input value to test for SQL injection vulnerability. It then declares a dynamic SQL query to select records from the Students table, using the Name column as a parameter. The sp_executesql system stored procedure is used to execute the dynamic query with a parameterized query to prevent SQL injection. Error handling is implemented using a TRY...CATCH block to log and handle any errors that may occur during the execution.

To validate the code, you can run it as is and observe the behavior. It is recommended to test with different input values and edge cases to ensure the code can handle them properly.