How to Backup a Table

There is not standard method for backing up a table in SQL Server.  A common request I receive from clients is to only backup specific tables within a database.  Well, unfortunately there is no out of the box method for doing this akin to a database backup so we need to roll our own way. It is usually done one of two ways.  The first is to use a select into method to copy the table.  The other way is to script the table out using the Generate Scripts Tasks.  Both methods have positives and negatives.  First off, the SELECT.. INTO Method is by far the fastest.  It can copy a large number of rows very quickly, the downfall to this however, is that it does not carry over the Keys, Indexes or Constraints.  The Generate Scripts method is slow (and I don’t recommend it for very large tables), however it can facilitate copying over any of the other objects associated with the table.  Let’s look at the generate scripts task: First, right click on the database that contains the table you want to backup and choose Tasks -> Generate Scripts. The Generate Scripts Wizard Appears.  Select “Next” past the splash screen then select the database that contains the table.  The next screen that appears is the Script Options. Scroll down on the script options until you see Table/View Options.  We want the following to be true: Check Constraints, Script Data, Foreign Keys, Primary Keys, Triggers, & Unique Keys.  (Or […]

Continue reading ...

How to Backup a Database

Backing up a database is relatively straight forward using SQL Server Management Studio. First open management studio and connect to the Database Server that contains the database you want to backup. Expand the “Databases” folder and right-click on the database you want to make a backup of. Choose Tasks -> Back Up… (now the Back up database dialog appears) From here, if you simply hit the “OK” button, your database will be backed up in the default location. However, let’s specify a location so we can find it. Select the “Remove” button to remove the current backup location Select the “Add” button and add a new location to backup the database to. Now choose a location on disk and give it a file name. It is good practice to include the date of the backup in the filename. You may also want to save the file name with a .BAK extension You can select “OK” and the backup will begin. You can optionally at this point, select the “Script” button at the top of the dialog in order to retrieve the script that SSMS uses to perform the backup. A new window will open and you will have your script. You can then use this script in a job, or run it yourself.

Continue reading ...

How to Add a Linked Server

Adding a Linked server can be done by either using the GUI interface or the sp_addlinkedserver command. Adding a linked Server using the GUI There are two ways to add another SQL Server as a linked server.  Using the first method, you need to specify the actual server name as the “linked server name”.  What this means is that everytime you want to reference the linked server in code, you will use the remote server’s name.  This may not be beneficial because if the linked server’s name changes, then you will have to also change all the code that references the linked server.  I like to avoid this method even though it is easier to initially setup.  The rest of the steps will guide you through setting up a linked server with a custom name: To add a linked server using SSMS (SQL Server Management Studio), open the server you want to create a link from in object explorer. In SSMS, Expand Server Objects -> Linked Servers -> (Right click on the Linked Server Folder and select “New Linked Server”) The “New Linked Server” Dialog appears.  (see below). For “Server Type” make sure “Other Data Source” is selected.  (The SQL Server option will force you to specify the literal SQL Server Name) Type in a friendly name that describes your linked server (without spaces). I use AccountingServer. Provider – Select “Microsoft OLE DB Provider for SQL Server” Product Name – type: SQLSERVER (with no spaces) Datasource – type the actual […]

Continue reading ...

How to Move TempDB

In order to move TempDB, use the alter database command with “modify file” and specify a new path. [cc lang=”sql”] use master go Alter database tempdb modify file (name = tempdev, filename = ‘e:tempdbtempdb.mdf’) go Alter database tempdb modify file (name = templog, filename = ‘e:tempdbtemplog.ldf’) go [/cc] Make sure the folder path already exist otherwise you will get an error. Once complete, an informative message will appear stating that tempdb will be moved the next time SQL is started. [code] The file “tempdev” has been modified in the system catalog. The new path will be used the next time the database is started. The file “templog” has been modified in the system catalog. The new path will be used the next time the database is started. [/code] This will require a restart of the SQL Server Database Engine service.

Continue reading ...

Configuring Parallelism

Parallelism is a powerful feature in SQL Server designed to allow greater bandwidth for high impact queries. Most of the time however, I’ve seen too many queries qualifying for parallelism and essentially bringing the SQL Server box to it’s knees. To understand parallelism, we need to understand SPIDs (Server Process ID’s). These SPID’s act essentially as threads on a SQL Server. Normally there is one SPID assigned to each database connection. Sometimes, if the query is determined to be expensive, a SPID can be split out into multiple execution threads. This is known as parallelism. Cost Threshold for Parallelism Prior to breaking a SPID out into multiple threads, there is a cost determination. This is called the cost threshold for parallelism (set in seconds) and is set at the server-level. The idea is to allow parallelism when a query is estimated to run over a certain number of seconds.  Generally, the determining factor is the estimated number of rows that will be returned either in the main query or an inner query within the execution plan.  To set parallelism right click on the Server Instance in Management Studio. And select “properties”. Once properties is selected, a new window pops up displaying a more detailed server settings. We want to select on “Advanced” Now, in the lower middle of the screen we see the settings for Parallelism. The main two we are concerned with the is the cost threshold and the Max Degree of parallelism. What Settings to Choose? You need […]

Continue reading ...

Featured Articles

 Site Author

  • Thanks for visiting!