Puoi creare una funzione definita dall'utente per questo:
CREATE FUNCTION dbo.CountUses(@pictureId INT)
RETURNS INT
AS
BEGIN
RETURN
(SELECT Count(id)
FROM PictureUse
WHERE PictureId = @PictureId)
END
La colonna calcolata può quindi essere aggiunta in questo modo:
ALTER TABLE dbo.Picture
ADD NofUses AS dbo.CountUses(Id)
Tuttavia, preferirei fare una considerazione per questo:
CREATE VIEW PictureView
AS
SELECT Picture.Id,
PictureName,
Picture.CreateDate,
Count(PictureUse.Id) NofUses
FROM Picture
JOIN PictureUse
ON Picture.Id = PictureUse.PictureId
GROUP BY Picture.Id,
PictureName,
Picture.CreateDate