Tuesday, November 23, 2010

How To Attach Or Restore DB From MDF And LDF Files After DB Deleted


--HOW TO ATTACH OR RESTORE DB FROM MDF AND LDF FILES AFTER DB DELETED

--If old DB is already deleted,Create a New DB
--Take the database in offline mode
--Then run the following System

SP_DETACH_DB <DatabaseName>

eg:-
EXEC SP_DETACH_DB 'TEMPDATABASE'

--This make the Database in Detach Mode
--Now to bring the database in attached mode
--Your attach statments needs to specify both files in one command, MDF first.then LDF second  Thus, change it to:


Exec SP_ATTACH_DB
@DBNAME = <DatabaseName>,
@FILENAME1 = <Loction.mdf>,
@FILENAME2 = <Location_Log.ldf>

eg:-
Exec SP_ATTACH_DB
@DBNAME = N'TEMPDATABASE',
@FILENAME1 = N'NEW.MDF',
@FILENAME2 = N'NEW_LOG.LDF'

Tuesday, November 16, 2010

How to Update Data of One Table to Other Table

You can use the below query to Update the data of one table by comparing and update that data with another table.


UPDATE A SET myCol=B.myCol 
FROM myTable A  
INNER JOIN myOtherTable B ON A.ID=B.ID 


If you want to update the table of one database with the value of another table of another database,Use the below query.
Nothing is different from the above query with just prefix with the database name


UPDATE A SET myCol=B.myCol 
FROM myDatabase.dbo.myTable A  
INNER JOIN myOtherDatabase.dbo.myOtherTable B ON A.ID=B.ID