What is the diff between Static Queries and Dynamic queries
give me some examples

Answer Posted / manoj

Static Queries are permanent and cannot be changed during run-time, like: "SELECT * FROM Employees"

Dynamic Queries can be changed during run-time as they are created by using variables and these variables contain parts of SQL Query, like:

DECLARE @SQL VARCHAR(MAX)
DECLARE @WHENSQL VARCHAR(2000)
DECLARE @SEARCHSQL VARCHAR(500)

SELECT @WHENSQL = 'EmployeeID'
SELECT @SEARCHSQL = '100'

SELECT @SQL = 'SELECT * FROM Employees WHERE ' + @WHENSQL + ' = ' + @SEARCHSQL

EXEC (@SQL)
-- or
EXEC sp_executesql @SQL


For more interview Questions on SQL Server: http://sqlwithmanoj.wordpress.com/interview-questions/

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is data compression?

544


What is 5nf in normalization form?

563


How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?

576


Explain why variables called the most powerful component of ssis?

526


What is data source in connection string?

487






What is dbcc?

728


Explain few examples of stored procedure over triggers?

512


What is “begin trans”, “commit tran”, “rollback tran” and “savetran”?

577


How do I find the sql server version?

512


What is resource governor in sql server?

542


as a part of your job, what are the dbcc commands that you commonly use for database maintenance? : Sql server database administration

486


What is the use of nvl work?

559


What are the results of running this script?

506


Explain how to integrate the ssrs reports in application?

516


where can you add custom error messages to sql server? : Sql server administration

572