Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet395/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   391   392   393   394   395   396   397   398   ...   443
Bog'liq
BookSQL

The 

sp_executesql

 Stored procedure

The sp_executesql stored procedure was introduced after the EXEC command. It is more secure and 

more flexible in the sense that it has an interface; that is, it supports input and output parame ters. Note 

that unlike EXECsp_executesql supports only Unicode character strings as the input batch of code. 

The fact that you can use input and output parameters in your dynamic SQL code can help you 

write more secure and more efficient code. In terms of security, parameters that appear in the code 

cannot be considered part of the code—they can only be considered operands in expressions. So, by 

using parameters, you can eliminate your exposure to SQL injection. 

The sp_executesql stored procedure can perform better than EXEC because its parameterization 

aids in reusing cached execution plans. An execution plan is the physical processing plan that SQL 

Server produces for a query, with the set of instructions regarding which objects to access, in what 

order, which indexes to use, how to access them, which join algorithms to use, and so on. To simplify 

things, one of the requirements for reusing a previously cached plan is that the query string be the 

same as the one for which the plan exists in cache. The best way to efficiently reuse query execution 

plans is to use stored procedures with parameters. This way, even when parameter values change, the 

query string remains the same. But if for your own reasons you decide to use ad-hoc code instead of 

stored procedures, at least you can still work with parameters if you use sp_executesql and therefore 

increase the chances for plan reuse. 

The sp_executesql procedure has two input parameters and an assignments section. You specify 

the Unicode character string holding the batch of code you want to run in the first parameter, which 

is called @stmt. You provide a Unicode character string holding the declarations of input and output 

parameters in the second input parameter, which is called @params. Then you specify the assign-

ments of input and output parameters separated by commas. 

The following example constructs a batch of code with a query against the Sales.Orders table. The 

example uses an input parameter called @orderid in the query’s filter.

DECLARE @sql AS NVARCHAR(100); 

 

SET @sql = N'SELECT orderid, custid, empid, orderdate 



FROM Sales.Orders 

WHERE orderid = @orderid;'; 

 

EXEC sp_executesql 



  @stmt = @sql, 

  @params = N'@orderid AS INT', 

  @orderid = 10248;

This code generates the following output.

orderid     custid      empid       orderdate 

----------- ----------- ----------- ----------------------- 

10248       85          5           2006-07-04 00:00:00.000

www.it-ebooks.info





Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   391   392   393   394   395   396   397   398   ...   443




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish