Мне всегда нравится создавать таблицы образцов в [tempdb], чтобы синтаксис был правильным. Я создал таблицы [t1], [t2] и [t3]. Есть первичные и внешние ключи.
Если у вас хорошо развитая схема, ERD (диаграмма отношения сущностей) http://en.wikipedia.org/wiki/Entity-relationship_diagram, эти отношения должны быть на месте.
-- Playing around
use tempdb
go
--
-- Table 1
--
-- Remove if it exists
if object_id('t1') > 0
drop table t1
go
-- Create the first table
create table t1
(
encounter_id int,
patient_id int,
the_date date,
the_time time,
constraint pk_t1 primary key (encounter_id)
);
go
-- Add one row
insert into t1 values (1234, 112233, '2014-01-02', '14:25:01:00');
go
-- Show the data
select * from t1
go
--
-- Table 2
--
-- Remove if it exists
if object_id('t2') > 0
drop table t2
go
-- Create the second table
create table t2
(
encounter_id int,
the_page int,
recorded_on date,
recorded_by int,
constraint pk_t2 primary key (encounter_id, the_page)
);
go
-- Add two rows
insert into t2 values
(1234, 1, '2014-01-02', 134),
(1234, 2, '2014-01-02', 134);
go
-- Show the data
select * from t2
go
--
-- Table 3
--
-- Remove if it exists
if object_id('t3') > 0
drop table t3
go
-- Create the third table
create table t3
(
encounter_id int,
the_page int,
key_name1 varchar(16),
key_value1 varchar(16),
constraint pk_t3 primary key (encounter_id, the_page, key_name1)
);
go
-- Add seven rows
insert into t3 values
(1234, 1, 'key1', 'aaa'),
(1234, 1, 'key2', 'bbb'),
(1234, 1, 'key3', 'ccc'),
(1234, 1, 'key4', 'ddd'),
(1234, 2, 'key5', 'eee'),
(1234, 2, 'key6', 'fff'),
(1234, 2, 'key7', 'ggg');
go
-- Show the data
select * from t3
go
--
-- Foreign Keys
--
alter table t2 with check
add constraint fk_t2 foreign key (encounter_id)
references t1 (encounter_id);
alter table t3 with check
add constraint fk_t3 foreign key (encounter_id, the_page)
references t2 (encounter_id, the_page);
Здесь представлена интересная часть, хранимая процедура для дублирования данных.
--
-- Procedure to duplicate one record
--
-- Remove if it exists
if object_id('usp_Duplicate_Data') > 0
drop procedure t1
go
-- Create the procedure
create procedure usp_Duplicate_Data @OldId int, @NewId int
as
begin
-- Duplicate table 1's data
insert into t1
select
@NewId,
patient_id,
the_date,
the_time
from t1
where encounter_id = @OldId;
-- Duplicate table 2's data
insert into t2
select
@NewId,
the_page,
recorded_on,
recorded_by
from t2
where encounter_id = @OldId;
-- Duplicate table 3's data
insert into t3
select
@NewId,
the_page,
key_name1,
key_value1
from t3
where encounter_id = @OldId;
end
И наконец, мы должны вызвать хранимую процедуру, чтобы убедиться, что она работает.
-- Sample call
exec usp_Duplicate_Data 1234, 7777
Таким образом, я не добавлял ошибок и не учитывал диапазон идентификаторов. Я оставляю эти задания для вас, чтобы учиться.
Что вы пробовали до сих пор ??? вы имеете в виду, что просто хотите изменить Encounter_ID на (сгенерированную системой) встречу для конкретного пациента во всех трех таблицах ??? –