Writing re runnable sql server scripts Part 66 — Transcript
Full transcript
- 0:00this is part 66 of SQL Server tutorial
- 0:02in this video we'll discuss writing
- 0:04runnable SQL scripts please watch part
- 0:0765 before proceeding so what is a
- 0:10runnable SQL script a runnable SQL
- 0:13script is a script that when run more
- 0:15than once will not throw errors let's
- 0:18understand what we mean by this
- 0:19statement with an example let's flip to
- 0:21SQL Server management Studio notice that
- 0:24here we have this create table script
- 0:26this is going to create this table TBL
- 0:28employee I want this table to be created
- 0:31in the sample database so that's why we
- 0:33are using use sample so this table is
- 0:36going to be created within the sample
- 0:38database so when I run this query at the
- 0:40moment notice that I don't have the
- 0:41stable TBL employee so when I run this
- 0:44the first time this is going to execute
- 0:46successfully and the table gets created
- 0:49so when we refresh this notice that we
- 0:51have the table within that sample
- 0:53database then if I try to run this query
- 0:56once again look at what's going to
- 0:57happen we get an error why because we
- 1:00have the table already that's why it
- 1:02says there is already an object named
- 1:04TBL
- 1:05employee okay so this script right here
- 1:08is not runnable so how do we make the
- 1:11script runnable pretty straightforward
- 1:13check for the existence of the table if
- 1:16the table doesn't exist only then try to
- 1:19create the table else print a message
- 1:21stating the table already exists so
- 1:24obviously the next question is how do we
- 1:27check for the existence of a table in
- 1:30the previous session we discussed using
- 1:32information on score schema. tables view
- 1:35to list all the tables in a SQL Server
- 1:37database so we're going to make that
- 1:39make use of that view to check for the
- 1:41existence of the table so look at this
- 1:43query select star from information
- 1:45andore schema. tabls where table name is
- 1:48equal to the the name of the table that
- 1:50we are trying to create so if that table
- 1:53exists this query is going to return a
- 1:55row and then we are passing the result
- 1:58of this query to this function exist so
- 2:01if at all if there is anything returned
- 2:03by this query then this exist function
- 2:05is going to return true otherwise it's
- 2:07going to return false so basically look
- 2:10at this expression right here if not
- 2:12exists so if the table does not exist
- 2:16then go ahead and create the table print
- 2:17a message stating the table is
- 2:19successfully created else print a
- 2:21message stating the table already exist
- 2:24pretty
- 2:25straightforward let's look at this query
- 2:27and action and just speed things up I
- 2:30have this query already typed so let me
- 2:32copy and paste that in SQL Server
- 2:34management
- 2:36studio so since we already have the
- 2:38table TBL employee when we execute this
- 2:40query um look at this first of all this
- 2:43query when we execute this query we get
- 2:46a row back and we are passing that to
- 2:48this exist function so obviously you
- 2:50know this is going to check this
- 2:52expression right here is going to check
- 2:53if the table is there or not and if the
- 2:56table is not there then create the the
- 3:00table else print this message
- 3:02straightforward so let's execute this
- 3:05look at that table TBL employee already
- 3:07exists on the other hand if I delete
- 3:09this
- 3:10table and execute this query notice that
- 3:14table TBL employee successfully created
- 3:17refresh this TBL employee there I
- 3:20execute this query once again table TBL
- 3:22employee already exists no matter how
- 3:24many times we execute this we don't get
- 3:27an error
- 3:30so this script is
- 3:34runable now to check for the existence
- 3:36of the table we have used information
- 3:39schema. tabls there is another way to
- 3:41check for the existence of the table you
- 3:43can simply use this function this SQL
- 3:46Server buil-in function object ID and
- 3:49pass the name of the object here the
- 3:51name of the object is our table name so
- 3:53if this object already exists in SQL
- 3:55Server database then it will have an ID
- 3:59and this function is simply going to
- 4:00return that ID so if there is an ID for
- 4:03that object then we know the table is
- 4:06already there if it is null then we know
- 4:10you know we don't have a table there so
- 4:11we'll go ahead and create that so just
- 4:14to show you that we already have the
- 4:17table right here this table TBL employee
- 4:20so when we say select object ID and then
- 4:23pass the name of the object which is TPL
- 4:27employee and then look at this when I I
- 4:29execute this query I get the ID of that
- 4:33object okay so if that is null if object
- 4:37ID for a given object is null then we
- 4:39know for sure that object doesn't exist
- 4:41we can go ahead and create that object
- 4:43in this case table and then print a
- 4:45message saying um table created
- 4:48successfully uh else will print a
- 4:50message saying the table already exists
- 4:52and just to show you that in action let
- 4:55me copy this
- 4:56query and paste that within SQL ser
- 4:59management Studio the table is already
- 5:01there so when we execute this table TBL
- 5:04employee already
- 5:07exists all right now depending on what
- 5:10we want to achieve sometimes you know um
- 5:13we may want to drop and recreate the
- 5:15table so if the table already exists we
- 5:18want to drop and recreate it okay this
- 5:21is especially true if we are building
- 5:23some test
- 5:24databases okay so how do we drop and
- 5:27recreate the tables you know again check
- 5:29for the existence of the table and then
- 5:32only drop the table um because if you
- 5:35don't check for the existence of the
- 5:36table and then try to drop a table and
- 5:38if the table doesn't exist you will get
- 5:41again an error so that's why when
- 5:43dropping a table check if the table
- 5:45already exist and again to check for the
- 5:47existence of the table you can either
- 5:49use information schema. tables view or
- 5:53this object ID function okay notice that
- 5:57if object ID then drop the table so if
- 6:00there is if that is not null then we
- 6:02know that object is there in which case
- 6:04go ahead and drop it and then recreate
- 6:06the table okay let's look at that query
- 6:09in
- 6:12action so let's copy this to SQL Server
- 6:15management studio so the table is
- 6:17already there so when I execute this
- 6:19it's going to drop the table and then
- 6:21recreate that okay so no matter how many
- 6:24times we execute this it's going to drop
- 6:27the table recreate it drop the table
- 6:28recreate it
- 6:30okay now you might be wondering let me
- 6:32actually delete
- 6:35this if you don't have this condition
- 6:37right here the check to make sure that
- 6:39the object is there before we try to
- 6:41delete if we don't have that condition
- 6:44and notice that we don't have the table
- 6:45tval employee now if I try to execute
- 6:48this we will get an error why because
- 6:51the table doesn't exist anymore so the
- 6:53query errors out right there and then
- 6:56this doesn't get executed that's why
- 6:58make sure you check for the existence of
- 7:01the table before you you know try to
- 7:04drop
- 7:06it let's look at another example now
- 7:10this script right here is not runnable
- 7:13look at what the script is trying to do
- 7:15it's trying to add a column email
- 7:17address to this stable TBL employee when
- 7:19we run the script the first time it's
- 7:21going to run without any problem if that
- 7:23column doesn't exist if I run it again
- 7:26then it's going to throw an error
- 7:28because a table cannot have um two
- 7:30columns with the same name so this
- 7:32script again is not runnable so how do
- 7:35we make the script runnable obviously
- 7:37check for the existence of that column
- 7:39so how do you check for the existence of
- 7:41the column again you can use this
- 7:43information schema. columns we discussed
- 7:46about this in previous session as well
- 7:48and notice that we are checking the name
- 7:50of the column not only that actually
- 7:53let's look at that query and action
- 8:04so when I execute this select star from
- 8:07information schema. columns where column
- 8:10name is equal to email address so when
- 8:13we execute this we we we we didn't get
- 8:15anything why because we don't have that
- 8:17column there so let me actually execute
- 8:20this query so it's going to add that
- 8:23colum to our table TPL
- 8:27employee so if we expand that
- 8:30we should have email address column
- 8:32right there so now when we execute
- 8:37this we should get a row look at that
- 8:39the column name is email address and we
- 8:42are also having here table name filter
- 8:47and that's very important because if
- 8:49there is another table with the same
- 8:51column name you know you will get that
- 8:53true as well that's why we need to
- 8:55filter you know on table name so if that
- 9:00column email address doesn't exist on
- 9:02the stable TBL employee only then try to
- 9:06add that column okay and notice that
- 9:08here I have another condition table
- 9:10schema again this is very important
- 9:13especially if your SQL server has
- 9:15multiple schemas okay in multiple
- 9:17schemas you can have same table name and
- 9:20same column name so obviously you don't
- 9:23want to be altering a table that is
- 9:25present in a different schema okay so
- 9:28that's why it's very important important
- 9:29to check the schema as well just in case
- 9:31if your SQL server has got multiple
- 9:34schemas if that's not the case it's
- 9:36enough if you check just the table name
- 9:38and the column name again this script is
- 9:41runnable no matter how many times you
- 9:43run you know this is not going to
- 9:45through an error if the column exists
- 9:47it's going to print that message if it
- 9:49doesn't exist it's going to add that
- 9:52column again to check for the column
- 9:55existence you can either use this view
- 9:57or you can use this column length
- 9:59function okay again just
- 10:02like you know object ID function so
- 10:06column length you pass the name of the
- 10:08table and the column that you're looking
- 10:10for if that column length function is
- 10:13not null then we know that the column
- 10:17already exists on that table else the
- 10:20column doesn't exist in which case you
- 10:22can have that you know the script to add
- 10:24the column to the
- 10:26table all right on this slide you can
- 10:29find resources for asp.net cop and SQL
- 10:31Ser interview questions that's it for
- 10:33today thank you for listening have a
- 10:35great day
About this transcript
This page contains the full transcript of Writing re runnable sql server scripts Part 66 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,795 words across 255 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.