Execute Dynamic SQL commands in SQL Server

In some applications having hard-coded SQL statements is not appealing, because of the dynamic nature of the queries being issued against the database server. Because of this sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can be done quite simply from the application perspective where the statement is built on the fly whether you are using ASP.NET , ColdFusion or any other programming language. But how do you do this from within a SQL Server stored procedure? SQL Server offers a few ways of running a dynamically built SQL statement. These ways are: Writing a query with parameters Using EXEC Using sp_executesql Writing a query with parameters This first approach is pretty straightforward if you only need to pass parameters into the WHERE clause of your SQL statement. Let’s say we need to find all records from the Customers table where City = ‘London’. This can be done easily as the following example shows. ...

How to rename a Stored Procedure in SQL

Sometimes by mistake, if you create a wrongly named stored procedure, you can easily rename the store procedure.
Checkout following commands for renaming the stored procedure

Syntax:

sp_rename 'procedure_name1', 'procedure_name2'
  • procedure_name1 The current name of the stored procedure
  • procedure_name2 The new name of the stored procedure.

A stored procedure can be renamed. The new name should follow the rules for identifiers.

Examples


`EXEC sp_rename 'spGetAvgGrade', 'spNewAvgGrade';`
Output:

Caution: Changing any part of an object name could break scripts and stored procedures. The object was renamed to ‘spNewAvgGrade’.`

Explanation:

In the above example, we change the name of the stored procedure spGetAvgGrade to spNewAvgGrade.

Comments

Yash said…
Thanks for quick help.



yash

Popular posts from this blog

Check If Temporary Table Exists

Multiple NULL values in a Unique index in SQL

How To search entire database?