General information
Forum:
Microsoft SQL Server
A trigger fires ONCE, no matter how may records are being updated. Those lines ensure that each parent that is updated gets it's childcount field updated.
>Thanks Bob.
>
>I did run the sql manually so the update alone worked fine.
>WRT your sample, I don't understand what the last two lines accomplish
>
>from parent
> join inserted on parent.pcode=inserted.pcode
>
>If its not too much trouble, could you attempt to enlighten me?
>
>Thanks,
>-Isaac
>
>
>>update parent
>>set childcount =
>> (select COUNT(item_code)
>> from child
>> where child.pcode = parent.pcode)
>>from parent
>>
>>If you do this in an insert trigger to the child you can do something like...
>>
>>update parent
>>set childcount =
>> (select COUNT(item_code)
>> from child
>> where child.pcode = parent.pcode)
>>from parent
>> join inserted on parent.pcode=inserted.pcode
>>
>>**********
>>BOb
>>
>>
>>>Hello,
>>>
>>>I'm trying to figure out the syntax for updating a count column based on the child table count.
>>>Parent table has the columns columns pcode, childcount
>>>Child table has the columns pcode, childname
>>>
>>>I'm thinking I want to do something like the following, though I'm stuck
>>>figuring out how to finish the sql
>>>
>>>update parent
>>>set childcount =
>>> (select COUNT(item_code)
>>> from child
>>> where child.pcode = parent.pcode)
>>>
>>>How do I tell the subquery what the parent.pcode is?
>>>
>>>Any suggestions would be greatly appreciated.
>>>
>>>Thanks,
>>>-Isaac
Previous
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only