Write a sql query to sort on different column name according to the parameters passed in the function?



Write a sql query to sort on different column name according to the parameters passed in the functio..

Answer / Shailja Singh

In SQL Server, you can dynamically build a SQL query based on the passed parameter to sort on a different column. Here's an example using dynamic SQL:

```sql
DECLARE @SortColumn NVARCHAR(100)
SET @SortColumn = 'YourColumnName' -- Pass the column name as a parameter

DECLARE @SQLQuery NVARCHAR(MAX)
SET @SQLQuery = 'SELECT * FROM YourTable ORDER BY ' + QUOTENAME(@SortColumn)

EXEC sp_executesql @SQLQuery

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More SQL Server Interview Questions

What is the concept of optimization?

1 Answers  


What are user-defined functions (udfs) in sql server?

1 Answers  


how do we insert 100 records at a time in a table without using for loop in database

1 Answers  


What is a user-defined function in the sql server and what is its advantage?

1 Answers  


You notice that the transaction log on one of your databases is over 4gb the size of the data file is 2mb what could cause this situation, and how can you fix it?

1 Answers  


In which year relase the 7.0& 2000 & 2005?

1 Answers  


What are various ways to enhance the ssrs report?

1 Answers  


What is global temp table?

1 Answers  


What is difference between index and primary key?

1 Answers  


Syntax to find the used space and free space of the Data file in a database(SQL Server). Following queries didn't give the exact Used space and Free Space Information sp_spaceused;DBCC showfilestats;exec MyDbName.dbo.sp_spaceused;SP_HELPFILE Can any one tell me the query for how to find the exact used data file space and free space in a Data File?

2 Answers   Cognizant,


What do you understand by the analysis services in sql server?

1 Answers  


Do you know what is replace and stuff function in sql server?

1 Answers  


Categories