Delete columns from multiple tables - sql

I have 4 columns that are repeated in all the tables in the database and I have to delete them
How can I do this deletion without having to enter table by table?

This code will output the necessary SQL to make the changes.
STRING_AGG is used twice to group up the columns and tables. QUOTENAME is used to place brackets around names correctly.
SELECT STRING_AGG(
N'ALTER TABLE ' + QUOTENAME(SCHEMA_NAME(t.schema_id)) + N'.' + QUOTENAME(t.object_id) + N'
' + c.ColumnSql, N'
')
FROM sys.tables t
CROSS APPLY (
SELECT ColumnSql = STRING_AGG(CAST(N'DROP COLUMN ' + QUOTENAME(c.name) AS nvarchar(max), N'
')
FROM sys.columns c
WHERE c.object_id = t.object_id
AND c.name IN (
'ID_Integracion_CodBodega',
'ID_Integracion_FechaUltRep',
'ID_Integracion_ControlTrigger',
'ID_Integracion_CodBodega_Origen'
)
) c
You can execute it all together by using
DECLARE #sql nvarchar(max) = (
SELECT STRING_AGG.....
);
EXEC(#sql);
I caution you against using INFORMATION_SCHEMA because it is only there for compatibility.

SQL Server provides system information schema views that can be queried to retrieve information about the database.
In your case, the COLUMNS view can be used to fetch the names of all tables containing a specific column name.
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN (
'ID_Integracion_CodBodega',
'ID_Integracion_FechaUltRep',
'ID_Integracion_ControlTrigger',
'ID_Integracion_CodBodega_Origen'
);
From there, you can use the normal process to delete a column from an existing table.
ALTER TABLE [table_name] DROP COLUMN [column_name];
You should be very careful with this approach. It is easy to drop a column you didn't mean to.
Be aware of any constraints/dependencies in your database schema that you might be affecting with this action.
Are there constraints on your tables that will be affected by the removal of these columns? (Especially ON DELETE CASCADE constraints that may impact other tables).
Are there views/stored procedures/triggers that depend on these columns?
Do you have queries/dynamic SQL that will be impacted by the removal of these columns?

Related

Alter table structure to match copy table

I have 2 tables corporate and corporate_copy. Initially they were same in structure but people started added new columns into corporate and forgot do do so for corporate_copy.
Somewhere in the application there is less used functionality that copies data from corporate to corporate_copy and that kept failing without anyone noticing. Now I have to add 28 columns (ofcourse with same type and length and constraints etc....).
I know it can be done in one ALTER TABLE statement but I still feel it is lengthy task.
Do we have any luxury that will make copy table same as main table by keeping data and adding default values in newly added columns?
I am asking much but is there anything like that?
--Generate a dynamic query which contain all the missing column list and Execute it
--for eg I tried Something
BEGIN TRAN
DECLARE #SqlSelect NVARCHAR(MAX),#ColumnDeclaration VARCHAR(2000)
SELECT DISTINCT ' '+COLUMN_NAME+' '+ DATA_TYPE +' '+ISNULL(CONVERT(NVARCHAR(10), CHARACTER_MAXIMUM_LENGTH ),'')+' 'Missing_Column INTO #T FROM INFORMATION_SCHEMA.COLUMNS a
WHERE a.column_name not in (SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS b
WHERE b.table_name in ('Corporate_Copy'))
and a.table_name in ('Corporate')
SELECT #ColumnDeclaration=STUFF((
SELECT ', ' + Missing_Column
FROM #T
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(max)'), 1, 1, '')
SET #SqlSelect=' ALTER TABLE Corporate_Copy Add'+ #ColumnDeclaration + ');'
PRINT #SqlSelect
ROLLBACK TRAN
You could use schema compare, found in SQL Server data tools (free) to generate a change script automatically.
But if this is just a copy, you could just run this:
DROP TABLE Corporate_Copy;
SELECT *
INTO Corporate_Copy
FROM Corporate;
It's not clear whether you really need to preserve the data in the copy. If so, it's not really a copy is it?
From SQL-Server 2015, you can use the following query to extract all different columns between 2 tables:
select distinct a.* from INFORMATION_SCHEMA.COLUMNS a
where a.column_name not in (select column_name from INFORMATION_SCHEMA.COLUMNS b
where b.table_name in ('tbl_A'))
and a.table_name in ('tbl_B')
order by a.column_name
The output gives you enough information to create a simple script to add the columns which are missing:
For exmaple:
Alter table tbl_A ADD res.Column_Name res.Data_Type ....
generate CREATE script in SSMS (right-click on table, then "script table as...")
Delete all things that already exists. Usually they are in the begining and it's a simple
change CREATE to ALTER ... ADD
That should be possible using SELECT INTO, for example the following SQL statement creates a backup copy of corporate:
SELECT * INTO corporate_copy
FROM corporate ;

Creating Multiple Tables with the reference of existing tables but with different names

I have a requirement where I need to create multiple tables with the help of some existing tables but the new tables would have some different names.
For example:
Existing Table Names:
INT_Employees,
INT_HumanResource,
INT_Payroll,
INT_Contacts etc..
New Tables should have prefix STG so it should look like:
STG_Employees,
STG_HumanResource,
STG_Payroll,
STG_Contacts etc..
Note: There are around 127 tables and we need to create new 127 tables with prefix STG_.
Thanks in Advance.
If you are just trying to copy the table structure you can use select into with a where predicate that will prevent any rows from being returned.
select * into STG_Employees from INT_Employees where 1 = 0
Just repeat this for each table.
--EDIT--
Here is an example of you can leverage dynamic sql to do this task instead of resorting to loops. We simply use sys.tables to help us generate our sql statement.
declare #sql nvarchar(max) = ''
select #sql = #sql + 'select * into ' + stuff(name, 1, 4, 'STG_') + ' from ' + name + ' where 1 = 0;'
from sys.tables
where name like 'INT[_]%'
select #sql --once your comfortable the sql being generated is correct simply uncomment the next line
--exec sp_executesql #sql
Please note that if you want primary keys, indexes, foreign keys, computed columns etc you will need to generate those scripts another way. This will only generate the table structures.

modifying multiple tables in a database with the same changes

I inherited a large database with many tables and related databases. They had used an identifying value of VARCHAR(8) this is used in different tables.
Now we want to make these improvements:
modify this column to be of type VARCHAR(20)
add a Unique ID that would be an INT.
How can I make a unique ID column and values for multiple tables and databases?
You can run below query in your database and copy result of the query into new window to change data type. Similarly you can build query to add column.
select 'ALTER TABLE ' + t.TABLE_NAME + ' ALTER COLUMN ' + column_name + ' VARCHAR(20)'
from INFORMATION_SCHEMA.TABLES t
join INFORMATION_SCHEMA.COLUMNS c on t.TABLE_NAME = c.TABLE_NAME
where c.COLUMN_NAME = 'Col1'

Add Identity column to all the tables in database Sql server 2005

I have a database that is used to transfer data across the main production database when we import data from a third party.
I would like to add an Identity column to each table.
I know that the below SQL will do for a single table, how can I do it for all the tables in the database?
ALTER TABLE dbo.YourTable
ADD Id INT IDENTITY(1,1) NOT NULL
Many thanks
You'll have to do it one table at a time - there's no "magic" way to do it to all tables at once.
You can have SQL Server generate the T-SQL statements needed for that operation by inspecting the sys.tables catalog view - assuming you want to call that identity column Id for all tables:
SELECT
t.NAME,
'ALTER TABLE [' + SCHEMA_NAME(t.SCHEMA_ID) + '].[' + t.NAME +
'] ADD Id INT IDENTITY(1,1) NOT NULL'
FROM
sys.tables t
Now, copy & paste the resulting lines of this statement and execute those lines against your database - and you're done!
U can use 'sp_MSforeachtable 'And add to Every table in your Database
just run this query:
EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''ColName'')
begin
ALTER TABLE ? ADD ColName <DataType> NULL ; // or NOT NULL DEFAULT
<DefaultValue>;
end';
If you are using an Azure SQL,This Stored Procedure is not available under programmability.
Either you can port it from your master database from your on-prem sql server.Or use this link to the run the stored procedure
https://gist.github.com/metaskills/893599

Select all columns from all tables in SQL Server 2008

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')**