In SQL Server 2000, I am trying to horizontally partition data into 2 tables using a check constraint on a datetime column as the partition key. The tables are unioned together by a view. There is no primary key in the base tables, so the view is not updatable. An instead-of-insert trigger is built on the view that inserts into the base tables whenever an attempt is made to insert into the view. The base tables are local (in the same database; not distributed). Details;
use testdb
drop table a
CREATE TABLE a (
ClientUserName varchar (32) NOT NULL ,
logTime datetime NOT NULL CHECK (datepart(wk,logtime)=1 OR datepart(wk,logtime)=2)
)
drop table b
CREATE TABLE b (
ClientUserName varchar (32) NOT NULL ,
logTime datetime NOT NULL CHECK (datepart(wk,logtime)=3 OR datepart(wk,logtime)=4)
)
create view ab as
SELECT clientusername,logtime FROM a
UNION ALL
SELECT clientusername,logtime FROM b
create trigger abtrig on ab instead of insert
as
if exists( select * from inserted where datepart(wk, logtime) >4)
begin
raiserror('Bad record', 16,1)
rollback transaction
end
else
begin
if exists (select * from inserted where
datepart(wk,logtime)=1 OR datepart(wk,logtime)=2)
insert into a
select * from inserted
if exists (select * from inserted where
datepart(wk,logtime)=3 OR datepart(wk,logtime)=4)
insert into b
select * from inserted
commit transaction
end
go
From query analyzer, if I insert into the view by running one statement at a time, the partitioning works:
insert ab clientusername, logtime values ('abcd',datepart(wk, getdate() - 50))
select * from a gives one row.
select * from b gives no rows.
But when trying to batch insert several rows at one go;
insert ab clientusername, logtime values ('abcd',datepart(wk, getdate() - 50))
insert ab clientusername, logtime values ('abcd',datepart(wk, getdate() - 45))
insert ab clientusername, logtime values ('abcd',datepart(wk, getdate() - 40))
only the first row is inserted. The other 2 inserts silently fail. What has gone wrong?
use testdb
drop table a
CREATE TABLE a (
ClientUserName varchar (32) NOT NULL ,
logTime datetime NOT NULL CHECK (datepart(wk,logtime)=1 OR datepart(wk,logtime)=2)
)
drop table b
CREATE TABLE b (
ClientUserName varchar (32) NOT NULL ,
logTime datetime NOT NULL CHECK (datepart(wk,logtime)=3 OR datepart(wk,logtime)=4)
)
create view ab as
SELECT clientusername,logtime FROM a
UNION ALL
SELECT clientusername,logtime FROM b
create trigger abtrig on ab instead of insert
as
if exists( select * from inserted where datepart(wk, logtime) >4)
begin
raiserror('Bad record', 16,1)
rollback transaction
end
else
begin
if exists (select * from inserted where
datepart(wk,logtime)=1 OR datepart(wk,logtime)=2)
insert into a
select * from inserted
if exists (select * from inserted where
datepart(wk,logtime)=3 OR datepart(wk,logtime)=4)
insert into b
select * from inserted
commit transaction
end
go
From query analyzer, if I insert into the view by running one statement at a time, the partitioning works:
insert ab clientusername, logtime values ('abcd',datepart(wk, getdate() - 50))
select * from a gives one row.
select * from b gives no rows.
But when trying to batch insert several rows at one go;
insert ab clientusername, logtime values ('abcd',datepart(wk, getdate() - 50))
insert ab clientusername, logtime values ('abcd',datepart(wk, getdate() - 45))
insert ab clientusername, logtime values ('abcd',datepart(wk, getdate() - 40))
only the first row is inserted. The other 2 inserts silently fail. What has gone wrong?