SQLTeam.com | Weblogs | Forums

Insert new columns to the table


#1

Hi,
I need to Insert the new columns to the table , i have already 200 records in the table.
The requirement is only add the new values based "version values." The idea is add new values and do not replace the existing column values.

Table Name : VersionManagement

i have maintained 12 w and 12,, then i should have 152w and 152. Do not replace the exising columns. Instead add this new value .

Id is Unique Numbeer (NEWID ( )), NameID from Other Tables.(Reference Key).

could you please help me, how add the new values..


#2
ALTER TABLE VersionManagement ADD new_column varchar(10)

not quite sure what do you mean by that

maybe like this

UPDATE VersionManagement
SET  new_column = case when Version = '12' then '152' else '152w' end
WHERE Version in ('12', '12w')

#3

Hi,

Thanks for reply.

please refer my below expected Table output,

now the total table record is 8 (4+4).


#4

So you mean new row / records not column.

INSERT INTO VersionManagement (
	Deleted,
	ID,
	NameID,
	Version
	)
SELECT [deleted],
	newid(),
	NameID,
	CASE 
		WHEN Version = '12'
			THEN '152'
		ELSE '152w'
		END
FROM VersionManagement
WHERE Version IN (
		'12',
		'12w'
		)

#5

:smile: Thanks, its working Fine. i got the expected Result..


#6

HI,

we need to check also NULL values, if version = NULL, no need new records/row.


#7

The WHERE condition would have excluded that

WHERE Version IN (
		'12',
		'12w'
		)