Can anybody know how to remove all the indexes from database ?
Using this query will create you a list of DROP statements which you can then execute:
SELECT
'DROP INDEX ' + ix.name + ' ON ' + OBJECT_NAME(ID)
FROM
sysindexes ix
WHERE
ix.Name IS NOT null
That should be pretty fast and take care of dropping all indices :-)
Marc
PS: ah, sorry, I just noticed this will only work in SQL Server 2005 and up. For SQL Server 2000, you'll need to use the "sysindexes" view instead... I updated my statement accordingly
Generate some sql querying the sysindexes table.
some thing along the lines of :
select 'drop index ' + i.name + ' on ' + o.name
from sysindexes i
inner join sysobjects o on i.id = o.id
where o.name <> i.name
The execute the results....
Related
Is there any query in SQL server 2005 that returns the list of Stored procedures in the particular database along with the name of databases whose objects are getting used in the stored procedure.
That is how to get all procedure names:
select *
from DatabaseName.information_schema.routines
where routine_type = 'PROCEDURE'
I will check now, if there is any way to check their code for table names.
you can use this query
it will show all dependencies even to the columns
SELECT
--SP, View, or Function
ReferencingName = o.name,
ReferencingType = o.type_desc,
--Referenced Field
ref.referenced_database_name, --will be null if the DB is not explicitly called out
ref.referenced_schema_name, --will be null or blank if the DB is not explicitly called out
ref.referenced_entity_name,
ref.referenced_minor_name
FROM sys.objects AS o
cross apply sys.dm_sql_referenced_entities('dbo.' + o.name, 'Object') ref
where o.type = 'p'
-- for other database object types use below line
-- o.type in ('FN','IF','V','P','TF')
works for single database
select *
from information_schema.routines
where routine_type = 'PROCEDURE'
This is not a simple thing to do reliably in SQL Server 2005. You might want to look at commercial products such as ApexSQL Clean or SQL Dependency Tracker.
In SQL Server 2008 you could try using the sys.sql_expression_dependencies dynamic management view. For example,
select
quotename(s.name) + N'.' + quotename(o.name) as ProcedureName,
ed.referenced_server_name,
ed.referenced_database_name,
ed.referenced_schema_name,
ed.referenced_entity_name
from sys.sql_expression_dependencies ed
inner join sys.objects o on o.object_id = ed.referencing_id
inner join sys.schemas s on s.schema_id = o.schema_id
where
o.type = 'P'
Hope this helps,
Rhys
I have a database with 1000s of tables. I want to drop all of them except say 15 of them.
Is there a quick way to do this?
You can run the below sql statement and get the list of tables you want then copy and paste the results to actually drop the tables.
SELECT 'drop table ' + t.TABLE_SCHEMA + '.' + T.TABLE_NAME + ';'
FROM INFORMATION_SCHEMA.tables t
WHERE table_name LIKE '%bob%'
I have a requirement where I have to change collation of my DB for that I need to drop all the constraints and recreate them after running the collation change script at my DB. May I know how can I generatescript of all constraints of my DB?
SELECT top 1
'ALTER TABLE '+ SCHEMA_NAME(schema_id) + '.' + OBJECT_NAME(parent_object_id) +
' ADD CONSTRAINT ' + dc.name + ' DEFAULT(' + definition
+ ') FOR ' + c.name
FROM sys.default_constraints dc
INNER JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id
script to generate all constraints
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
get all constraints on db then filter on your table
This will get you all the constraints in the database, you can filter it by what you need:
SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'
I think you might also need to look at any indices, statistics, etc. that might also keep you from dropping the column.
This can be done easily from SQL Server Management Studio.
Right-click on the database and go to Tasks, Generate Scripts....
This will bring up a script generation wizard that will generate DROP and CREATE scripts for whatever schema constructs that you select.
Select your database
Make sure that you select Script Drop to True
Select Tables
Select New Query Editor Window and click Finish.
You can program something like this using SMO or even easier is to just use the script wizard in SSMS. Go to your database in the Object Explorer and right-click on it. Select Tasks->Generate Scripts (NOT "Script Database as"). In the wizard you can make sure that constraints are scripted and remove other items. Check the generated script and make sure that it does what you need. If not, go through the wizard again making the necessary adjustments.
I have created many tables on my local database and moved them to production database.
Now I am working on fine tuning the database and created many constraints on my local database tables such as PK, FK, Default Values, Indexes etc. etc.
Now I would like to copy only these constraints to production database. Is there a way to do it?
Please note that my production database tables already populated with some data. So I can’t drop and recreate them.
If you don't want to buy any tools (which are totally worth their price, BTW), you can always interrogate the system catalog views, and extract the info from there to create scripts you could execute on your new database.
In the case of e.g. the default constraints, this query shows you a list of all the default constraints in your database:
SELECT
dc.name 'Constraint Name',
OBJECT_NAME(parent_object_id) 'Table Name',
c.name 'Column Name',
definition
FROM
sys.default_constraints dc
INNER JOIN
sys.columns c ON dc.parent_object_id = c.object_id
AND dc.parent_column_id = c.column_id
ORDER BY
OBJECT_NAME(parent_object_id), c.name
and based on that, you could of course create a query which would emit T-SQL statements to recreate those default constraints on your target server:
SELECT
'ALTER TABLE ' + OBJECT_SCHEMA_NAME(dc.parent_object_id) + '.' + OBJECT_NAME(dc.parent_object_id) +
' ADD CONSTRAINT ' + dc.name + ' DEFAULT(' + definition
+ ') FOR ' + c.name
FROM
sys.default_constraints dc
INNER JOIN
sys.columns c ON dc.parent_object_id = c.object_id
AND dc.parent_column_id = c.column_id
You'd get something like this (for the AdventureWorks sample DB):
ALTER TABLE dbo.Store ADD CONSTRAINT DF_Store_rowguid DEFAULT((newid())) FOR rowguid
ALTER TABLE dbo.Store ADD CONSTRAINT DF_Store_ModifiedDate DEFAULT((getdate())) FOR ModifiedDate
ALTER TABLE dbo.ProductPhoto ADD CONSTRAINT DF_ProductPhoto_ModifiedDate DEFAULT((getdate())) FOR ModifiedDate
ALTER TABLE dbo.ProductProductPhoto ADD CONSTRAINT DF_ProductProductPhoto_Primary DEFAULT(((0))) FOR Primary
ALTER TABLE dbo.ProductProductPhoto ADD CONSTRAINT DF_ProductProductPhoto_ModifiedDate DEFAULT((getdate())) FOR ModifiedDate
ALTER TABLE dbo.StoreContact ADD CONSTRAINT DF_StoreContact_rowguid DEFAULT((newid())) FOR rowguid
ALTER TABLE dbo.StoreContact ADD CONSTRAINT DF_StoreContact_ModifiedDate DEFAULT((getdate())) FOR ModifiedDate
ALTER TABLE dbo.Address ADD CONSTRAINT DF_Address_rowguid DEFAULT((newid())) FOR rowguid
Of course, you could tweak the resulting T-SQL being output to your liking - but basically, copy&paste those results from the query to your new database, and off you go.
Of course, there are similar system catalog views for foreign key relationships (sys.foreign_keys), check constraints (sys.check_constraints), indexes (sys.indexes and sys.index_columns) and many more.
It's a bit of work - but it can be done on your own time, and you'll learn a lot about SQL Server in the process.
So it's a traditional "make or buy" decision all over again :-)
Marc
The best way would be to store all your DDL code in a source control. Then deploy it to production using tools like dbGhost (my favorite) or SQL Compare
Red Gate's SQL Compare is a popular, non-free way to do this.
Sure this is an old post, but none of the scripts in all the above answers put out the table schemas as well. So it didn't work out the box for my database.
This one does, so it did:
-- ===========================================================
-- Default Constraints
-- How to script out Default Constraints in SQL Server 2005+
-- ===========================================================
-- view results in text, to make copying and pasting easier
-- drop default constraints
SELECT
'ALTER TABLE ' +
QuoteName(OBJECT_SCHEMA_NAME(sc.id)) + '.' + QUOTENAME(OBJECT_NAME(sc.id)) +
CHAR(10) +
' DROP CONSTRAINT ' +
QuoteName(OBJECT_NAME(sc.cdefault))
FROM
syscolumns sc
INNER JOIN
sysobjects as so on sc.cdefault = so.id
INNER JOIN
syscomments as sm on sc.cdefault = sm.id
WHERE
OBJECTPROPERTY(so.id, N'IsDefaultCnst') = 1
-- create default constraints
SELECT
'ALTER TABLE ' +
QuoteName(OBJECT_SCHEMA_NAME(sc.id)) + '.' + QuoteName(OBJECT_NAME(sc.id)) +
' ADD CONSTRAINT ' +
QuoteName(OBJECT_NAME(sc.cdefault))+
' DEFAULT ' +
sm.text +
' FOR ' + QuoteName(sc.name)
+ CHAR(13)+CHAR(10)
FROM
syscolumns sc
INNER JOIN
sysobjects as so on sc.cdefault = so.id
INNER JOIN
syscomments as sm on sc.cdefault = sm.id
WHERE
OBJECTPROPERTY(so.id, N'IsDefaultCnst') = 1
I adapted it from Donabel Santos's blog here.
EDIT and NB: Be sure to run both parts of the query and save the second result set (i.e. ADD CONSTRAINTs) before dropping your default constraints, else you won't be able to re-create them again (no I didn't do that :)
A good and free Microsoft tool. You can export the schema and the data.
Microsoft SQL Server Database Publishing Wizard
Try DBSourceTools. http://dbsourcetools.codeplex.com
It has schema compare function that will help you create an update script.
Bear in mind though, that you should be using source-code control on your entire database.
This is what DBSourceTools was designed to do - help developers bring their databases under source control.
How can I Select all columns from all tables from the DB, like:
Select * From *
in SQL Server 2008???
The table list it´s very very big, and have so many columns, is it possible to do it without writing the column names?
Or maybe make a select that returns the name of the tables.
This SQL will do this...
DECLARE #SQL AS VarChar(MAX)
SET #SQL = ''
SELECT #SQL = #SQL + 'SELECT * FROM ' + TABLE_SCHEMA + '.[' + TABLE_NAME + ']' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES
EXEC (#SQL)
Try this, works fine
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
then you could add
WHERE TABLE_NAME LIKE '' AND COLUMN_NAME LIKE ''
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID where t.name = 'ProductItem' AND C.name like '%retail%'
ORDER BY schema_name, table_name
It is possible to retrieve the name of all columns from sys.columns
It is possible to retrieve the name of all table from sys.tables
But is impossible to retrieve all the data from all the tables. As soon as more than one table is involved in a query, a JOIN is necessary. Unless join conditions are provided, tables are joined as full Cartesian product, meaning each row from each table is matched with each row from ll other tables. Such a query as you request would produce for 10 tables with 10 records each no less than 10e10 records, ie. 100 billion records. I'm sure you don't want this.
Perhaps if you explain what you what to achieve, not how, we can help better.
To select * from each table, one after another, you can use the undocumented but well known sp_msforeachtable:
sp_msforeachtable 'select * from ?'
If you are going to send to Excel, I would suggest you use the export wizard and simply select all the tables there. In the object browser, put your cursor on the database name and right click. Chose Tasks - Export Data and follow the wizard. WHy anyone would want an entire database in Excel is beyond me, but that's the best way. If you need to do it more than once you can save the export in an SSIS package.
In SQL Server 2016 Management Studio ( Version: 13.0.15900.1), to get all column names in a specified table, below is the syntax:
**Select name from [YourDatabaseName].[sys].[all_columns]
where object_id=(Select object_id from [YourDatabaseName].[sys].[tables]
where name='YourTableName')**