YouTube2Text

SQL Server Integration Services (SSIS) Part 13 - Script Tasks using C# — Transcript

by WiseOwlTutorials · 2,942 words · 437 segments · language en · Watch on YouTube

Full transcript

  1. 0:00welcome to this wisal tutorial on
  2. 0:02creating script tasks using the C
  3. 0:04programming language within SQL Server
  4. 0:06integration Services here's what you'll
  5. 0:08learn in the
  6. 0:09tutorial we'll Begin by looking at the
  7. 0:11two types of script within ssis script
  8. 0:14tasks and script components and then
  9. 0:17we'll introduce an overview of what
  10. 0:18we're trying to
  11. 0:19achieve we'll then look at actually
  12. 0:21writing script looking at basic script
  13. 0:24tasks looking at using the variable
  14. 0:26dispenser to get at variables within our
  15. 0:28script and looking at simpler method to
  16. 0:30access variables which is to pass them
  17. 0:32into script tasks as readon or read
  18. 0:34write
  19. 0:35variables finally we're look at how to
  20. 0:37finish the package by joining all the
  21. 0:39script tasks up together and testing it
  22. 0:41to check it works so let's get
  23. 0:44[Music]
  24. 0:45started before we begin looking at
  25. 0:48script tasks in the control flow let's
  26. 0:50just have a look at the possible ways of
  27. 0:52writing script within integration
  28. 0:54Services there's basically two as I said
  29. 0:57in control flow you can create a script
  30. 0:59task
  31. 1:00and what this will do is firstly manage
  32. 1:02the the order of execution of tasks
  33. 1:04within your control flow and it's also
  34. 1:06handy for reading in and setting the
  35. 1:08value of variables and that's what we'll
  36. 1:10be covering in this
  37. 1:12tutorial you can also separately create
  38. 1:15script components in your data flow and
  39. 1:19a script component as this dialog box
  40. 1:21suggests allows you to say where your
  41. 1:23data is coming from the source where
  42. 1:25it's going to the destination or by far
  43. 1:28the most commonly it will do trans
  44. 1:29informations on the data so it'll
  45. 1:31massage it as it flows through your data
  46. 1:33flow and that'll be covered in a
  47. 1:35separate tutorial but for now let's go
  48. 1:38on and look at what we're trying to
  49. 1:39achieve in this
  50. 1:41[Music]
  51. 1:42package so what's our package going to
  52. 1:45do well I've got an Excel workbook of
  53. 1:48those familiar xfactor contestants UK
  54. 1:50version again and I've got two
  55. 1:52worksheets I've got a mentors worksheet
  56. 1:54which contains 10 mentors and I've got a
  57. 1:57finist one which contains
  58. 2:00109
  59. 2:02finalists add those two numbers together
  60. 2:04and you get 119 uh Records in total
  61. 2:08remember that number if you could
  62. 2:10please in SQL Server I've got the same
  63. 2:13thing I've got a table of finalists
  64. 2:15there's 108 rows in that and I've got a
  65. 2:18table of mentors and there's nine rows
  66. 2:21in that if you add those two numbers
  67. 2:23together you get 117 not 119 remember
  68. 2:27that number too if you could
  69. 2:28please what's all this got to do with
  70. 2:30integration
  71. 2:32Services well here's the package I want
  72. 2:34to create it looks a great deal more
  73. 2:36complicated than it actually is what
  74. 2:38it's going to do is ask an initial
  75. 2:40question saying do I want to get my
  76. 2:42information from Excel or not if I do
  77. 2:45then what it will do is reading the two
  78. 2:46XL worksheets using use a row number
  79. 2:49data flow task to store those
  80. 2:52information in
  81. 2:53variables add the two variables together
  82. 2:56and show the results so I'll get a
  83. 2:58message saying there's 100 19 rows in
  84. 3:01total if on the first question I say I
  85. 3:04don't want to get my information from
  86. 3:06Excel I'll go down the failure route and
  87. 3:08in that case I'll decide to read the
  88. 3:10information in from SQL Server tables
  89. 3:13I'll get 117 as a result and that will
  90. 3:15be displayed in the final
  91. 3:17message so let's see this actually
  92. 3:19working in practice before I continue I
  93. 3:22must close down my Excel workbook
  94. 3:24otherwise it won't
  95. 3:25work so if I started off first time
  96. 3:28around I'm going to choose get my
  97. 3:30information from Excel so I'll choose
  98. 3:32yes I go down the left hand path and I
  99. 3:34get a message saying there's 119
  100. 3:36finalists and
  101. 3:38mentors I'll now run that again but this
  102. 3:41time I'll go down the right hand path
  103. 3:43and I'll choose no I don't want Excel in
  104. 3:46this case I get the message saying
  105. 3:47there's 117 finalists and
  106. 3:49mentors so I can choose okay don't be
  107. 3:52put off by the failure symbol there
  108. 3:55that's intentional that makes me go down
  109. 3:57the SQL Server path so first what my
  110. 4:00package is going to do I'm not in any
  111. 4:02way claiming it's the most efficient way
  112. 4:04to achieve this but it does illustrate
  113. 4:06how script scripting Works in script
  114. 4:09tasks in the control
  115. 4:11flow so what we'll now do is go on to
  116. 4:14the next stage which is to show how to
  117. 4:16write basic script
  118. 4:19tasks so let's have a look at how to
  119. 4:21create our first script task in
  120. 4:23integration Services which will be to
  121. 4:25ask a question saying do you want to use
  122. 4:27Excel or not I've created a new package
  123. 4:29P Ag and I'm going to ask I'm going to
  124. 4:32create a script task and I'll say use
  125. 4:35Excel as a name of it to attach script
  126. 4:39to it as always I can double click on it
  127. 4:42I'm actually going to use the language C
  128. 4:44by default throughout this tututorial
  129. 4:45there's a separate tutorial if you're a
  130. 4:47VB
  131. 4:48person and I'm going to change the name
  132. 4:51of my default procedure to run to make
  133. 4:54choice just to show it's possible but I
  134. 4:56won't at this stage pass any variables
  135. 4:58in so if I edit that script what will
  136. 5:02happen is it will create a separate
  137. 5:03project in Visual Studio tools for
  138. 5:05applications and what I can now do
  139. 5:07within that firstly is to get rid of the
  140. 5:10horrible comments a personal view of
  141. 5:13that and then I'm going to have to
  142. 5:15rename my entry level procedure to make
  143. 5:17choice because that's what I chose to
  144. 5:19call it so I've now got a stub of a
  145. 5:23procedure which will run when that
  146. 5:25script task
  147. 5:26runs now I've got some code in my
  148. 5:30clipboard which I'm going to paste in at
  149. 5:31this point saying what it's going to
  150. 5:33do so it's going to display a message
  151. 5:36box the message box will have the prompt
  152. 5:38saying choose Excel it will have a
  153. 5:41caption or a title saying Choice it will
  154. 5:43have a yes and a no button it will have
  155. 5:46a question mark symbol and by default if
  156. 5:49I press return it's the second button
  157. 5:51button two which will be selected so
  158. 5:53I'll choose SQL
  159. 5:54Server what I'm then going to do is look
  160. 5:57at the value of the answer so to see
  161. 5:59whether I chose yes or no and if I chose
  162. 6:02yes what I'm going to do is return
  163. 6:04success from this procedure and if I
  164. 6:07chose no I'm going to return
  165. 6:10failure to make it easier Microsoft
  166. 6:12thoughtfully add in every single script
  167. 6:14task this enumeration it's called Script
  168. 6:17results and it means that instead of
  169. 6:19having to type microsoft. SQL server.
  170. 6:21DTS runtime. DTS exact result. suuccess
  171. 6:24I can just type script results. suuccess
  172. 6:27as a shorthand so that's what I'm going
  173. 6:29to to do so what I'm going to do in the
  174. 6:32yes part of my Clause is say that the
  175. 6:37task result
  176. 6:40equals script results.
  177. 6:44success and if I leave it like that it
  178. 6:47will give an error and the reason for
  179. 6:49that is because I need to convert the
  180. 6:51type of it if you look at task result
  181. 6:53can you see that the data type of that
  182. 6:55is int so what I need to do is perform a
  183. 6:58data type version to make sure that
  184. 7:01apples and apples are being
  185. 7:03compared so that will return success if
  186. 7:05I choose Excel IF on the other hand I
  187. 7:08choose SQL
  188. 7:10Server I want to choose a failure or
  189. 7:12return
  190. 7:13failure and that completes my script I
  191. 7:16think what I can now do is try it out if
  192. 7:19I close this down it will automatically
  193. 7:20save it and when I choose okay it will
  194. 7:22build it as well to make sure it makes
  195. 7:24sense and the fact that it's not coming
  196. 7:26up with any errors at that point is a
  197. 7:28very good sign
  198. 7:30so if I want now want to run this
  199. 7:31package I'll just make sure it's my
  200. 7:33default before I
  201. 7:35begin I can choose either yes or no if I
  202. 7:38choose yes it goes down this it will go
  203. 7:41down the Excel path if I choose no it
  204. 7:44will go down the SQL Server path and
  205. 7:46I'll see a failure so that's all looking
  206. 7:48very good so far what I'll now do is go
  207. 7:51on to look at using variables in two
  208. 7:55different
  209. 7:58ways I've got a a second version of our
  210. 8:01tutorial as you can see I've added two
  211. 8:03sequence containers one for two tasks to
  212. 8:06import data from Excel and one for two
  213. 8:08tars to import data from SQL
  214. 8:10Server to see how these work I've set up
  215. 8:13two connection managers one to my SQL
  216. 8:15Server database and one to my Excel
  217. 8:17workbook what I've then done is within
  218. 8:20my sequence container for Excel I've
  219. 8:22added two data flow tasks the first one
  220. 8:25we'll read the mentors in from the Excel
  221. 8:27worksheet of called mentors and then it
  222. 8:29will use a row count task to assign that
  223. 8:33to the variable called number
  224. 8:35mentors the other data flow task does a
  225. 8:38similar thing but this time it stores a
  226. 8:40number of finalists in a variable called
  227. 8:42number
  228. 8:43finalists and the SQL Server task do
  229. 8:45exactly the same thing but in this case
  230. 8:47they take their table their data from
  231. 8:49the table of mentors in SQL Server
  232. 8:51rather than an Excel
  233. 8:53worksheet and the fin list takes its
  234. 8:55data from the table of fin list and SQL
  235. 8:57Server
  236. 8:59so I've got three variables created I've
  237. 9:01got number finalists which will hold the
  238. 9:03number of rows in the finalist table or
  239. 9:05worksheet number mentors which will do
  240. 9:07the equivalent for the number me for the
  241. 9:09mentor worksheet or table and what I
  242. 9:11need to do is calculate the number total
  243. 9:13which is the sum of the first
  244. 9:15two as it happens by far the easiest way
  245. 9:18to do this is with an expression task
  246. 9:20but to make life more complicated and to
  247. 9:22show how script tasks work what I'm
  248. 9:25going to do is add one of those down
  249. 9:26here and use it to calculate the result
  250. 9:30[Music]
  251. 9:32so I'll calculate the total
  252. 9:35there I'll connect all my tasks up later
  253. 9:37on in this tutorial for the moment let's
  254. 9:39just edit
  255. 9:41this I could and probably normally would
  256. 9:44at this point pass in the two readon
  257. 9:47variables I want to work with number
  258. 9:48finalists and number mentors and pass in
  259. 9:51the read right variable called uh total
  260. 9:54numbers uh which I want to
  261. 9:57set but I'm actually going to make
  262. 9:58things that bit harder and use something
  263. 10:00called a variable dispenser just to show
  264. 10:03how this works I have to say I'm not a
  265. 10:05big fan of this but if somebody may find
  266. 10:07it
  267. 10:08useful so in my main main procedure
  268. 10:11which I've left called main on this
  269. 10:13occasion I've again got some code in my
  270. 10:17clipboard what this does is create a
  271. 10:20collection of variables which is
  272. 10:22initially
  273. 10:24empty it then uses something called the
  274. 10:26variable dispenser and looks for reading
  275. 10:29two variables number mentors and number
  276. 10:31finalists so nothing else can change
  277. 10:34them it then locks for writing the
  278. 10:37number total
  279. 10:38[Music]
  280. 10:39variable what it then does is call makes
  281. 10:42a call to the get variables
  282. 10:44method and what that does is add the
  283. 10:47variables which I've locked into my
  284. 10:49variables collection I need to use the
  285. 10:51word ref because I'm passing the
  286. 10:52argument by ref so that I can get some
  287. 10:54information out of the method as well as
  288. 10:56passing me information into it
  289. 10:59what I then do is use the two variables
  290. 11:02I've added to my variables collection
  291. 11:06get their values and read them into in
  292. 11:08integer variables called number mentors
  293. 11:09and number
  294. 11:10finalists and finally I set the value of
  295. 11:14my number total
  296. 11:16variable on this occasion rather
  297. 11:18dangerously I've done this by index
  298. 11:20number it's the third item in the
  299. 11:22collection which is why it's got number
  300. 11:24two as the index number it might have
  301. 11:26been safer to use number total instead
  302. 11:30and you access it by
  303. 11:32name what I then have to do is unlock my
  304. 11:35variables collection and finally say
  305. 11:37that the whole thing has been a
  306. 11:39success so that's how you can use a
  307. 11:41variable dispenser what we're now going
  308. 11:43to do is show an easier way of creating
  309. 11:45script using by passing in variable
  310. 11:48values instead which I think is what I'd
  311. 11:51actually
  312. 11:53recommend so the final bit of our jigsaw
  313. 11:56before we link all the pieces together
  314. 11:58is to write write a script to display
  315. 12:00the total the value in fact in the
  316. 12:03variable called number total now I could
  317. 12:06have done that in the previous script
  318. 12:08but I wanted to show it in a separate
  319. 12:09one to give me an opportunity to show
  320. 12:11passing variable values which is
  321. 12:12actually something we've done in quite a
  322. 12:14few previous tutorials in this
  323. 12:16series so I'll add a script task in
  324. 12:18there and what it will do is display the
  325. 12:21total so that's what I'll call
  326. 12:24it and then we can double click on it to
  327. 12:27edit it what I'm going to do in order to
  328. 12:29be able to access a variable called
  329. 12:31number total in my script is I'm going
  330. 12:33to pass it as a readon variable so
  331. 12:36there's all my system variables being
  332. 12:37listed and there in the middle is number
  333. 12:41total it's a readon variable because I
  334. 12:43only want to find out what the value is
  335. 12:45I don't want in any way to change it so
  336. 12:48now I can edit my
  337. 12:51script
  338. 12:52and in the main task I've got some code
  339. 12:55in my clipboard I'm just going to paste
  340. 12:56that in by right clicking and pasting
  341. 13:00and what it will do is create a variable
  342. 13:01called number total and set it equal to
  343. 13:04the value of the variable called number
  344. 13:07total so it's much easier I think to
  345. 13:10pass in the variable and then access it
  346. 13:13as part of the collection of variables
  347. 13:15um within uh DTS within
  348. 13:18ssis now that I've got that variable I
  349. 13:21can just display the value in a message
  350. 13:22I need to convert it to a string first
  351. 13:24before I can do that and then I'll spit
  352. 13:26out the fact that the whole thing has
  353. 13:27been a success
  354. 13:29so that completes my script what I can
  355. 13:31now do is to exit that just before I do
  356. 13:35that here's what would happen if I had
  357. 13:36an error me error in my script so I've
  358. 13:39missed out the semicolon which I fairly
  359. 13:41often do in
  360. 13:42C if I now don't notice that red
  361. 13:46underlining and try exiting this and
  362. 13:48then choose okay it will tell me that
  363. 13:51I've got compilation errors in my
  364. 13:52package do I want to save my changes and
  365. 13:55at this point the thing I'll be most
  366. 13:56likely to do is choose no go back into
  367. 13:59my script and correct the change and I
  368. 14:02can use the error list window to find
  369. 14:05the
  370. 14:06error so if I reinstate my error and go
  371. 14:09to my error list I can double click on
  372. 14:12that and it will highlight where the
  373. 14:13error
  374. 14:13[Music]
  375. 14:16is but I'm going to hopefully write a
  376. 14:18perfect script and all I now need to do
  377. 14:21is link all these pars together by
  378. 14:22setting some
  379. 14:23[Music]
  380. 14:25constraints so we'll be covering
  381. 14:28constraints in a later tutorial in the
  382. 14:30series but for now let's have a quick
  383. 14:32look at what we need to do to get this
  384. 14:33package to work the first thing to do is
  385. 14:36to say connect up the first script task
  386. 14:39with this import from Excel bit but I
  387. 14:42also need to connect it up to the SQL
  388. 14:44Server
  389. 14:45bit that's not going to work if I choose
  390. 14:49yes to use Excel it will return a
  391. 14:50success from that script and everything
  392. 14:52will be fine but it will execute both
  393. 14:54the two packages so what I'm going to do
  394. 14:56is right click on this second constraint
  395. 14:58and change it into a failure
  396. 15:00one and what will happen now is if I
  397. 15:03choose no from the first error message
  398. 15:05I'll go down the failure route and I'll
  399. 15:07import my data from SQL
  400. 15:08Server now I need to address what
  401. 15:10happens after importing the data and to
  402. 15:13do that I'm again going to connect up
  403. 15:14the Excel sequence container up to the
  404. 15:17calculate total
  405. 15:19script and I'm also going to connect the
  406. 15:21SQL
  407. 15:22one as things stand this script will
  408. 15:25never run because it can only run if
  409. 15:28both of the these two sequence
  410. 15:29containers both complete successfully
  411. 15:31and that can never happen so what I'm
  412. 15:34going to do is right click on each or
  413. 15:36one of these and choose edit and change
  414. 15:40it so it's a logical all so in this case
  415. 15:43any one constraint leading into the task
  416. 15:45must evaluate to True which is exactly
  417. 15:47what I want if I then choose okay both
  418. 15:51the two green lines show in dotted as
  419. 15:54dotted lines to show that any one of
  420. 15:55them can be true and I can now just link
  421. 15:58up the final two
  422. 15:59tasks to finish my
  423. 16:02package I'll just make sure that that's
  424. 16:04my um startup object and if I now run
  425. 16:08this to test it out it will work whether
  426. 16:10I choose
  427. 16:11[Music]
  428. 16:13Excel
  429. 16:15or if I
  430. 16:17[Music]
  431. 16:19don't and that completes this tutorial
  432. 16:22you can find loads more training
  433. 16:23resources on SQL Server integration
  434. 16:25Services reporting Services analysis
  435. 16:28services is.net and Microsoft Office at
  436. 16:32www.wis our.co.th
  437. 16:34[Music]

About this transcript

This page contains the full transcript of SQL Server Integration Services (SSIS) Part 13 - Script Tasks using C# by WiseOwlTutorials, generated from the public captions YouTube serves with the video. The transcript has 2,942 words across 437 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.