Posts

Showing posts with the label Loops In Sql

How to Create trigger to Update the NoofOrgs Count under each category in Category Table based on Temp Table in Sql Server

--creating a trigger CREATE TRIGGER [dbo].UPDATEORGSCOUNTINCATEGORY_AFTERINSERT_FROMORGANISATIONS]   ON --here we mention the table name   [dbo].[Organisations] --here we can have insert, update, delete as trigger point --I want to execute my trigger when a record is inserted in organizations table (step-1) --then get the count of orgs in each category from organisationcategories table and save it in temp table(step-2) --then update the category table with orgs count in each category(step-3)   AFTER INSERT   AS   BEGIN   --we are updating the count bcz a listing or nonprofi can be approved or rejected then the count changes. --creating a temp table with two columns of int type DECLARE @temp TABLE ( CategoryId INT, NoofOrgs INT ) --inserting values into temp table INSERT INTO @temp SELECT * from (         --here we are selecting these two columns select CategoryId,ISNULL(NoofOrgs , 0) AS 'NoofOrgs' from (   ...