|
You can use the ability to copy records in order to get
records from two or more tables and add them to a another table.
The formula to follow is:
INSERT INTO TableName
SELECT WhatField(s) FROM OneTable
UNION [ALL]
SELECT WhatField(s) FROM AnotherTable;
Here is an example:
USE LPM1;
GO
INSERT INTO Listing.Apartments
SELECT props.PropertyNumber,
props.[Address],
props.UnitNumber,
props.City,
props.[State],
props.ZIPCode,
props.Bedrooms,
props.Bathrooms,
props.MonthlyRate,
props.SecurityDeposit
FROM Listing.Properties props
UNION
SELECT condos.CondoCode,
condos.[Address],
condos.CondoNbr,
condos.City,
condos.[State],
condos.PostalCode,
condos.Beds,
condos.Baths,
condos.Rent,
condos.SecDepot
FROM Listing.Condominiums condos
UNION
SELECT alex.ApartmentCode,
alex.[Address],
alex.ApartNbr,
alex.City,
alex.[State],
alex.ZIP,
alex.Bedrooms,
alex.Bathrooms,
alex.MonthRent,
alex.InitialDeposit
FROM Listing.AlexandriaApartments alex;
GO
Here is an example that displays the results
USE LPM1;
GO
SELECT aparts.PropertyNumber N'Prop #',
aparts.[Address],
aparts.UnitNumber N'Apart #',
aparts.City,
aparts.[State],
aparts.ZIPCode N'ZIP Code',
aparts.Bedrooms Beds,
aparts.Bathrooms Baths,
aparts.MonthlyRate Rent,
aparts.SecurityDeposit Deposit
FROM Listing.Apartments aparts;
GO
ss F5
|
Practical
Learning: Updating the Database
|
|
- Type the following code:
USE LPM2;
GO
/* Change the property number where a record from Alexandria has
the name property number as an apartment from Rockville */
UPDATE Listing.Properties
SET PropertyNumber = N'3840-0002'
WHERE (PropertyNumber = N'3840-8262') AND (City = N'Alexandria');
GO
UPDATE Listing.Properties
SET PropertyNumber = N'6200-0008'
WHERE (PropertyNumber = N'6200-8048') AND (City = N'Alexandria');
GO
UPDATE Listing.Properties
SET PropertyNumber = N'3840-0003'
WHERE (PropertyNumber = N'3840-0683') AND (City = N'Alexandria');
GO
UPDATE Listing.Properties
SET PropertyNumber = N'6286-0008'
WHERE (PropertyNumber = N'6286-6408') AND (City = N'Alexandria');
GO
-- Harmonize the security deposits based on the rate of each house
UPDATE Listing.Properties
SET SecurityDeposit = 500 WHERE MonthlyRate >= 800;
GO
UPDATE Listing.Properties
SET SecurityDeposit = 600 WHERE MonthlyRate >= 900;
GO
UPDATE Listing.Properties
SET SecurityDeposit = 700 WHERE MonthlyRate >= 1000;
GO
UPDATE Listing.Properties
SET SecurityDeposit = 800 WHERE MonthlyRate >= 1200;
GO
UPDATE Listing.Properties
SET SecurityDeposit = 1000 WHERE MonthlyRate >= 1500;
GO
-- Add to the Rockville table of tenants the missing columns from the Alexandria table of customers
ALTER TABLE Rentals.Tenants
ADD DateCreated date;
GO
ALTER TABLE Rentals.Tenants
ADD DateCreated date;
GO
ALTER TABLE Rentals.Tenants
ADD EmergencyName nvarchar(40);
GO
ALTER TABLE Rentals.Tenants
ADD EmergencyPhone nvarchar(20);
GO
/* Change the tenants account numbers from Alexandria
where they are the same as tenants in Rockville */
UPDATE Rentals.Customers
SET AccountNumber = N'292000'
WHERE AccountNumber = N'292475';
GO
UPDATE Rentals.Customers
SET AccountNumber = N'293000'
WHERE AccountNumber = N'293759';
GO
UPDATE Rentals.Customers
SET AccountNumber = N'295000'
WHERE AccountNumber = N'295804';
GO
UPDATE Rentals.Customers
SET AccountNumber = N'524000'
WHERE AccountNumber = N'524794';
GO
UPDATE Rentals.Customers
SET AccountNumber = N'824000'
WHERE AccountNumber = N'824857';
GO
-- Copy the Alexandria tenants to those in Rockville
INSERT INTO Rentals(TenantCode, DateCreated, FirstName, LastName,
MaritalStatus, NumberOfChildren, PhoneNumber,
EmailAddress, EmergencyName, EmergencyPhone)
SELECT AccountNumber, DateCreated, FirstName, LastName, [Status],
Children, Phone, Email, EmergencyName, EmergencyPhone
FROM Rentals.Customers;
GO
/* Change the records of the Alexandria registrations
based on the properties numbers and the accounts numbers
that were changed earlier */
UPDATE Rentals.Allocations
SET ApartmentNumber = N'3840-0002', AccountNumber = N'295000'
WHERE AllocationID = 2;
GO
UPDATE Rentals.Allocations
SET ApartmentNumber = N'6200-0008', AccountNumber = N'824000'
WHERE AllocationID = 5;
GO
UPDATE Rentals.Allocations
SET ApartmentNumber = N'3840-0003', AccountNumber = N'292000'
WHERE AllocationID = 7;
GO
UPDATE Rentals.Allocations
SET ApartmentNumber = N'6286-0008', AccountNumber = N'293000'
WHERE AllocationID = 9;
GO
-- Create a new Registrations table to use an identity column
CREATE TABLE Rentals.Agreements
(
AgreementID int identity(1, 1) not null,
RegistrationDate Date,
EmployeeNumber int, -- Processed By
TenantCode nvarchar(10), -- Processed For
PropertyNumber nvarchar(10) not null,
RentStartDate date
);
GO
-- Add the Rockville regustrations to this table
INSERT INTO Rentals.Agreements(RegistrationDate, EmployeeNumber,
TenantCode, PropertyNumber, RentStartDate)
SELECT RegistrationDate, EmployeeNumber, TenantCode, PropertyNumber, RentStartDate
FROM Rentals.Registrations
GO
-- Then add the Alxandria registrations to the new table
INSERT INTO Rentals.Agreements(RegistrationDate, TenantCode, PropertyNumber, RentStartDate)
SELECT DateAllocated, AccountNumber, ApartmentNumber, DateOccupiedFrom)
FROM Rentals.Allocations;
GO
/* Change the registrations numbers of the Alexandria tenants based on
the new identity numbers of the Agreements table */
UPDATE Rentals.Receipts
SET PaymentForAllocationNumber = 11
WHERE PaymentForAllocationNumber = 2;
GO
UPDATE Rentals.Receipts
SET PaymentForAllocationNumber = 12
WHERE PaymentForAllocationNumber = 5;
GO
UPDATE Rentals.Receipts
SET PaymentForAllocationNumber = 13
WHERE PaymentForAllocationNumber = 7;
GO
UPDATE Rentals.Receipts
SET PaymentForAllocationNumber = 14
WHERE PaymentForAllocationNumber = 9;
GO
UPDATE Rentals.Receipts
SET PaymentForAllocationNumber = 15
WHERE PaymentForAllocationNumber = 12;
GO
UPDATE Rentals.Receipts
SET PaymentForAllocationNumber = 16
WHERE PaymentForAllocationNumber = 15;
GO
-- Add the Alexandria payments to the Roclville's
INSERT INTO Rentals.Payments(PaymentDate, EmployeeNumber, RegistrationNumber, AmountPaid, Notes)
SELECT PaymentAmt, PaymentForAllocationNumber, DateReceiptMade, ReceiptDescription
FROM Rentals.Receipts;
GO
- To execute, on the main menu, click Query -> Execute
- Close Microsoft SQL Server
- When asked whether you want to save, click No
|
|