• Dieselbf2 (1/22/2014)


    Thank you! I think this is what you are asking for.. My Boss wants a Trigger and the reason is maybe to just get me use to writing more logic instead og updat, Delete, and insert along with basic SQL quires as well. Any help would be greatly appreciated

    Thank you

    That is certainly the ddl. Unfortunately that is for only one table. The other challenge is there are lots of errors when I try to load this on my local instance.

    Msg 1767, Level 16, State 0, Line 2

    Foreign key 'FK_ProposalProfitSplitWorksheet_db_branches' references invalid table 'dbo.db_branches'.

    Msg 1750, Level 16, State 0, Line 2

    Could not create constraint. See previous errors.

    Msg 4917, Level 16, State 0, Line 2

    Constraint 'FK_ProposalProfitSplitWorksheet_db_branches' does not exist.

    Msg 4916, Level 16, State 0, Line 2

    Could not enable or disable the constraint. See previous errors.

    Msg 1767, Level 16, State 0, Line 2

    Foreign key 'FK_ProposalProfitSplitWorksheet_logins_AcceptedByInstallingBranchImpersonatingUserID' references invalid table 'dbo.logins'.

    Msg 1750, Level 16, State 0, Line 2

    Could not create constraint. See previous errors.

    Msg 4917, Level 16, State 0, Line 2

    Constraint 'FK_ProposalProfitSplitWorksheet_logins_AcceptedByInstallingBranchImpersonatingUserID' does not exist.

    Msg 4916, Level 16, State 0, Line 2

    Could not enable or disable the constraint. See previous errors.

    Msg 1767, Level 16, State 0, Line 2

    Foreign key 'FK_ProposalProfitSplitWorksheet_logins_AcceptedByInstallingBranchUserID' references invalid table 'dbo.logins'.

    Msg 1750, Level 16, State 0, Line 2

    Could not create constraint. See previous errors.

    Msg 4917, Level 16, State 0, Line 2

    Constraint 'FK_ProposalProfitSplitWorksheet_logins_AcceptedByInstallingBranchUserID' does not exist.

    Msg 4916, Level 16, State 0, Line 2

    Could not enable or disable the constraint. See previous errors.

    Msg 1767, Level 16, State 0, Line 2

    Foreign key 'FK_ProposalProfitSplitWorksheet_logins_AcceptedBySellingBranchImpersonatingUserID' references invalid table 'dbo.logins'.

    Msg 1750, Level 16, State 0, Line 2

    Could not create constraint. See previous errors.

    Msg 4917, Level 16, State 0, Line 2

    Constraint 'FK_ProposalProfitSplitWorksheet_logins_AcceptedBySellingBranchImpersonatingUserID' does not exist.

    Msg 4916, Level 16, State 0, Line 2

    Could not enable or disable the constraint. See previous errors.

    Msg 1767, Level 16, State 0, Line 2

    Foreign key 'FK_ProposalProfitSplitWorksheet_logins_AcceptedBySellingBranchUserID' references invalid table 'dbo.logins'.

    Msg 1750, Level 16, State 0, Line 2

    Could not create constraint. See previous errors.

    Msg 4917, Level 16, State 0, Line 2

    Constraint 'FK_ProposalProfitSplitWorksheet_logins_AcceptedBySellingBranchUserID' does not exist.

    Msg 4916, Level 16, State 0, Line 2

    Could not enable or disable the constraint. See previous errors.

    Msg 1767, Level 16, State 0, Line 2

    Foreign key 'FK_ProposalProfitSplitWorksheet_logins_CreatedByImpersonatingUserID' references invalid table 'dbo.logins'.

    Msg 1750, Level 16, State 0, Line 2

    Could not create constraint. See previous errors.

    Msg 4917, Level 16, State 0, Line 2

    Constraint 'FK_ProposalProfitSplitWorksheet_logins_CreatedByImpersonatingUserID' does not exist.

    Msg 4916, Level 16, State 0, Line 2

    Could not enable or disable the constraint. See previous errors.

    Msg 1767, Level 16, State 0, Line 2

    Foreign key 'FK_ProposalProfitSplitWorksheet_logins_CreatedByUserID' references invalid table 'dbo.logins'.

    Msg 1750, Level 16, State 0, Line 2

    Could not create constraint. See previous errors.

    Msg 4917, Level 16, State 0, Line 2

    Constraint 'FK_ProposalProfitSplitWorksheet_logins_CreatedByUserID' does not exist.

    Msg 4916, Level 16, State 0, Line 2

    Could not enable or disable the constraint. See previous errors.

    Msg 1767, Level 16, State 0, Line 2

    Foreign key 'FK_ProposalProfitSplitWorksheet_proposals' references invalid table 'dbo.proposals'.

    Msg 1750, Level 16, State 0, Line 2

    Could not create constraint. See previous errors.

    Msg 4917, Level 16, State 0, Line 2

    Constraint 'FK_ProposalProfitSplitWorksheet_proposals' does not exist.

    Msg 4916, Level 16, State 0, Line 2

    Could not enable or disable the constraint. See previous errors.

    Obviously you have a little work on the ddl before we can get much going here. Why does your boss ask for this to be done in a trigger? What they are asking for is referential integrity and triggers are not a good choice for that. One issue is you will likely have to write at least 2 triggers for this. An update and an insert. The reason triggers are not good at enforcing RI is because they can be disabled. Let's say I want to force a row in that does not meet the criteria. I simply disable the trigger, insert my data and then enable the trigger.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/