Writing and Executing Dynamic Query in SQL Server example

Category > MSSQLSERVER || Published on : Thursday, November 5, 2015 || Views: 1565 || Writing and Executing Dynamic Query in SQL Server example


 Database: I am making use of Microsoft Northwind database,

SQL Snippets:-


CREATE PROCEDURE Customers_GetCustomer
      @CustId CHAR(5)
AS
BEGIN
      DECLARE @SQL NVARCHAR(2000)
      SET @SQL = 'SELECT ContactName FROM Customers WHERE CustomerId = @CustomerId'
      EXEC sp_executesql @SQL, N'@CustomerId CHAR(5)', @CustomerId = @CustId
END