YouTube2Text

Select into in sql server — Transcript

by kudvenkat · 1,974 words · 280 segments · language en · Watch on YouTube

Full transcript

  1. 0:00this is part 97 of SQL Server tutorial
  2. 0:03in this video we'll discuss the power
  3. 0:05and use of Select into statement in SQL
  4. 0:07server for the purpose of this demo
  5. 0:09we'll be using these two tables
  6. 0:11departments and employees I've already
  7. 0:13created these tables and here is the SQL
  8. 0:15script to create and public them with
  9. 0:17test data I'll have the script available
  10. 0:19on my blog in case you need it so what's
  11. 0:22the use of Select into statement select
  12. 0:24into statement selects data from one
  13. 0:26table and inserts it into a new table
  14. 0:29now let's look at some of the use cases
  15. 0:31for this select into statement we can
  16. 0:34use it to copy all rows and columns from
  17. 0:36an existing table into a new table this
  18. 0:39is extremely useful when you want to
  19. 0:41make a backup copy of the existing table
  20. 0:44let's understand this with an example
  21. 0:45here we have got this employees table
  22. 0:47which has some data within it now we
  23. 0:49want to make a backup copy of this table
  24. 0:52the easiest way to do that is by using
  25. 0:54select into statement look at the
  26. 0:57example here select star into employees
  27. 0:59backup from employees table so the star
  28. 1:02here specifies that we want to retrieve
  29. 1:05all the rows and columns from employees
  30. 1:07table and then this employees backup
  31. 1:09table should be created on the Fly and
  32. 1:12all that data from the employees table
  33. 1:14should be inserted into that employees
  34. 1:16backup table this employees backup table
  35. 1:19has to be a new table it cannot be an
  36. 1:22existing table if you specify an
  37. 1:24existing table with select into
  38. 1:26statement it's going to throw an error
  39. 1:27saying that the table already exists
  40. 1:30let's look at this in action let's flip
  41. 1:31to SQL management studio so if you look
  42. 1:34at the tables folder within sample DB
  43. 1:36database we don't have the employees
  44. 1:38backup table there we only have the
  45. 1:40employees table so now let's go ahead
  46. 1:43and execute our select into statement so
  47. 1:45when we execute that the first thing
  48. 1:47that should happen is this employees
  49. 1:49backup table should have been created on
  50. 1:51the Fly and all the data from employees
  51. 1:53should have been copied into it so let's
  52. 1:56refresh this tables folder notice that
  53. 1:58we have our employees back up table
  54. 2:00there and when we select the data from
  55. 2:02it we should have all the rowes of the
  56. 2:04employees table within that employees
  57. 2:07backup now let's go ahead and drop this
  58. 2:10table now we can also use this select
  59. 2:13into statement to copy all rows and
  60. 2:16columns from an existing table into a
  61. 2:19new table in an external database so if
  62. 2:22you look at this employees and employees
  63. 2:24backup table they are present within the
  64. 2:27same database now what if you want you
  65. 2:30know the backup in a different database
  66. 2:32now here I have another database hrdb
  67. 2:35now what we want to do is create a
  68. 2:38backup copy for this employees table in
  69. 2:41an external database that is in hrdb
  70. 2:44database so the employees table is
  71. 2:46present in Sample DB but we want the
  72. 2:48backup in another database HR DB now we
  73. 2:51can still achieve that using select into
  74. 2:53statement all we have to do is specify
  75. 2:57the fully qualified name so this EMP
  76. 2:59employees backup table has to be present
  77. 3:01in hrdb database and we're using the
  78. 3:03schema dbo so HR db. dbo do employees
  79. 3:07backup so when we execute this select
  80. 3:10into statement you know this employees
  81. 3:13backup table should have been created in
  82. 3:15hrdb database so let's go ahead and
  83. 3:18refresh this tables folder notice that
  84. 3:20we have the employees backup table there
  85. 3:23and when we select the data we should
  86. 3:24have all the rules of the employees
  87. 3:26table so now let's go ahead and drop
  88. 3:29that table
  89. 3:31we can also use the select into
  90. 3:33statement to copy only selected columns
  91. 3:36into a new table so if you look at this
  92. 3:40employees table we have got like five
  93. 3:43columns in it now let's say for example
  94. 3:46in our backup table I don't want all the
  95. 3:49columns I just want the first three
  96. 3:51columns ID name and gender okay if
  97. 3:54that's the case instead of using star
  98. 3:56simply specify the column list the
  99. 3:58columns that you want we want ID name
  100. 4:01gender from employees table the backup
  101. 4:03table has to be employees backup so this
  102. 4:05table will now have only those three
  103. 4:08columns so let's look at that in action
  104. 4:10when I execute that the employees backup
  105. 4:12table is created on the fly with those
  106. 4:14three columns and all the data should
  107. 4:17have been copied into that so let's drop
  108. 4:21this backup table
  109. 4:22again now we can also use select into
  110. 4:25statement to copy only selected rows
  111. 4:28into a new table table so now if you
  112. 4:31look
  113. 4:33at this employees table at the moment it
  114. 4:36has got five rows in it uh in it now
  115. 4:39what we want to do is we want to
  116. 4:40retrieve and make a backup copy of the
  117. 4:43employees whose department is Department
  118. 4:46ID 1 we can still do that using a wear
  119. 4:49Clause here so in this case the schema
  120. 4:52for this employees backup table is going
  121. 4:54to match up with the employees table but
  122. 4:57as far as the data is concerned it's
  123. 4:59going to contain only the employee rows
  124. 5:02where Department ID equals 1 so let's go
  125. 5:05ahead and execute this
  126. 5:07query now look at that the message says
  127. 5:09two rows affected and if you look at the
  128. 5:11data in our backup table we should only
  129. 5:14have those employees whose Department ID
  130. 5:17is one let's drop
  131. 5:22that now so far we have seen examples of
  132. 5:25how to make a backup copy from a single
  133. 5:29table
  134. 5:30now we can also use select into to copy
  135. 5:33columns from two or more tables into a
  136. 5:36new table so here we have got two tables
  137. 5:39departments and employees now what I
  138. 5:41want to do is I want to copy all the
  139. 5:44columns from both of these tables into
  140. 5:47you know a backup table okay so if you
  141. 5:50look at the select into statement here
  142. 5:52this is again straightforward now let me
  143. 5:54actually remove that uh you know into
  144. 5:57here and let me say simply select star
  145. 6:01from employees in a join departments and
  146. 6:03that's the join condition we are joining
  147. 6:05with Department ID in employees table
  148. 6:08with Department ID in Department table
  149. 6:11so when we execute this what are we
  150. 6:12going to get we're going to get you know
  151. 6:15all the columns from both the tables
  152. 6:17okay now what I'm going to do here is
  153. 6:19Select star
  154. 6:23into
  155. 6:26employees backup so this is going to set
  156. 6:29select all these rows and columns into
  157. 6:31this employees backup table let's
  158. 6:33actually look at that in action so I am
  159. 6:36going to execute this and when we select
  160. 6:39data from employees backup look at that
  161. 6:42we've got all the columns and rows from
  162. 6:45both the tables employees and
  163. 6:47departments now what if we want you know
  164. 6:49only specific columns now if you look at
  165. 6:51this data definitely Department ID is
  166. 6:54duplicated we have Department ID in
  167. 6:56employees table and Department ID in
  168. 6:58Department table table now let's say we
  169. 7:00want only you know one column either
  170. 7:04from the employees table or from the
  171. 7:05Department's table we don't want that to
  172. 7:07be duplicated we can still do that so
  173. 7:10instead of specifying star here we can
  174. 7:12specify the list of columns that we want
  175. 7:15so let's do that now so let's actually
  176. 7:18drop
  177. 7:19this and I am going to say we want all
  178. 7:23the
  179. 7:24columns from employee table so I'm going
  180. 7:27to say employees uh
  181. 7:29star and then I want from departments
  182. 7:33table so departments dot Department name
  183. 7:38so let's execute
  184. 7:40this and now if we select from employees
  185. 7:44backup notice that we only got the
  186. 7:46department ID from employees table and
  187. 7:49then you know the department name from
  188. 7:51Department's table we didn't get the
  189. 7:52Department ID column from departments
  190. 7:55table so you can either specify star or
  191. 7:58the column list list that you want when
  192. 8:00you are joining two or more tables so
  193. 8:02here the example covers only two tables
  194. 8:05but you can have as many tables as you
  195. 8:08want now you can also create a new table
  196. 8:12whose columns and data types match with
  197. 8:14an existing table now there might be
  198. 8:17situation where you know your table has
  199. 8:19got a lot of columns maybe like 40 to 50
  200. 8:22different columns in a given table now
  201. 8:25your requirement is to create a table
  202. 8:28whose ke matches with that table but you
  203. 8:31don't want any data within that new
  204. 8:33table that you're going to create now
  205. 8:35you can still do that using select into
  206. 8:38if you have to manually do that you know
  207. 8:40you have to write that create table
  208. 8:41script and it's going to take a bit of
  209. 8:43time but with select into statement it's
  210. 8:45going to take literally like 5 Seconds
  211. 8:47to do that now the trick here is to
  212. 8:51specify your wear clause and you know
  213. 8:53the condition here is never going to be
  214. 8:55true one not equal to one that will
  215. 8:58never be true so no row will be copied
  216. 9:01from employees into this employees
  217. 9:03backup okay it will only create that
  218. 9:06table with the structure of the
  219. 9:07employees table okay let's look at that
  220. 9:10in action let's drop this employees
  221. 9:12backup table and now I'm going to
  222. 9:14execute this query right here and look
  223. 9:17at the message it says zero rows
  224. 9:19affected that means no rows are copied
  225. 9:21from the employees table into employees
  226. 9:23backup but the structure of this table
  227. 9:26must be very similar to employees table
  228. 9:29look at that we've got ID name gender
  229. 9:31salary and Department ID columns the
  230. 9:33same columns that we have in employees
  231. 9:36table now one very important thing to
  232. 9:40keep in mind is that you cannot use
  233. 9:43select into statement to select data
  234. 9:45into an existing table select into
  235. 9:48statement can only be used to select
  236. 9:50data into a new table okay to select
  237. 9:54data into an existing table you will
  238. 9:56have to use insert into state
  239. 9:59statement so look at this when I execute
  240. 10:02this select star into employees backup
  241. 10:04now at the moment we have this employees
  242. 10:06backup table we don't have any data in
  243. 10:08that so it's an existing table and here
  244. 10:11I'm using select into statement and I'm
  245. 10:14trying to select data into this existing
  246. 10:16table and look at what we are going to
  247. 10:18get look at the message it says there is
  248. 10:21already an object named employee backup
  249. 10:23in the database so you cannot use select
  250. 10:25into statement to select data into an
  251. 10:27existing table to select data into the
  252. 10:30existing table you will have to use
  253. 10:32insert into statement so here insert
  254. 10:34into employees backup select sta from
  255. 10:38employees so when we do that it's going
  256. 10:41to retrieve all the rows from employees
  257. 10:43and insert them into this existing table
  258. 10:45employees
  259. 10:47backup there we go now let's actually
  260. 10:50truncate the data in this
  261. 10:55one so truncate table employees backup
  262. 10:58so this is going to remove all the rows
  263. 11:01from that table so now we don't have any
  264. 11:05data now let's say for some reason you
  265. 11:07don't want you know all the columns from
  266. 11:10employees table you only want to insert
  267. 11:12ID name and gender column values uh but
  268. 11:15not salary and Department ID if that's
  269. 11:17the case you can specify the column list
  270. 11:20here so insert into employees backup and
  271. 11:23the values that I want to insert are ID
  272. 11:26name gender and and we want to select ID
  273. 11:33name and gender from employees so when
  274. 11:36we execute this within our employees
  275. 11:39backup you know only those three columns
  276. 11:41should be populated whereas salary and
  277. 11:43Department ID or
  278. 11:49not thank you for listening and have a
  279. 11:51great
  280. 11:52day

About this transcript

This page contains the full transcript of Select into in sql server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,974 words across 280 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.