SQL SERVER – Refresh Database Using T-SQL

Yesterday I received following questions on blog. Ashish Agarwal asked following question.

Hi Pinal,

Can we refresh a database (like we do by right clicking database node in object explorer and clicking on refresh) thru SQL Query?
If yes, can you please tell me the query?

Thanks,
Ashish Agarwal

Answer to above question is NO. It is not possible to do the same task using SQL Query.

However, if you have changed some SP or any other object and if they are cached in the database, database can be refreshed using DBCC commands.

Read my previous article about SQL SERVER – Clear SQL Server Memory Caches.

Reference: Pinal Dave (http://www.SQLAuthority.com)

About these ads

12 thoughts on “SQL SERVER – Refresh Database Using T-SQL

  1. Hi Pinal,

    I am using SQL SERVER 2005. So I am working on this project, where I have an ITEM_PURCHASE table and an WAREHOUSE table
    I want users to be able to input data into the ITEM_PURCHASE table thru a form and the WAREHOUSE table will automatically populate from certain fields in ITEM_PURCHASE. In this scenario should I use a trigger or stored procedure? Can someone provide the SQL code to get me started? Can someone help me out here, it will be greatly appreciated.

    Thanks,

    Wakil

  2. Hi.

    I m using SQL Server 2005, 64 bit edition. Our problem is .. We need to restart or Refersh sql services mostly to increase the performence of Serve. My server configration is very good but the performence goes down suddenly. Please help me regarding this issue.

  3. Dear all

    I am using ms sql 2005 for database.

    i want if i make any changes in database or add any thing.

    it should be refresh automatic i don’t have to refresh my website or software just like cricket’s website.

    if someone is using my website it should be automatic refresh in every 1/2 minutes refresh.

    can someone help me.

    regards all

    firoz khan

  4. Hi Wakil

    You can use below code

    begin try

    begin transaction updateITEM_PURCHASE_and_WAREHOUSE

    – tsql here
    insert into ITEM_PURCHASE
    insert into WAREHOUSE

    commit transaction updateITEM_PURCHASE_and_WAREHOUSE

    commit transaction updateITEM_PURCHASE_and_WAREHOUSE
    end try
    begin catch
    IF @@TRANCOUNT > 0
    rollback transaction updateITEM_PURCHASE_and_WAREHOUSE
    insert into errortble (ErrorNumber,ErrorSeverity,ErrorState,ErrorProcedure,Errorline,ErrorMessage)(SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState,ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage)
    SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState,ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage;
    end catch

    Regards
    Jayant Das

  5. ‘Connect to the local, default instance of SQL Server.
    Dim srv As Server
    srv = New Server

    ‘Reference the AdventureWorks database.
    Dim db As Database
    db = srv.Databases(“AdventureWorks”)

    ‘Rename the database
    db.Rename(“AdventureWorks2″)

    ‘Refresh the properties on the database.
    db.Refresh()
    db.Rename(“AdventureWorks”)

  6. Hi Pinal,

    I have to restore a production database in QA. What are the steps to be followed? Please guide. Need to know how to script the login,users.

    Thanks
    Arun

  7. HI Arun/Pinal,

    I am in need of DB Refresh process…
    please provide me the steps to be followed for db refresh? Please guide. Need to know how to script the login,users

  8. Pingback: SQL SERVER – Beginning New Weekly Series – Memory Lane – #002 « SQL Server Journey with SQL Authority

  9. Pingback: SQL SERVER – Weekly Series – Memory Lane – #007 « SQL Server Journey with SQL Authority

  10. Hi,

    I wanna know what will happen to the stored procedures while doing the refreshing process to an instance. Will they also be refreshed? Kindly help me out asap.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s