Dynamic SQL example using SQL Server

Dynamic SQL example using SQL Server

Before going through below example, Download adventureworks database for sql server 2008 r2.

Using below example we are going to execute the dynamic query at run time.

Code:
declare @columnName varchar(max)
declare @_query varchar(max)
DECLARE @jobTitle varchar(75)
set @jobTitle='''Design Engineer'''
set @columnName='OrganizationNode, OrganizationLevel'
set @_query='select '+@columnName+' from [AdventureWorks2008R2].[HumanResources].[Employee] where jobtitle='+@jobTitle+''
exec(@_query)



Output



Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar