Tuesday, July 15, 2008

Transact-SQL to Create, Add Entries To, and Update a Table

Today we realized we need to change the entries in a reference table because the .ASPnet developers need to call one of the categories in the reference table something else. You can make entries right in the tables in SQL but its a better practice to script out such changes.

Well, I had to figure out how to do that because I've never had to change just one record column entry string into something else. Here's my practice on that.

--first I created a sample table to test against--

create table Managers
(FirstName varchar (100) not null,
LastName varchar (100) not null,
OfficeLocation varchar (50) null)

--then I inserted some content--

insert into Managers (FirstName, LastName, OfficeLocation)
values ('Jason', 'Peabody', 'New York')

insert into Managers (FirstName, LastName, OfficeLocation)
values ('Heather', 'Ace', 'Burbank')

insert into Managers (FirstName, LastName, OfficeLocation)
values ('John', 'Grass', 'Burbank')

select * from Managers


--now I want to go back and change all entries for Burbank to Beverly Hills because we've leased a new building and everyone moved into it this week. First I run a select all to see how many records there are to change.--

SELECT *
FROM Managers
WHERE OfficeLocation = 'Beverly Hills'

UPDATE Managers--the tablename
SET OfficeLocation = 'Beverly Hills'--the new entry
WHERE OfficeLocation = 'Burbank'--the old entry

select * from Managers --check it

No comments: