- Learn T-SQL Querying
- Pedro Lopes Pam Lahoud
- 168字
- 2021-06-24 14:38:13
The sp_executesql procedure
The sp_executesql procedure is the recommended method for sending an ad hoc T-SQL statement to SQL Server. If stored procedures cannot be leveraged for some reason, such as when T-SQL statements must be constructed dynamically by the application, sp_executesql allows the user to send an ad hoc T-SQL statement as a parameterized query, which uses a similar caching mechanism to stored procedures. This ensures that the plan can be reused whenever the same query is executed again. Building on our example from the Ad hoc plan caching section, we can rewrite the query using sp_executesql, as in the following example which can be executed in the AdventureWorks sample database:
EXECUTE sp_executesql @stmt = N'SELECT LastName, FirstName, MiddleName
FROM Person.Person
WHERE PersonType = @PersonType;'
, @params = N'@PersonType nchar(2)'
, @PersonType = N'EM';
This ensures that any time the same query is sent with the same parameter markers, the plan will be reused, even if the statement is dynamically generated by the application.
- 集成架構(gòu)中型系統(tǒng)
- Mastering Mesos
- 輕輕松松自動(dòng)化測(cè)試
- Machine Learning for Cybersecurity Cookbook
- PHP開(kāi)發(fā)手冊(cè)
- Implementing Oracle API Platform Cloud Service
- 網(wǎng)站前臺(tái)設(shè)計(jì)綜合實(shí)訓(xùn)
- 網(wǎng)站入侵與腳本攻防修煉
- 電氣控制與PLC原理及應(yīng)用(歐姆龍機(jī)型)
- JRuby語(yǔ)言實(shí)戰(zhàn)技術(shù)
- Data Analysis with R(Second Edition)
- Kubernetes on AWS
- 傳感器應(yīng)用技術(shù)
- Spark Streaming實(shí)時(shí)流式大數(shù)據(jù)處理實(shí)戰(zhàn)
- NetSuite ERP for Administrators