Interview

Dynamic sql output parameter

Dynamic sql output parameter
Avatar of shohal
Written by shohal

Dynamic sql output parameter

SQL script to create Employees table

Create table Employees (  

    ID int primary key identity,   

   FirstName nvarchar(50),     

LastName nvarchar(50),    

  Gender nvarchar(50),    

  Salary int )

Go

Insert into Employees values (‘Mark’, ‘Hastings’, ‘Male’, 60000)

Insert into Employees values (‘Steve’, ‘Pound’, ‘Male’, 45000)

Insert into Employees values (‘Ben’, ‘Hoskins’, ‘Male’, 70000)

Insert into Employees values (‘Philip’, ‘Hastings’, ‘Male’, 45000)

Insert into Employees values (‘Mary’, ‘Lambeth’, ‘Female’, 30000)

Insert into Employees values (‘Valarie’, ‘Vikings’, ‘Female’, 35000)

Insert into Employees values (‘John’, ‘Stanmore’, ‘Male’, 80000)

Go


We want to write a dynamic sql statement that returns total number of male of female employees. If the gender value is specified as “Male”, then the query should return total male employees. Along the same lines, if the the value for gender is “Female”, then we should get total number of female employees.

The following dynamic sql, will give us what we want. In this case, the query returns total number of “Male” employees. If you want the total number of female employees, simply set @gender=’Female’.

Declare @sql nvarchar(max) Declare @gender nvarchar(10) Set @gender = ‘Male’ Set @sql = ‘Select Count(*) from Employees where Gender=@gender’ Execute sp_executesql @sql, N’@gender nvarchar(10)’, @gender


At the moment we are not using output parameters. If you want the count of employees to be returned using an OUTPUT parameter, then we have to do a slight modification to the query as shown below. The key here is to use the OUTPUT keyword in your dynamic sql. This is very similar to using OUTPUT parameters with a stored procedure.

Declare @sql nvarchar(max) Declare @gender nvarchar(10) Declare @count int Set @gender = ‘Male’ Set @sql = ‘Select @count = Count(*) from Employees where Gender=@gender’ Execute sp_executesql @sql, N’@gender nvarchar(10), @count int OUTPUT’, 
                                      @gender, @count OUTPUT Select @count


The OUTPUT parameter returns NULL, if you forget to use OUTPUT keyword.. The following query returns NULL, as we removed the OUTPUT keyword from @count parameter

Declare @sql nvarchar(max) Declare @gender nvarchar(10) Declare @count int Set @gender = ‘Male’ Set @sql = ‘Select @count = Count(*) from Employees where Gender=@gender’ Execute sp_executesql @sql, N’@gender nvarchar(10), @count int OUTPUT’,
                                      @gender, @count Select @count

🚀
Dynamic sql output parameter | TechTweet

If you’d like me to proceed with any of these, please just let me know from the site techtweet.xyz! Also if you need to learn something new than subscribe YouTube : ASP.NET With SQL SERVER

About the author

Avatar of shohal

shohal

I have profession and personal attachment with custom ERP Software development, Business Analysis, Project Management and Implementation almost (36) ,also Oracle Apex is my all-time favorite platform to developed the software. Moreover i have some website development experience with WordPress. For hand on networking experience DevOps and CCNA, it create me a full package. Here are some core programming language with networking course i have been worked: Oracle SQL ,PL/SQL,Oracle 19c Database , Oracle Apex 20.1,WordPress,Asp.Net ,MS SQL ,CCNA ,Dev Ops, SAP SD

Leave a Comment