YouTube2Text

Dynamic SQL in SQL Server — Transcript

by kudvenkat · 1,772 words · 267 segments · language en · Watch on YouTube

Full transcript

  1. 0:00This is part 138 of SQL Server tutorial.
  2. 0:03In this video, we'll discuss what is
  3. 0:05dynamic SQL and a simple example of
  4. 0:08using dynamic SQL. So, what is dynamic
  5. 0:11SQL? In simple terms, a dynamic SQL is a
  6. 0:15SQL built from strings at runtime. Let's
  7. 0:18understand this with an example. Let's
  8. 0:20say we want to implement an employee
  9. 0:22search form that looks like this. Notice
  10. 0:25on the form, we've got four search
  11. 0:27fields. first name, last name, gender,
  12. 0:30and salary. Using this employee search
  13. 0:33form, we want to search this employees
  14. 0:36table. Here is the SQL script to create
  15. 0:38and populate this employees table with
  16. 0:41test data. I'll have the script
  17. 0:43available on my blog in case you need
  18. 0:45it. I've already executed the script. So
  19. 0:48to search this employees table using
  20. 0:51this employees form, we may have a store
  21. 0:54procedure that might look like this.
  22. 0:57Notice on the employee search form we
  23. 0:59have got four search fields and the
  24. 1:01search employee store procedure has got
  25. 1:04four input parameters corresponding to
  26. 1:07those search
  27. 1:08fails and within the body of the store
  28. 1:10procedure we are retrieving all the rows
  29. 1:12and columns from employees table where
  30. 1:16first name column equals whatever value
  31. 1:19we have passed to the first name
  32. 1:20parameter or first name parameter is
  33. 1:23null. So when is first name parameter
  34. 1:25going to be null? If we don't specify
  35. 1:27anything for the first name field, then
  36. 1:30this first name parameter is going to be
  37. 1:32null. So either the parameter is null or
  38. 1:35first name column equals whatever value
  39. 1:38the first name parameter has got. So if
  40. 1:41either of those conditions are true,
  41. 1:43then give me that employee row. And we
  42. 1:46have got other conditions as well for
  43. 1:48each search field. Now here we only have
  44. 1:52f four search fields on the form. So the
  45. 1:55store procedure is not that complicated.
  46. 1:57Now here we are not using any dynamic
  47. 1:59SQL. It's all static SQL in a stored
  48. 2:02procedure. Now let's quickly test this
  49. 2:05store procedure to make sure it works as
  50. 2:08expected. At the moment within our
  51. 2:10employees table we have got seven rows.
  52. 2:13Now let's say we want to use our SP
  53. 2:15search employee store procedure and find
  54. 2:18all employees whose first name is Mark
  55. 2:20and whose last name is Hastings. We're
  56. 2:23not going to make use of the other
  57. 2:25parameters. So in this case gender and
  58. 2:28salary parameters are null. So when we
  59. 2:30execute this notice we get an employee
  60. 2:34whose first name is Mark and last name
  61. 2:36is Hastings. Now let's say we want to
  62. 2:39find all female employees. So in this
  63. 2:41case right here except the gender
  64. 2:43parameter we have set all the other
  65. 2:45parameters to null. Gender is set to
  66. 2:47female. So when we execute this we get
  67. 2:50all female
  68. 2:52employees. Now this procedure in this
  69. 2:55case is not very complicated as we only
  70. 2:58have four search filters. But what if
  71. 3:01there are 20 or more such filters? This
  72. 3:04store procedure can get complex. And to
  73. 3:06make things worse, what if we want to
  74. 3:08specify conditions like and, or, etc.
  75. 3:12between these search filters? The stored
  76. 3:14procedure can get extremely large,
  77. 3:17complicated, and difficult to maintain.
  78. 3:19One way to reduce this complexity is by
  79. 3:22using dynamic SQL. So what we can do
  80. 3:24here is depending on for which search
  81. 3:27filters the end user has provided the
  82. 3:30values on the search page we can build
  83. 3:32the wear clause dynamically at runtime
  84. 3:35which can reduce complexity. However you
  85. 3:38might hear arguments that dynamic SQL is
  86. 3:41bad both in terms of security and
  87. 3:43performance. That is true if the dynamic
  88. 3:47SQL is not properly implemented. From a
  89. 3:49security standpoint, it may open doors
  90. 3:52for SQL injection attack. And from a
  91. 3:54performance standpoint, the cache query
  92. 3:56plans may not be properly reused. If
  93. 3:59properly implemented, we will not have
  94. 4:01these problems with dynamic SQL. In our
  95. 4:04upcoming videos, we'll discuss good and
  96. 4:07bad dynamic SQL implementations. For
  97. 4:10now, let's implement the search example
  98. 4:12using dynamic SQL. Fire up a new query
  99. 4:16editor vendor. And in here I've declared
  100. 4:19a variable at SQL of type envir.
  101. 4:22Remember dynamic SQL is formed from
  102. 4:25strings. So in this string variable,
  103. 4:29let's store our dynamic SQL statement.
  104. 4:31Let's assume the end user has not
  105. 4:34provided values for any of these search
  106. 4:36fields. That means we want to load all
  107. 4:39the rows and columns from employees
  108. 4:41table. Which also means we don't have to
  109. 4:44have this wear clause at all. So
  110. 4:47depending on the values that the user
  111. 4:48has provided for these search fields,
  112. 4:51we're going to build our SQL statement.
  113. 4:54So here our SQL statement is now just
  114. 4:56going to be select star from
  115. 5:00employees. So in this variable we have
  116. 5:02our dynamic SQL statement. The obvious
  117. 5:05question that comes to our mind at this
  118. 5:06point is how to execute this dynamic SQL
  119. 5:09statement. To execute the dynamic SQL
  120. 5:12statement, we use system store procedure
  121. 5:17sp_execute SQL. And to this system store
  122. 5:20procedure, we pass the dynamic SQL
  123. 5:23statement that we want to execute. So
  124. 5:26let's execute all these three statements
  125. 5:28together. And notice we get all the rows
  126. 5:31and columns from our employees table.
  127. 5:34Now let's assume the end user has
  128. 5:37provided values for first name and last
  129. 5:39name fields on the search form. That
  130. 5:42means we have to include the wear clause
  131. 5:44with first name and last name. So to our
  132. 5:47dynamic SQL statement I'm going to
  133. 5:49append the wear clause where first name
  134. 5:55equals at first name. So here we're
  135. 5:59using parameters within our query and
  136. 6:02last name equals at last
  137. 6:07name. Now in this dynamic SQL statement
  138. 6:10we have got parameters. So we have to
  139. 6:12declare these parameter types. And to do
  140. 6:15that I'm going to declare another
  141. 6:18variable. And let's call this at params.
  142. 6:23And in this
  143. 6:25variable we're going to
  144. 6:27declare the parameters that we have in
  145. 6:30our dynamic SQL statement. So set at
  146. 6:34params
  147. 6:35equals. So this at first name
  148. 6:39parameter is of type nare care and let's
  149. 6:43set the length to 100.
  150. 6:48Similarly, last name parameter is also
  151. 6:52of type nvac care and its length is also
  152. 6:55100. Now the system store procedure has
  153. 6:59got two predefined parameters. The first
  154. 7:02parameter is the dynamic SQL statement
  155. 7:04that we want to execute and the second
  156. 7:06parameter declares the parameters that
  157. 7:09we have in our dynamic SQL statement. So
  158. 7:12in our dynamic SQL statement as you can
  159. 7:14see we have got two parameters at first
  160. 7:16name at last name and using this
  161. 7:18variable we have declared the
  162. 7:20parameters. So the second parameter for
  163. 7:24the system store procedure is this
  164. 7:26params variable which contains the
  165. 7:29parameter declarations that we have in
  166. 7:33our dynamic SQL statement. And then we
  167. 7:36also have to supply values for these
  168. 7:38parameters that we have in our dynamic
  169. 7:41SQL statement. So the rest of the
  170. 7:43parameters that we can have for the
  171. 7:45system store procedure are user defined
  172. 7:48parameters. So now we are going to pass
  173. 7:50values for first name and last name
  174. 7:53parameters. So at first
  175. 7:57name
  176. 7:59equals you know let's say for example
  177. 8:02Mark is the first name
  178. 8:07and let's set last
  179. 8:10name to
  180. 8:18Hastings. So now let's go ahead and
  181. 8:21execute this.
  182. 8:23Notice we have an error message. It says
  183. 8:25incorrect syntax near equals. Let's
  184. 8:28understand why we are getting that
  185. 8:30error. And to do that, let's first
  186. 8:32comment this execute statement and let's
  187. 8:35print our SQL statement that we have
  188. 8:37here. So I'm going to use the print
  189. 8:39statement and pass it this variable at
  190. 8:42SQL. So when we execute this, look at
  191. 8:46the query that we have. select star from
  192. 8:49employees where there is no space
  193. 8:52between employees and where that's the
  194. 8:54reason why we are getting that statement
  195. 8:56and the reason we don't have that space
  196. 8:58is because either here or here we don't
  197. 9:02have a space so let's include a space
  198. 9:04there and now when we execute
  199. 9:07this notice now we have got space
  200. 9:09between employees and the wear clause so
  201. 9:13let's comment this print SQL uncomment
  202. 9:16our execute statement
  203. 9:19And let's execute this one more time.
  204. 9:22Notice now we get Mark Hastings as
  205. 9:26expected. So here is the dynamic SQL
  206. 9:29example that we just looked at. To
  207. 9:31execute dynamic SQL, we use system store
  208. 9:34procedure
  209. 9:35SP_execute SQL. It takes two predefined
  210. 9:38parameters and any number of userdefined
  211. 9:41parameters. As you can see the first
  212. 9:43parameter is mandatory and it contains
  213. 9:46the dynamic SQL statement that we want
  214. 9:48to execute. The second parameter is
  215. 9:50optional and this parameter is used to
  216. 9:53declare the parameters that we have in
  217. 9:56our dynamic SQL statement. And then we
  218. 9:59can have any number of userdefined
  219. 10:01parameters. So within our dynamic SQL
  220. 10:03statement we have got two parameters at
  221. 10:06first name and at last name. So we using
  222. 10:09the rest of the parameters here to pass
  223. 10:12values for those parameters that we have
  224. 10:15in our dynamic SQL statement. Notice
  225. 10:17here we are using parameter names as
  226. 10:19well. The parameter names are optional.
  227. 10:22So when we use parameter names, we can
  228. 10:25have those parameters in any order. For
  229. 10:27example, we can have our last name
  230. 10:31parameter first and then our first name
  231. 10:34parameter. So when we execute this, it
  232. 10:37still works exactly the same way as
  233. 10:39before. But then these parameter names
  234. 10:42are optional. So if I don't use
  235. 10:44parameter names, then it's going to use
  236. 10:47the parameter position to specify which
  237. 10:50value is for which parameter. So
  238. 10:53here if I am passing Hastings first,
  239. 10:56then it's going to treat this value is
  240. 10:59for first name parameter and mark for
  241. 11:02last name. But that's not what we want.
  242. 11:04We want mark to be the value for the
  243. 11:07first name parameter. So we need to
  244. 11:08specify that in the first position and
  245. 11:11then Hastings in the second position. So
  246. 11:13when we execute this, it again is going
  247. 11:16to work exactly the same way as
  248. 11:18before. This is just the introduction to
  249. 11:21dynamic SQL. If a few things are unclear
  250. 11:24at the moment, don't worry. In our
  251. 11:26upcoming videos, we will discuss
  252. 11:28implementing a real world search web
  253. 11:30page with and without dynamic SQL,
  254. 11:33performance and security implications of
  255. 11:35dynamic SQL, and along the way we'll
  256. 11:38also discuss good and bad dynamic SQL
  257. 11:41implementations, different options that
  258. 11:43are available for us for executing
  259. 11:45dynamic SQL and their implications and
  260. 11:48finally using dynamic SQL in store
  261. 11:51procedures and implications. Once we
  262. 11:54discuss all these then you will
  263. 11:56understand the flexibility dynamic SQL
  264. 11:59provides, advantages and disadvantages
  265. 12:01of dynamic SQL, when and when not to use
  266. 12:04dynamic SQL. So please stay tuned. Thank
  267. 12:08you for listening and have a great day.

About this transcript

This page contains the full transcript of Dynamic SQL in SQL Server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,772 words across 267 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.