YouTube2Text

Stored procedures in sql server Part 18 — Transcript

by kudvenkat · 3,183 words · 463 segments · language en · Watch on YouTube

Full transcript

  1. 0:00welcome to preum Technologies I am wenet
  2. 0:02this is part 18 of SQL server in this
  3. 0:05session we'll understand what a stor
  4. 0:07procedure is a simple stor procedure
  5. 0:10example creating a store procedure with
  6. 0:12parameters altering a stor procedure
  7. 0:15viewing the text of a stor procedure and
  8. 0:17finally we'll see how to drop a stored
  9. 0:20procedure a stored procedure is a group
  10. 0:23of transact SQL statements if you ever
  11. 0:26have a situation where you have to write
  12. 0:28the same query over and over again you
  13. 0:31can save that specific query as a stored
  14. 0:33procedure and call it just by its name
  15. 0:36let's understand what we mean by this
  16. 0:38with an
  17. 0:40example now I have this table called TBL
  18. 0:43employee which has got you know the ID
  19. 0:46name gender and Department ID columns
  20. 0:48let's say I want name and gender of an
  21. 0:52employee so we type in select name comma
  22. 0:55agenda from TBL employee so every time I
  23. 0:59want the the name and gender of an
  24. 1:01employee you know I have to write this
  25. 1:03query okay instead of that what we can
  26. 1:06actually do is wrap this query inside a
  27. 1:09stored procedure and call that stored
  28. 1:12procedure rather than you having to
  29. 1:14write this query again and again so how
  30. 1:16do we create a stor procedure to create
  31. 1:18a stor procedure we use create procedure
  32. 1:22command so create procedure and then you
  33. 1:26have to give this procedure a name okay
  34. 1:29so s P let us say get
  35. 1:33employees okay since this procedure is
  36. 1:35getting us you know the employee name
  37. 1:37and genda I'm giving it get employees
  38. 1:40and look at this in the name I have this
  39. 1:42letters SP a common naming convention
  40. 1:45for store procedures is that we usually
  41. 1:47prefix that with small letter S and
  42. 1:50small letter P okay indicating that you
  43. 1:52know just by looking at this name you
  44. 1:54can tell okay this is a stor procedure
  45. 1:57all right so create procedure procedure
  46. 2:00your name and then you will use
  47. 2:03as
  48. 2:05begin and
  49. 2:08end okay so the definition of your stor
  50. 2:11procedure goes uh between this begin and
  51. 2:14end okay so this is the body of your
  52. 2:17stor procedure okay now when I execute
  53. 2:19this
  54. 2:20command what's going to happen is a St
  55. 2:24procedure with this name gets created in
  56. 2:27this database which is nothing but
  57. 2:29sample that we have selected here okay
  58. 2:31now if you want to look at the stor
  59. 2:33procedure that you have just created you
  60. 2:34know you want to make sure whether if
  61. 2:36this procedure is actually created or
  62. 2:38not okay go into that database which is
  63. 2:41sample and then you have this folder
  64. 2:44called programmability expand that and
  65. 2:46you should see a folder called store
  66. 2:48procedures if you expand that you know
  67. 2:50we don't have it currently listed there
  68. 2:52just refresh that and you should see
  69. 2:55that store procedure which we have just
  70. 2:56created which is sp get employees okay
  71. 3:00so anytime you want the name and gender
  72. 3:02of an employee instead of writing this
  73. 3:05query again what you can actually do is
  74. 3:08execute the St procedure okay so if you
  75. 3:11want to execute you just need the name
  76. 3:13of the procedure so what happens when I
  77. 3:15execute the St procedure okay to execute
  78. 3:18the stor procedure you just highlight
  79. 3:20that and click execute and you get the
  80. 3:22name and gender you don't have to write
  81. 3:25that query anymore now you might be
  82. 3:26wondering it's a very simple query why
  83. 3:28don't I write that rather than having to
  84. 3:30create this procedure and then invoke it
  85. 3:33now this procedure may be simple in
  86. 3:35reality the procedures will be long you
  87. 3:37know there are very know St procedures
  88. 3:40with over 3,000 of lines for example
  89. 3:43okay and not only that there are several
  90. 3:45other benefits of using St procedures
  91. 3:47from security to network reducing
  92. 3:49Network traffic Etc we will be talking
  93. 3:52about the advantages of St procedures in
  94. 3:54a very great detail in a later
  95. 3:57session Okay so we use create procedure
  96. 4:01or create proc statement to create SP I
  97. 4:03mean you can either
  98. 4:05say create procedure or you can just say
  99. 4:08create proc for
  100. 4:11shortcut okay to create a store
  101. 4:13procedure uh we'll talk about the naming
  102. 4:16Convention of the stor procedures in
  103. 4:18just a bit okay and to execute the stor
  104. 4:21procedure we have seen how to execute
  105. 4:22that stor procedure you know you just
  106. 4:24copy that the name of the store
  107. 4:27procedure and you click this execute
  108. 4:29part button and what happens the SQL
  109. 4:31statement within that short procedure
  110. 4:33gets executed and it Returns the name
  111. 4:35and gender columns all right so that's
  112. 4:38where one way to execute it or you can
  113. 4:41use the exec
  114. 4:42keyword and then click this or you can
  115. 4:46use a full execute keyword and then
  116. 4:49again press F5 or you can also
  117. 4:52graphically execute the store procedure
  118. 4:54just right click on the stor procedure
  119. 4:56and select execute stor procedure and
  120. 4:58the moment you do that that it shows
  121. 5:00this window this procedure doesn't have
  122. 5:03any parameters otherwise you will have
  123. 5:05to supply the values for the parameters
  124. 5:07in just a bit we will see how to create
  125. 5:09a stor procedure that takes parameters
  126. 5:11now when I click okay it executes that
  127. 5:14stor procedure look at
  128. 5:16that all right so those are the
  129. 5:19different ways to execute St
  130. 5:25procedure now let us look at a simple
  131. 5:28example of how to create a stored
  132. 5:31procedure with
  133. 5:36parameters
  134. 5:38okay so let's go back to the table TBL
  135. 5:43employees all right now what I want to
  136. 5:45do is I I want to create a store
  137. 5:48procedure which takes two parameters
  138. 5:50maybe gender and the department ID okay
  139. 5:53for example if I pass gender as male and
  140. 5:57Department ID as one to your store
  141. 5:59procedure
  142. 6:00it should give me employees only within
  143. 6:03that gender and within that department
  144. 6:06okay so your store procedure needs to
  145. 6:08have these parameters okay so let's see
  146. 6:10how to do
  147. 6:12that so as usual to create a store
  148. 6:14procedure we use create procedure
  149. 6:16command so create procedure and give
  150. 6:19your procedure a meaningful name so
  151. 6:22SP get
  152. 6:25employees by
  153. 6:27gender and
  154. 6:31Department okay so I want the employees
  155. 6:33by gender and department now look at
  156. 6:35this now this procedure okay the user
  157. 6:39who invokes your St procedure is going
  158. 6:41to pass the store procedure the gender
  159. 6:44and the department ID so for them to be
  160. 6:47able to pass the values for gender and
  161. 6:49Department ID they should be parameters
  162. 6:51just like how functions have parameters
  163. 6:54in C or any other programming language
  164. 6:56St procedures can also have parameters
  165. 6:59okay so one is the gender parameter and
  166. 7:02if you look at gender it's text here so
  167. 7:05the data type is going to be nare care
  168. 7:07of maybe
  169. 7:0920 and Department ID Department ID is
  170. 7:13going to be integer so Department ID
  171. 7:17integer
  172. 7:18as begin and so the definition of your
  173. 7:22store procedure goes in between these
  174. 7:25lines okay so what do we want from the
  175. 7:28table we want the
  176. 7:30name and maybe just the gender and also
  177. 7:34the department
  178. 7:37ID from which table we want it from TBL
  179. 7:41employee table but we don't want all
  180. 7:44names genders and Department IDs we we
  181. 7:47want to filter them with what the user
  182. 7:50is going to pass in he's going to pass
  183. 7:52in the gender and Department ID so we
  184. 7:54are going to say the gender column here
  185. 7:58should be equal to whatever the user is
  186. 8:00passing in at gender and along the same
  187. 8:04lines and Department
  188. 8:08ID let's bring this to another line is
  189. 8:11equal to whatever the user is going to
  190. 8:13pass
  191. 8:15in okay so these parameters are like
  192. 8:19placeholders when users execute your
  193. 8:22store procedure they're going to pass in
  194. 8:24values for this gender and Department ID
  195. 8:27which will be replaced at execution time
  196. 8:29time okay so let's create the store
  197. 8:32procedure so to create that select the
  198. 8:34entire store procedure click execute
  199. 8:36button command completed successfully
  200. 8:38now if you refresh the stor procedures
  201. 8:41folder you should see SP get employees
  202. 8:44by agender and Department okay now to
  203. 8:48execute this store procedure I just need
  204. 8:51the name of the store procedure and look
  205. 8:53at this this store procedure now is
  206. 8:55expecting gender and Department ID
  207. 8:57parameters now look at this if I don't
  208. 8:59don't pass the parameters and if I try
  209. 9:01to execute that St procedure see
  210. 9:03highlight that and then press execute
  211. 9:07what's going to happen this procedure or
  212. 9:09function SP get employees by gender and
  213. 9:11Department expects parameter at gender
  214. 9:14which was not supplied and that makes
  215. 9:16sense it's expecting a gender parameter
  216. 9:19which is not supplied so we need to pass
  217. 9:21in the gender parameter since gender is
  218. 9:23of type nare I have to use single codes
  219. 9:26so I want the male employees Within
  220. 9:30you know Department ID 1 so Department
  221. 9:33ID is one so these are the two
  222. 9:35parameters that this to procedure
  223. 9:38expects and we need to pass them so when
  224. 9:40I press F5 now look at that I only get
  225. 9:44male employees within that you know
  226. 9:46Department ID one okay on the other hand
  227. 9:49if I want all the male employees in
  228. 9:50Department ID do two I can do
  229. 9:54so all right now when you have
  230. 9:58parameters like say you know what you're
  231. 10:00doing is you're just passing in the
  232. 10:02parameters so so so this male value will
  233. 10:06be taken into at gender parameter
  234. 10:08whereas this number one is passed into
  235. 10:11Department ID parameter okay now what
  236. 10:15happens if I put it the other way around
  237. 10:18I am passing one first okay so what's
  238. 10:22going to happen it will take this one
  239. 10:24into gender and one is an integer but
  240. 10:29gender is of type nare care and this one
  241. 10:31will be converted into nare care
  242. 10:33implicitly no problem but it comes to
  243. 10:36the second parameter male it it tries to
  244. 10:39take this into Department ID parameter
  245. 10:41and if you look at the data type of
  246. 10:43Department ID parameter it is integer
  247. 10:46okay so it tries to convert this string
  248. 10:50into integer and it fails and it throws
  249. 10:53an exception look at this if I try to
  250. 10:55execute this now I get an exception
  251. 10:58saying that error converting data type
  252. 11:01Vare to integer so it is trying to
  253. 11:04convert this male you know string of
  254. 11:07type and Vare into integer and we get
  255. 11:10that error okay so when you have
  256. 11:12multiple parameters that dist stor
  257. 11:14procedure is expecting and if you're
  258. 11:16passing just values the values order you
  259. 11:20know the order in which you pass them is
  260. 11:24important okay the first parameter will
  261. 11:27be used I mean the value here the first
  262. 11:30argument will be used with the first
  263. 11:32parameter and the second argument will
  264. 11:34be used with the second parameter okay
  265. 11:37that's why the order is important
  266. 11:40but if you use the parameter names like
  267. 11:44this let's say I want to pass one to at
  268. 11:48Department ID I can specify the name of
  269. 11:50the parameter like so and similarly I
  270. 11:53can specify the name of the parameter
  271. 11:55for
  272. 11:56gender so when I execute this now now I
  273. 11:59will have no issues because you're
  274. 12:01specifying the name of the parameter
  275. 12:03okay so SQL Server knows okay this one
  276. 12:06is meant you know to be the value for
  277. 12:09Department ID parameter and male is the
  278. 12:12value for gender parameter it's only
  279. 12:14when you don't specify the names of the
  280. 12:15parameter the order of the the order in
  281. 12:18which you pass the parameter is
  282. 12:19parameters is
  283. 12:22important all
  284. 12:26right and okay so we have seen how to
  285. 12:29create a simple store procedure and how
  286. 12:30to create a procedure with parameters as
  287. 12:32well and we have seen how to execute
  288. 12:34them as well okay now once you have the
  289. 12:37stored procedures let's say I have
  290. 12:39created two procedures until now SP get
  291. 12:41employees and SP get employees by gender
  292. 12:43and department now if I want to view the
  293. 12:46text of these store procedures what are
  294. 12:48the different ways that are available
  295. 12:50one way is to Simply right click on that
  296. 12:52store procedure script stor procedure as
  297. 12:55create to new query editor window this
  298. 12:59this you know generates the contents of
  299. 13:02that stor procedure look at this this is
  300. 13:04the stor procedure definition that we
  301. 13:05have created create procedure procedure
  302. 13:07name as begin end and then our query
  303. 13:12this is one way to look at the
  304. 13:14definition of a stored procedure and the
  305. 13:17other way is to use a system stored
  306. 13:20procedure you know these stored
  307. 13:22procedures that we have created here um
  308. 13:25are user defined store procedures these
  309. 13:27are not system store procedures now SQL
  310. 13:29Server you
  311. 13:31know has some system stor procedures
  312. 13:34defined okay and we use it for certain
  313. 13:37task for example I want to find the text
  314. 13:40of his store procedure how do I do that
  315. 13:42I can use a system store procedure
  316. 13:44called s Spore help text okay so look at
  317. 13:49this this is the name of the system St
  318. 13:51procedure SP underscore help text okay
  319. 13:56SP help text and then if I pass in the
  320. 13:58name name of the store procedure there
  321. 14:01SP get
  322. 14:03employees and then when I select them
  323. 14:06together and execute this look at this I
  324. 14:08get the text of my store procedure you
  325. 14:11can then copy that paste it here and see
  326. 14:14how does the implementation of the store
  327. 14:16procedure looks like okay so to view the
  328. 14:18definition of a stor procedure you can
  329. 14:21either right click on
  330. 14:24that script store procedure as create to
  331. 14:27new query editor window or you can use
  332. 14:30the system stored procedure Spore help
  333. 14:32text followed by the name of the store
  334. 14:35procedure which gives you the text of
  335. 14:37the store
  336. 14:38procedure okay now in this slide if you
  337. 14:41look at this you know whenever we name
  338. 14:44user defined store procedure Microsoft
  339. 14:47recommends not to use
  340. 14:50Spore you know prefix for user defined
  341. 14:53stored procedures because the reason is
  342. 14:56system stored procedures has that pre
  343. 14:59prefix okay now if you happen to use
  344. 15:02Spore prefix for your user defined store
  345. 15:06procedures there are two problems number
  346. 15:08one there will be an ambiguity between
  347. 15:10user defined stor procedures and system
  348. 15:12defined stor procedures just by looking
  349. 15:14at the name we cannot tell you know is
  350. 15:16this a user defined store procedure or
  351. 15:18system defined store procedure okay and
  352. 15:21another problem is with future releases
  353. 15:23of you know new SQL Server version there
  354. 15:26may be name conflicts you know if you
  355. 15:29create let's say Spore get date you know
  356. 15:32stored procedure and in the future
  357. 15:34release there is a system stored
  358. 15:36procedure which is you know similarly
  359. 15:38named Spore get date you know it's going
  360. 15:41to conflict with the user store
  361. 15:43procedure okay so to avoid problems like
  362. 15:46this it's always better not to prefix
  363. 15:49user defined St procedures with Spore
  364. 15:57prefix all right so to change the stor
  365. 16:00procedure now once we have created a
  366. 16:02stor procedure for example I have the
  367. 16:04store procedure SP get employees after I
  368. 16:06have created the St procedure let's say
  369. 16:08I want to change its implementation in
  370. 16:10some way how do I do that let's say at
  371. 16:14the moment when I execute this SP get
  372. 16:16employees to procedure I'm not getting
  373. 16:19the names sorted you
  374. 16:21know I mean the names are basically not
  375. 16:23sorted I want them to be sorted so how
  376. 16:26do I do that I will use the order by CL
  377. 16:28Clause so order by
  378. 16:31name okay so I'm changing the
  379. 16:33implementation of this toia now if I
  380. 16:35execute this create procedure once again
  381. 16:38look at this we get an error stating
  382. 16:39that there is already an object named s
  383. 16:41SP get employees why we we we already
  384. 16:44have that and we are trying to create
  385. 16:45that again with the same name so
  386. 16:47obviously we get that error Our
  387. 16:49intention here is to change the
  388. 16:51definition of that St procure not to
  389. 16:53create another store procedure so if you
  390. 16:55want to change the definition of the
  391. 16:57store procedure then you say alter
  392. 17:00procedure and I press F5 the store
  393. 17:02procedure gets changed now if we execute
  394. 17:04that we should have the name
  395. 17:06sorted okay so we use alter procedure
  396. 17:11statement to change the definition of a
  397. 17:12stor procedure and to delete the stored
  398. 17:14procedure we use drop procedure
  399. 17:17procedure name just like you know if you
  400. 17:19want to drop a table you will use drop
  401. 17:22table table name okay so similarly to
  402. 17:25drop a procedure you will say drop
  403. 17:29drop procedure and procedure name for
  404. 17:32example I want to drop or delete SP get
  405. 17:34employees you know I just pass it there
  406. 17:37I press F5 and then if I refresh
  407. 17:41the store procedures folder it's gone
  408. 17:44now it's deleted or what you can do
  409. 17:46alternatively right click on that and
  410. 17:48select
  411. 17:51delete okay now it's also possible to
  412. 17:54encrypt the text of the store procedure
  413. 17:56and it's very simple to do for example I
  414. 17:59have the store procedure
  415. 18:02now SP get employees
  416. 18:06by gender and Department look at this
  417. 18:09now this is not encrypted at the moment
  418. 18:12so when I use Spore help text and when I
  419. 18:15press F5 I able to get the text of that
  420. 18:19St
  421. 18:20procedure so that's how the St procedure
  422. 18:23is implemented now if I want to encrypt
  423. 18:27the contents the text of the store
  424. 18:29procedure I can do that how do I do that
  425. 18:32all you have to do is to use this switch
  426. 18:35this option with
  427. 18:38encryption okay and we want to alter
  428. 18:42that so I will say
  429. 18:44alter now when I press F5 look at this
  430. 18:47command completed successfully and now
  431. 18:49the moment I refresh this folder look at
  432. 18:52this we get a little lock symbol there
  433. 18:54indicating that this to procedure is now
  434. 18:56encrypted okay now if some somebody
  435. 18:59tries to you know um get the text of the
  436. 19:02encrypted St procedure we get a message
  437. 19:05saying that the text for the object is
  438. 19:08encrypted and and we cannot retrieve the
  439. 19:10text of that okay a and you get the same
  440. 19:13kind of message when you kind of use
  441. 19:16script stor procedure as create to new
  442. 19:19query editor window okay we get this
  443. 19:22error box you know if you look at here
  444. 19:25it says the text is encrypted so once a
  445. 19:28St proced procedure is encrypted you
  446. 19:30cannot view the text of that store
  447. 19:32procedure but however if you want to
  448. 19:34delete the store procedure you can go
  449. 19:36ahead and delete it and know just right
  450. 19:38click and select delete it gets deleted
  451. 19:40but you cannot view the contents of a
  452. 19:43store procedure that is
  453. 19:46encrypted all right in the next session
  454. 19:49we will see how to create an invoke a St
  455. 19:51procedure with output parameters in this
  456. 19:53session we have seen how to create a sto
  457. 19:54procedure with input parameters in the
  458. 19:57next session we'll talk about creating
  459. 19:58store procedures with output
  460. 20:01parameters on this slide you can find
  461. 20:03resources for asp.net and cop interview
  462. 20:05questions that's it for today thank you
  463. 20:07for listening have a great day

About this transcript

This page contains the full transcript of Stored procedures in sql server Part 18 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 3,183 words across 463 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.