What is a dynamic SQL query, and when would I want to use one?

ぃ、小莉子 提交于 2019-12-10 02:08:06

问题


What is a dynamic SQL query, and when would I want to use one? I'm using SQL Server 2005.


回答1:


Here's a few articles:

  • Introduction to Dynamic SQL
  • Dynamic SQL Beginner's Guide

From Introduction to Dynamic SQL:

Dynamic SQL is a term used to mean SQL code that is generated programatically (in part or fully) by your program before it is executed. As a result it is a very flexible and powerful tool. You can use dynamic SQL to accomplish tasks such as adding where clauses to a search based on what fields are filled out on a form or to create tables with varying names.




回答2:


Dynamic SQL is SQL generated by the calling program. This can be through an ORM tool, or ad-hoc by concatenating strings. Non-dynamic SQL would be something like a stored procedure, where the SQL to be executed is predefined. Not all DBA's will let you run dynamic SQL against their database due to security concerns.




回答3:


A dynamic SQL query is one that is built as the program is running as opposed to a query that is already (hard-) coded at compile time.

The program in question might be running either on the client or application server (debatable if you'd still call it 'dynamic') or within the database server.



来源:https://stackoverflow.com/questions/2190016/what-is-a-dynamic-sql-query-and-when-would-i-want-to-use-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!