Showing posts with label SQL Server Management. Show all posts
Showing posts with label SQL Server Management. Show all posts

Friday, November 21, 2008

Create SQL Job to shrink database

If you are a newbie in SQL database, this post may be helpful for you to create SQL job in SQL Server 2000. SQL Jobs are similar to Windows Scheduled job. It also can be scheduled to run hourly, daily, weekly or monthly as per required, provided that SQL Server agent is on and no other external things stops the execution.

 

  1. Open the SQL Server Enterprise Manager.
  2. Go to Microsoft SQL Servers -> SQL Server Group (or other group) -> Open the node of required SQL Server running -> Open Management folder -> Open SQL Server Agent -> Jobs.
  3. In the Right Pane, right click and select New Job.
  4. In General tab, give an appropriate name (say "Shrink Database – testdb") for new job and select owner.
  5. In Steps tab, click New to open a pop-up window.
  6. Write an appropriate name for step (say "Step 1").
  7. Type the required query to shrink database in Command multiline box.

             DBCC SHRINKDATABASE (N'testdb', 10)

             It shrinks database named "testdb". But, it keeps 10% free space in the database.

             It gives the freed space to operating system for its use.

 

  1. On Advanced tab, you can choose what to do on successful completion of job and on failure. For now, you can leave it to default.
  2. Click Ok to save and return to main pop up window of "Shrink Database – testdb".
  3. On Schedule tab of the pop up, click New Schedule.
  4. Give the name of new schedule (say "Shrink Database – testdb").
  5. Select Recurring and click Change.
  6. Select frequency of job and time for the job and click Ok to return.
  7. Click Ok to return to main pop up window "Shrink Database – testdb".
  8. If you have configured SQL main operator, you can send mail on different stages of job execution through Notification tab. I will discuss more on it in my forth coming posts. For now, you can leave it.
  9. Click Ok to save the job and you are done.
  10. Check if the job is executed on time or not and executed properly or not.
  11. You can always run any job instantly by right clicking the job and selecting Start Job.

 

Shrinking database through SQL Jobs can definitely remove burden on database administrators to manually shrink database each time. I hope this post is helpful for new comers to create basic jobs in SQL Server.

 

 

Read more...

Tuesday, November 11, 2008

Transport SQL Data from a server to another server

Sometimes, you may want to copy data from a table of a server to another table to another server which is not connected to source server. What will you do?

 

In fact, it is not much a deal. It can be easily done with the help of "Import and Export Data" utility provided by SQL Server 2000.

 

Just open the utility, select the source data source (Microsoft OLE DB Provider), source SQL Server name and database name.

 

Then, select the destination data source as Microsoft Excel (any version), Access or Text file.

 

Then, select "Copy Tables" option and click "Next".

Finally, select the table(s) you want to copy. If you use Text file as destination, then you can choose only one table from source to export.

 

The process can be reversed for importing data from exported Text File or Excel. You have to choose the Text file or Excel as source and SQL Server as destination this time.

 

SQL Sever uses bulk insert in background to achieve this. It works fine under normal conditions. For this to work fine, of course, source and destination tables should have same structure or destination tables should have columns of same data type as source tables. The mapping can be done at the time of importing.

 

If you are using table with column of "uniqueidentifier" data type, then it is preferable to use Excel for import export because while using Text file it converts the values to string and creates problem while importing string to column of datatype uniqueidentifier.

 

Under default options, this utility appends new imported rows to destination tables. So, if it contains identity and uniqueidentifier columns, then chances are there that after importing, it contains duplicate identities and uniqueidentifier. However, there are options to delete all rows first or drop and create a new destination table.

 

If the destination tables contain some constraints which prevent from inserting import data, then error is thrown and import fails.

 

Finally, SQL Import and Export Data is an excellent utility to copy raw data from one location to another location not having direct connection.

 

 

Read more...