Truncate all tables of a database in SQL Server 2005 - sql-server

How can I truncate all tables of a database?

Why would you want to truncate all tables? If you want an empty database, why not run the CREATE script of the database?
If you want to Truncate a table referenced by a foreign key, you will have to drop the FK constraint first. Disabling constraints is something that is not possible anymore in recent versions of SQL Server.

You can see this post : how-do-you-truncate-all-tables-in-a-database-using-tsql

I use the script
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'DELETE FROM ?'
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO

Reset Auto-Increment? I'm not sure if you understand correctly how this works.
Primary Key incrementing is handled by SQL Server using the IDENTITY specification. If your tables have got no data in them, it will always start from 0.
If I were you, I'd go have a flick through your programming books and pick up some basic database knowledge as it sounds like you're missing some fundamental facts there.

Related

Cannot drop the table <TableName> because it is being used for replication

I am not able to modify the structure of a table in a database.
The database is used for replication.
I am getting This error
As the error says the table is used for replication, meaning it's an article in a replication publisher. To be able to modify the table you should remove it from replication, then update the schema, afterwards add it again in the replication. After adding it again you will have to reinitialize the subscriptions to pick up the modified table schema.
I was having this error on my replication
Cannot drop the table 'dbo.repl_application_camp_choice' because it is
being used for replication. (Source: MSSQLServer, Error number: 3724)
the first thing I tried - wrongly - is to manually drop the table in the subscriber db.
But the same error was there.
the next thing I tried is this:
USE [ORCASTG]
GO
EXEC sp_msunmarkreplinfo 'dbo.repl_application_camp_choice'
--Msg 3724, Level 16, State 3, Line 5
--Cannot drop the table 'dbo.repl_application_camp_choice' because it is being used for replication.
but it did not work
then I tried this one:
USE [ORCASTG]
GO
DECLARE #subscriptionDB AS sysname
SET #subscriptionDB = N'ORCASTG'
USE master
EXEC sp_removedbreplication #subscriptionDB
GO
USE [ORCASTG]
GO
DROP TABLE IF EXISTS [dbo].[repl_application_camp_choice]
GO
and this did the trick
and after running the script above:
I also looked here and here and here.

How to delete all data in each tables on my database

I have a database which consist of 300 tables with data in it. I need to delete all the data inside each tables. I tried to truncate all tables but then I got an error that the process could not be completed because one of the column in a table is a foreign key. Is there other way to resolve my problem? Thanks.
You need to either:
remove all the foreign keys, truncate, then re-create FKs;
disable all the foreign keys, delete (not truncate), then re-enable FKs; or,
delete from child tables first.
The latter may not be possible if you're lucky enough to have circular references, and it can still be complicated even without circular references. The first two are also relatively complex, but I solved a very similar problem for a different user recently (and I find these easier than trying to determine the proper delete order):
Temporarily disable all foreign key constraints
Another idea is to perform a simpler and more complete wipe:
script the tables (and other objects obviously), drop the database and re-create it; or,
create a copy of the database, and use Visual Studio / SSDT or a 3rd party schema comparison tool to create all of the objects in the empty database (then you can drop the old database and rename the new one).
Try this : A quick way of doing it .sp_msforeachtable is an undocumented SP so there's a risk in using them. I came up with this answer using Aaron Logic by disabling the constraints used in his answer.
use [YourDB]
Go
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'Truncate Table ?'
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'

SQL server: what does this sql statement mean

I've been comparing databases using vs 2010 scheme comparing tool and it generated some stuff which is not clear. For example at the end of the script it has this statement:
ALTER TABLE [dbo].[My_table] WITH CHECK CHECK CONSTRAINT [FK_FOREIGN_ID];
Can anyone explain what this means?
It means that existing data should be checked against the constraint when it is added.
Failure to have the CHECK CHECK leaves your constraints untrusted and they cannot be used by the query optimiser.
That tells SQL Server to validate the constraint against new rows. The counter example would be to use WITH NOCHECK to temporarily disable validation checks for new rows.
ALTER TABLE (Transact-SQL) (WITH CHECK | WITH NOCHECK )

Drop all tables in sql server database using ant script

Can anybody help me with this?
I prefer if I don't have to explicitly list the table names.
Saw this before...
exec sp_MSforeachtable "DROP TABLE ? PRINT '? to be dropped' "
Source - http://sqlserver-qa.net/blogs/t-sql/archive/2008/05/20/4266.aspx
How about dropping the database? DROP DATABASE <database name>
Of course, that's rough on stored procedures, triggers, etc. But if the purpose is to eliminate all the tables in order to recreate them, it makes sense that you'd recreate all the other associated components as well, such as indexes.

Changing column name in SQL Server 2000

Is there a way to change a column name in sql without having to recreate the table?
I've tried alter table dbo.Conforming rename column [xxx] to [xxx] and it doesn't work. any other ideas?
use sp_rename:
USE AdventureWorks;
GO
EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';
GO

Resources