"The multi-part identifier .... could not be found"

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on an error encountered in Microsoft SQL Server 2014 (T-SQL) related to a trigger designed to delete comment descendants. The error message "The multi-part identifier deleted.descendant could not be bound" arises because the "deleted" virtual table can contain multiple records during a delete operation. The solution provided suggests modifying the delete statement to use an IN clause: "DELETE FROM Comments WHERE id IN (SELECT deleted.descendant FROM deleted);". Additionally, the database setup logic is confirmed to be sound, with no inherent issues identified.

PREREQUISITES
  • Understanding of Microsoft SQL Server 2014 and T-SQL syntax
  • Knowledge of database triggers and their behavior in SQL
  • Familiarity with foreign key constraints and cascading deletes
  • Experience with SQL virtual tables, specifically "deleted" and "inserted"
NEXT STEPS
  • Research the use of SQL Server triggers and their limitations
  • Learn about handling multiple records in SQL Server delete operations
  • Explore alternative methods for cascading deletes without triggers
  • Investigate best practices for designing relational database schemas in SQL Server
USEFUL FOR

Database developers, SQL Server administrators, and anyone involved in managing relational databases who seeks to optimize delete operations and understand trigger functionality in T-SQL.

SlurrerOfSpeech
Messages
141
Reaction score
11
Microsoft SQL Server 2014 (T-SQL)

My codez are

Code:
CREATE DATABASE JsPracticeDb; 
/* Create tables corresponding to the problems, solutions to 
   problems, and ratings of problems or solutions */
CREATE TABLE Problems ( 
    id INT PRIMARY KEY IDENTITY(1,1) NOT NULL, 
    prompt_code VARCHAR(3000) NOT NULL,
    test_func_code VARCHAR(3000) NOT NULL,
    test_input_code VARCHAR(3000) NOT NULL,
    created DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE Solutions (
   id INT PRIMARY KEY IDENTITY(1,1) NOT NULL, 
   problem_id INT NOT NULL,
   solver VARCHAR(50),
   code VARCHAR(3000),
   FOREIGN KEY (problem_id) REFERENCES Problems(id) ON DELETE CASCADE,
   created DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE Ratings (
    id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
    solution_id INT NOT NULL,
    stars TINYINT NOT NULL,
    FOREIGN KEY (solution_id) REFERENCES Solutions(id) ON DELETE CASCADE
);
CREATE TABLE Comments ( 
    id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
    solution_id INT NOT NULL,
    commenter VARCHAR(50),
    comment VARCHAR(2000) NOT NULL,
    created DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (solution_id) REFERENCES Solutions(id) ON DELETE CASCADE
);
/* Closure Table for comment hierarchy */
CREATE TABLE CommentPaths (
    ancestor INT NOT NULL,
    descendant INT NOT NULL,
    PRIMARY KEY (ancestor, descendant),
    FOREIGN KEY (ancestor) REFERENCES Comments(id) ON DELETE CASCADE,
    FOREIGN KEY (descendant) REFERENCES Comments(id) ON DELETE CASCADE
);
/* Create trigger for deleting all comment descendants when 
   the comment is deleted */
GO
CREATE TRIGGER deleteDescendants ON CommentPaths FOR DELETE
AS
DELETE FROM Comments WHERE id=deleted.descendant;

and the very last part deleted.descendant is getting hit with the error

"The multi-part identifier deleted.descendant could not be bound."

Any help?

Also, is there anything wrong with the logic of my database setup, and there is any way of doing what I'm trying to do without using a trigger?
 
Technology news on Phys.org
SlurrerOfSpeech said:
Microsoft SQL Server 2014 (T-SQL)

My codez are

Code:
CREATE DATABASE JsPracticeDb;
/* Create tables corresponding to the problems, solutions to
   problems, and ratings of problems or solutions */
CREATE TABLE Problems (
    id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
    prompt_code VARCHAR(3000) NOT NULL,
    test_func_code VARCHAR(3000) NOT NULL,
    test_input_code VARCHAR(3000) NOT NULL,
    created DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE Solutions (
   id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
   problem_id INT NOT NULL,
   solver VARCHAR(50),
   code VARCHAR(3000),
   FOREIGN KEY (problem_id) REFERENCES Problems(id) ON DELETE CASCADE,
   created DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE Ratings (
    id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
    solution_id INT NOT NULL,
    stars TINYINT NOT NULL,
    FOREIGN KEY (solution_id) REFERENCES Solutions(id) ON DELETE CASCADE
);
CREATE TABLE Comments (
    id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
    solution_id INT NOT NULL,
    commenter VARCHAR(50),
    comment VARCHAR(2000) NOT NULL,
    created DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (solution_id) REFERENCES Solutions(id) ON DELETE CASCADE
);
/* Closure Table for comment hierarchy */
CREATE TABLE CommentPaths (
    ancestor INT NOT NULL,
    descendant INT NOT NULL,
    PRIMARY KEY (ancestor, descendant),
    FOREIGN KEY (ancestor) REFERENCES Comments(id) ON DELETE CASCADE,
    FOREIGN KEY (descendant) REFERENCES Comments(id) ON DELETE CASCADE
);
/* Create trigger for deleting all comment descendants when
   the comment is deleted */
GO
CREATE TRIGGER deleteDescendants ON CommentPaths FOR DELETE
AS
DELETE FROM Comments WHERE id=deleted.descendant;

and the very last part deleted.descendant is getting hit with the error

"The multi-part identifier deleted.descendant could not be bound."

Any help?

Also, is there anything wrong with the logic of my database setup, and there is any way of doing what I'm trying to do without using a trigger?

In a delete trigger, the virtual table "deleted" can contain more than one record, that's why
Code:
DELETE FROM Comments WHERE id=deleted.descendant;
doesn't work.
Try something like
SQL:
DELETE FROM Comments WHERE id in (select deleted.descendant from deleted);
 
  • Like
Likes   Reactions: Silicon Waffle and SlurrerOfSpeech

Similar threads

Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K