all groups > sql server programming > february 2005 >
You're in the

sql server programming

group:

Query problem



Query problem ALI-R
2/28/2005 7:09:54 PM
sql server programming: I have three tables as follows:

Table1:
ClientID StudentID Name
----------------------------
1 22 John
2 23 Chen
3 34 Ted
4 22 Bod

Primary Key=ClientID + StudentID

Table2:
InternalID ClientID StudentID Name
---------------------------------------
001 5 22 John
002 2 23 Chen

Primary Key=InternalID
Unique Constraint= ClientID + StudentID


Table3:
InternalID ClientID StudentID Name DateTransfered
----------------------------------------------
001 1 22 John 2/2/05

Primary Key=InternalID
Unique Constraint= ClientID + StudentID


Here is what I'd like to do:

I'd like to check every single record in Table1 with Table2 and Table 3
if certain rules arre met,then I'd insert the record into Table2 and if not
set a flag on the record itself in Table1 and move to the next record(for
instance the record wasn't inserted)

How can I do that???

Thanks

Re: Query problem ALI-R
2/28/2005 8:48:56 PM
Sorry I should give you a better information on Table1:

Table1:
ClientID StudentID Name Checked? Inserted? Updated?
--------------------------------------------------------------
1 22 John 0 0 0
2 23 Chen 0 0 0
3 34 Ted 0 0 0
4 22 Bod 0 0 0

,ok now here is a one of the rules:

I'd like to check if ClientID of each record exist in Table3 or not,if yes
is the StudentId different with the current record in Table1 or not ,if they
are the same ,I should ignore the record and move to the next record ,if
they are not the same I should insert the current record(in Table1) into
Table 2 and set the following flags in the current record
checked=1,Inserted=1.

So I make the comparisons with Table3 but I insert the record into Table2.

Thanks

[quoted text, click to view]

Re: Query problem Madhivanan
2/28/2005 9:38:14 PM
Check whether this works for you

Insert Into Table2 Select T1.*,1,1 from Table1 T1, Table3 T3 where
T1.ClientId=T3.ClientId and T1.StudentId<>T3.StudentId

Madhivanan
Re: Query problem Adam Machanic
2/28/2005 10:18:56 PM
[quoted text, click to view]

What rules? What flag? Can you post an example of what you actually
want to do?


--
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--


Re: Query problem ALI-R
2/28/2005 11:07:05 PM
How about updating the flags in Table1?
Thanks for your help
[quoted text, click to view]

Re: Query problem Madhivanan
3/1/2005 12:01:01 AM

What do you want to update on Table1?

Madhivanan
RE: Query problem Tobbe
3/1/2005 12:47:02 AM
Hi,
How about an "instead of" trigger on the table itself.

/Tobbe

create trigger EnforceRule on table2
instead of insert
as

--Rules for table1
if exists
(
select *
from table1 t1
join inserted i
on i.clientid = t1.clientid
and i.studentid = t1.studentid
)
begin
update t1
set t1.checked = 1
from table1 t1
join inserted i
on i.clientid = t1.clientid
and i.studentid = t1.studentid
end
--Rules for table3
else if exists
(
select *
from table3 t3
join inserted i
on i.internalid = t3.internalid
)
begin
...
end
--If no rules apply
else begin
insert table1
select *
from inserted
end

RE: Query problem Tobbe
3/1/2005 1:53:04 AM
oops, correction on the last statement ...

insert table2
select *
from inserted


[quoted text, click to view]
Re: Query problem RayAll
3/1/2005 9:01:50 AM
There are some flags in Table1 that need to be updated ,,for instance if any
record from Table1 is inserted into Table2 ,the equivalent flag needs to be
set,or if it's ignored same thing must happen
thanks
[quoted text, click to view]

Re: Query problem RayAll
3/1/2005 1:57:51 PM
Have you ever used this? How is the performance?

Thanks for the reply

[quoted text, click to view]

Re: Query problem Tobbe
3/2/2005 12:59:06 AM
I normally avoid using "instead of" triggers but it seems as a pretty good
way to solve your problem.
I use "instead of" triggers when I can't control the inserts (maybe from
other programs...) but want to log data inserted in my tables.
Converting rows to fit my tables constraints and log rows which do not
follow the constraints... this is usually bad practice because the inserts
should follow your table structure from the start. But sometimes you have no
choice.

I think the performance is pretty good because we use the primary keys all
the way.
But as always, the only way to know is to try :)
I don't know if there is som special performance issues regarding "instead
of" triggers but all you gurus out there certainly has something to say.

if the rule for a table is the same as the marking you can use the following
trigger. In this we only access the tables once, instead of twice as I did in
the previous, once for the rulecheck and once for the actual update of the
marking.
The downside is that it fires triggers in table1 and table3 even if no rows
are affected so us the "if @@rowcount != 0" in the beginning of each trigger
to ensure that no unnecessary sql is executed.


/Tobbe

create trigger EnforceRule on table2
instead of insert
as

if @@rowcount != 0 --Only trigger when rows are actually inserted.
@@rowcount contains the last batch (The actual insert statement)

--Rules for table1
update t1
set t1.checked = 1
from table1 t1
join inserted i
on i.clientid = t1.clientid
and i.studentid = t1.studentid

--Rules for table3
if @@Rowcount = 0
begin
update t3
set t3.checked = 1
from table3 t3
join inserted i
on i.internalid = t3.internalid

if @@Rowcount = 0
begin
insert table2
select *
from inserted
end
end


/*
--TEST
create table table1(clientid int, studentid int, checked int)
create table table2(clientid int, studentid int, internalid int)
create table table3(internalid int,checked int)

insert table1 values(1,1,0)
insert table3 values(1,0)

insert table2 values(1,1,1)
insert table2 values(2,1,1)
insert table2 values(2,1,2)

select * from table1
select * from table2
select * from table3
*/

[quoted text, click to view]
AddThis Social Bookmark Button