April 4, 2014 at 4:49 am
need to delete a column named "SSMA_TimeStamp", this column could be in some tables of db. My idea is to use sp_MSforeachtable procedure to delete the field "SSMA_TimeStamp" of the tables that this field appear. I'm trying to do this on this way.
set @mycommand='IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME="SSMA_TimeStamp")'
sp_MSforeachtable
@command1='ALTER TABLE ? DROP COLUMN SSMA_TimeStamp
@whereand = 'IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME="SSMA_TimeStamp")'
But it return an error
Someone can help me?
Thanks in advance.
Arsenio.
April 4, 2014 at 5:30 am
What error you are getting?
sp_MSforeachtable will perform the execution on all the tables but you need to remove the column from some tables?
---------------------------------------------------
"Thare are only 10 types of people in the world:
Those who understand binary, and those who don't."
April 4, 2014 at 5:34 am
since this is a one off, i would generate the statements, and copy/paste/run like this:
select
'ALTER TABLE '
+ quotename(object_name(object_id))
+ ' DROP COLUMN '
+ quotename(name)
from sys.columns
where name = 'SSMA_TimeStamp'
Lowell
April 4, 2014 at 10:16 am
EXEC sp_MSforeachtable '
IF EXISTS ( SELECT TOP (1) * FROM sys.columns
WHERE object_id(''?'') IS NOT NULL AND
name = ''SSMA_TimeStamp'')
BEGIN
PRINT ''Altering table "?" to remove column "SSMA_TimeStamp"''
--ALTER TABLE ? DROP COLUMN SSMA_TimeStamp
END
'
Edit: I commented out the ALTER TABLE so you could do a test run to verify. Naturally uncomment the ALTER when you're ready to actually make the table changes.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
April 4, 2014 at 3:11 pm
Thanks for your reply. I have run the sript but I get this error "Error in ALTER TABLE DROP COLUMN.The column 'SSMA_TimeStamp' not exist in table 'Doctors'. I had cretaed a DB whith 3 tables to try the script.
CREATE TABLE [dbo].City(
[Id] [int] NULL,
[LastName] [varchar](255) NULL,
[FirstName] [varchar](255) NULL,
SSMA_TimeStamp varchar(30)
) ON [PRIMARY]
CREATE TABLE [dbo].Doctors(
[Id] [int] NULL,
[LastName] [varchar](255) NULL,
[FirstName] [varchar](255) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].Persons(
[Id] [int] NULL,
[LastName] [varchar](255) NULL,
[FirstName] [varchar](255) NULL,
SSMA_TimeStamp varchar(30)
) ON [PRIMARY]
As you can see table Doctors don't have column "SSMA_TimeStamp". In my truly DB it happens, it means that in DB there are tables that have column "SSMA_TimeStamp" and others don't have this column.
Have you any idea to change the script you posted?
Thanks again
April 4, 2014 at 3:22 pm
D'OH, sorry, quite right, here's a
CORRECTED script:
EXEC sp_MSforeachtable '
IF EXISTS ( SELECT * FROM sys.columns
WHERE object_id = OBJECT_ID(''?'') AND name = ''SSMA_TimeStamp'' )
BEGIN
PRINT ''Altering table "?" to remove column "SSMA_TimeStamp"''
--ALTER TABLE ? DROP COLUMN SSMA_TimeStamp
END
'
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
April 6, 2014 at 10:43 pm
Yes, your error is very correct as there is no column called 'SSMA_TimeStamp' on the table 'doctors'.
---------------------------------------------------
"Thare are only 10 types of people in the world:
Those who understand binary, and those who don't."
April 7, 2014 at 12:21 am
Thanks you. Now It's work fine.
I appreciateso much your solution. Now I can resolve the trouble I had have and I can continue with my work.
Greetings,
Arsenio.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy