How to recover table after drop it in SQL Server - sql

I drop a table in SQL Server using this code:
DROP TABLE temp
Now, I try to recover this table, but I don't know a solution.
Please, tell me a solution for this problem if you know.

If DROP TABLE was executed inside the Transaction and it has not been committed then you can ROLLBACK. But if it isn't, then you need backup of your database. Recover your table from database. If backup is also not present, then search for 3rd party recovery tools.

Related

Can I restore the content of a table after that I deleted all the rows inside it?

I am very new in Microsoft SQL Server and I am not so into databasess.
Yesterday I made an error and I deletd all the rows inside the wrong table (I should delete the records in another table)
So now it is very important to me restore in some way all the deleted records in this table (only these records and not all the DB, if it is possibile in someway).
for completeness the table is named dbo.VulnerabilityWorkaround and have the following fields:
Id: int not null (is the PK)
Description: varchar(max), not null
I think that the SQL Server
retains the information related to the deleted records in a log file (or in something like it, maybe a DB table...I don't know)
Can in some way restore my original dbo.VulnerabilityWorkaround by a query or something like it?
There is the transaction log, but as far as I know that can be used depending on the backup strategy the database instance has, meaning you would have to fire up a restore backup operation.
Other than restoring a previous backup, I don't think you have much options.
Since you just need one table it could be easier to restore a backup to a different server and then copy/move only the data you need using SSIS or Bulk Import/Export.

SQL drop table and re-create and keep data

On our original design we screwed up a foreign key constraint in our table. Now that the table is full of data we cannot change it without dropping all of the records in the table. The only solution I could think of is to create a backup table and put all of the records in there, then delete all the records, alter the table and start adding them back. Any other (BETTER) ideas? Thanks!
Using MS SQL Server
I'm a bit late, just for reference.
If You are using SQL Server Management Studio, You could generate a DROP and RECREATE script with "Keep schema and data" option.
Right click on the desired DB in object explorer
Tasks > Generate scripts
Select the table you want to script
Then clicking on Advanced button
"Script DROP and CREATE" ->"Script DROP and CREATE"
"Types of data to script" -> "Schema and data"
Hope this helps
Here's some pseudo-code. No need to make a backup table, just make a new table with the right constraint, insert your records into it, and rename.
CREATE TABLE MyTable_2
(...field definitions)
<add the constraint to MyTable_2>
INSERT INTO MyTable_2 (fields)
SELECT fields
FROM MyTable
DROP TABLE MyTable
exec sp_rename 'MyTable2', 'Mytable'
Using SQL Server Management Studio (SSMS), you can use it's table designer to specify the final condition of the table. Before saving the changes, have it generate the change script and save that script. Cancel out of the design window, open the script and review it. SSMS may already have generated a script that does everything you need, fixing the primary-foreign key relationship while preserving all existing data. If not, you will have a script, already started, that performs most of what you need to do and should be able to modify it for your needs.
This is your only solution.
Create the backup table, empty the original one, modify the table and then insert step-by-step until you find a violation.
Update All Schema Database Old by new Schema Database .
Create script (Right click on the desired DB in object explorer Tasks > Generate scripts -> select option select specific database objects and tables ->next -> advanced-> option Type of data to script Data only -> ok ->next ->next.) to data only and backup Database to old database
Drop database old and create new database and make new DB is empty .
Excute script of Old Data only on new database .

Copy data only between two databases

Im trying to copy data only between two SQL server 2008 databases. I need to keep the existing stored procs and functions intact and copy data only. The DB schemas are identical but im running into issues with PK's.
I first tried:
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'DELETE FROM ?'
To remove all data. But get
Failure inserting into the read-only column
So i then tried to set IDENTITY_INSERT ON across all tables with:
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'DELETE FROM ?'
EXEC sp_MSForEachTable 'ALTER TABLE ? SET IDENTITY_INSERT ON'
with no luck.
What is the best way to export data only between two databases, leaving the original procs and functions intact?
Thanks.
Edit: Im using SQL Export to copy the data from source to destination. I need to keep the destinations DBs procs and functions, just copy the data only.
Just remove the identity specification from all the table pkeys in the second db.
What is likely happening here is that you have pkey as an identity column in both dbs, and it makes sense to do so in the first, but you cant copy its value into another identity column.
You wouldn't want the pkey as an identity pkey in the second db anyway, then, all your foreign keys wouldn't work.
I would probably approach it from a different angle: by scripting all objects via SQL Enterprise Manager into a file and running this file on a blank database. This way, you'll have all metadata but no actual data in the second database, and you can use it for additional copies in the future.
The error you are getting doesn't seems like a PK violation or an Identity issue. I see two possible causes:
If you are getting the error when trying to insert the data, I would check if the tables have any computed columns. Many programs fail to take them into account when exporting data, and include the computed columns in the insert column list.
If you are getting that error in the delete step, probably you have a trigger that fires on delete, and it try to insert data and fails for some reason (the idea of these triggers is maintain a copy of the deleted data in another location). If that is the case, fix the insert or just disable the trigger.
I went with a varation of both answers this in the end. I used a 3rd database as a temp database.
1)I did a full back up of the database i needed the data from (live)
2)I restored this backup to my temp database.
3)I scripted the database i needed the procs and functions from, only scripting procs and funcs and using DROP and IF INCLUDES.
4)I ran the script from #3 against my temp database giving the data from DB1 and the procs and funcs from DB2
5)I restored DB2, using OVERWRITE from a backup of my temp database.
Thanks guys id mark all as correct if I could.
Hi in order to get around your issues with your constraints, please read this blog post I wrote on the subject.
http://tenbulls.co.uk/2009/07/22/checking-your-constraints-to-check-your-integrity/

Persistent SQL Table lock from C#

I'm trying to create a persistent SQL (SQL Server 2005) lock on a table level. I'm not updating/querying the specified table, but I need to prevent a third party application from updating the locked table as a means to prevent transactions from being posted (the table I wish to lock is the key on their transaction that interferes with my processing).
From my experience the table is only locked for the time a specific transaction is taking place. Any ideas?
The 3rd party developer has logged this feature as an enhancement, but since they are in the middle of rolling out a major release I can expect to wait at least 6 months for this. I know that this isn't a great solution, since their software will fall over but it is of a critical enough nature that we're willing to live with the consequences.
Move the table to a different file group, then alter the file group to read-only. The table will be in effect read-only:
ALTER DATABASE dbName
ADD FILEGROUP ReadOnlyFG;
GO
ALTER DATABASE dbName
ADD FILE (
NAME = ...,
FILENAME = '...')
TO FILEGROUP ReadOnlyFG;
GO
ALTER TABLE tableName MOVE TO ReadOnlyFG;
GO
ALTER DATABASE dbName
MODIFY FILEGROUP ReadOnlyFG READONLY;
rename the table and when the 3rd party is ready rename it back to what it should be

What is the difference between drop and delete database?

What is the difference between drop and delete database?
Difference between DELETE and DROP commands
Delete: The DELETE command is used to
remove rows from a table. A WHERE
clause can be used to only remove some
rows. If no WHERE condition is
specified, all rows will be removed.
After performing a DELETE operation
you need to COMMIT or ROLLBACK the
transaction to make the change
permanent or to undo it. Note that
this operation will cause all DELETE
triggers on the table to fire.
Drop: The DROP command removes a table
from the database. All the tables'
rows, indexes and privileges will also
be removed. No DML triggers will be
fired. The operation cannot be rolled
back.
Simply: a DELETE command removes matching rows, a DROP command removes the entire table.
I know this is an old post, but I was looking for the answer myself, and since I believe the OP has asked about dropping or deleting the database, rather than tables or content, I thought I'd chip in.
Both will delete the database, but both could involve more than one step if you want to completely remove the database and associated artefacts.
DELETE
If you are using SQL Server Management Studio, you can delete the database by right clicking on the database and selecting Delete. The resulting dialog offers a couple of checkboxes :
'Delete backup and restore history information for databases'
'Close Existing connections'
If you don't tick 'Delete backup and restore history..' , those files will remain on the server unless you manually get rid of them.
'Close Existing connections' is a good idea, otherwise you may get an error telling you the database is still in use (even if it's just you, while you're trying to delete it)
DROP
The SQL Command 'DROP' alone will not remove everything. You will still need to also remove the backup history, and set the database to 'single user mode' - or it may complain the database is still in use, as above.
--Remove backup history
EXEC msdb.dbo.sp_delete_database_backuphistory #database_name = N'YourDBName'
GO
USE [master]
GO
--close all the open connections to your database
ALTER DATABASE [YourDBName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
USE [master]
GO
--remove the actual database
DROP DATABASE [YourDBName]
GO
Delete removes content and drop the structure of a database.
Delete removes content of table.Drop removes content and structure of table.Delete can be rolled back but Drop can not be rolled back
Suppose you are using MS SQL
Then if you want to delete the whole Table then you would use:
DROP TABLE MyTable
This would delete the whole table and its constraints
To delete any specific row you would use:
DELETE FROM MyTable WHERE id=5
This would delete the row with the id = 5
If no conditions are matched it would delete all rows
DROP removes the entire table and associated objects from the catalog, requiring you to create all the indexes and constraints from scratch.
Drop in a function that physically removes and particular table or a column. The table struction remails same. If you want to see the structure, you can write this
desc tablename; and the structure will be same after dropping the table.
Delete function permanantly deletes the table or a column, the structure of the table also.