troubleshooting Question

Dynamically Construct and Execute a SQL Server Statements

Avatar of Declan Basile
Declan BasileFlag for United States of America asked on
Microsoft SQL Server
10 Comments1 Solution91 ViewsLast Modified:
There's a table named "IndexFields" with fields "TableId", "DataField", and "PKField"  (see below)

Table "IndexFields"
"TableId"    "DataField"           "PKField"
  1                  PartNumber        PartId
  1                  PartId                   PartId
  2                  SupplierName    SupplierId
etc.

I need code so that an On Update/On Insert trigger on the "Parts" table will essentially lookup all the records in "IndexFields" with TableId "1" and create and run SQL statements for each record found.  (see below)

INSERT INTO IndexTable (TableId, PKId, FieldData) SELECT '1', PartId, PartNumber FROM INSERTED
INSERT INTO IndexTable (TableId, PKId, FieldData) SELECT '1', PartId, PartId FROM INSERTED

How can I do this, or what else can I do to achieve the same result?  (Note: I'll want to add triggers for other tables with their corresponding table id's as well)
ASKER CERTIFIED SOLUTION
HainKurt
Sr. System Analyst

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros