Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I've a task:

Create SQL script to add the column to the database and update it immediately. But this script could be started more then one time - so it is required to this (add and update) only first time.

Here is my solution:

SQL
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Table1' AND COLUMN_NAME = 'PlateDistance')
BEGIN
	ALTER TABLE [OmniCalc].[dbo].[Table1]
	ADD [PlateDistance] decimal(18,0) default 0 NOT NULL;
	
	UPDATE [OmniCalc].[dbo].[Table1]
	SET [PlateDistance] = [bauteilAbstand] / 2
END

GO

But in result i get:

Msg 207, Level 16, State 1, Line 13
Invalid column name 'PlateDistance'.

I've tried to do some changes to allow GO batch processing, but in this case it is impossible to avoid situation when scripts overwrite user data by scenario:

1. Start script

2. User changes some data

3. Start script again

SQL
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
	WHERE TABLE_NAME = 'Table1' AND COLUMN_NAME = 'PlateDistance')
BEGIN
	ALTER TABLE [dbo].Table1
	ADD [PlateDistance] decimal(18,0) default 0 NOT NULL;
	
	Select [PlateDistance] from [dbo].Table1
END

GO

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
	WHERE TABLE_NAME = 'Table1' AND COLUMN_NAME = 'PlateDistance')
BEGIN
	UPDATE [dbo].Table1
	SET [PlateDistance] = [bauteilAbstand] / 2
END

GO


And even if I add variable to the construction - It does not help because variable is loss after GO operator.

SQL
DECLARE @created BIT;

SET @created = 0;

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
	WHERE TABLE_NAME = 'Table1' AND COLUMN_NAME = 'PlateDistance')
BEGIN
	ALTER TABLE [dbo].Table1
	ADD [PlateDistance] decimal(18,0) default 0 NOT NULL;
	
	Select [PlateDistance] from [dbo].Table1
	
	SET @created = 1;
END

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
	WHERE TABLE_NAME = 'Table1' AND COLUMN_NAME = 'PlateDistance') AND @created = 1
BEGIN
	UPDATE [dbo].Table1
	SET [PlateDistance] = [bauteilAbstand] / 2
END

GO



So. Question - how to ADD column and UPDATE its data immediately only on the first script run?
Posted

Use Sql Triggers...

SQL
delimiter $$
CREATE TRIGGER stu_insert
AFTER insert ON stu_table FOR EACH ROW
BEGIN

	INSERT into stu_log(user_id, description)
        VALUES (user(), CONCAT('Insert Student record ',
	new.stu_id,' ',new.stu_name,' ',new.stu_class));

END$$
delimiter ;
 
Share this answer
 
v2
If you don't want a trigger, you may try this:

SQL
create table Table1 ([bauteilAbstand] int)
insert Table1 values (2)
insert Table1 values (4)
insert Table1 values (6)
go
-- Use a temporary constraint to initialize the column to an invalid value.
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Table1' AND COLUMN_NAME = 'PlateDistance')
BEGIN
    ALTER TABLE [dbo].[Table1]
    ADD [PlateDistance] decimal(18,0) not null, 
    constraint df_PlateDistance_tmp default -12345678987654321 for PlateDistance;
END
GO
-- Set the new column to the desired value wherever it has the invalid value.
update Table1 
set [PlateDistance] = [bauteilAbstand] / 2 
where [PlateDistance] = -12345678987654321
go
select * from Table1
go
-- If the temporary constraint still exists...
if exists (select * from sys.objects where object_id=object_id(N'df_PlateDistance_tmp') )
begin
    -- Modify the default constratint in the new column to be what it should be: notice its name also changes.
    ALTER TABLE [dbo].[Table1]
    drop constraint df_PlateDistance_tmp;

    ALTER TABLE [dbo].[Table1]
    add constraint  df_PlateDistance default 0 for PlateDistance
end
go
-- exec sp_help table1
go
-- DROP TABLE Table1
go


Hope this helps,
Pablo.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900