Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

looking for select statement dynamic way?

$
0
0

hello experts,

i have below primary table,

CREATE TABLE A
(
A_Id int NOT NULL PRIMARY KEY,
A_NAME VARCHAR(10) NOT NULL
)

Now, this table has refernce either with one or more table, let say with 2 tables like below,

CREATE TABLE B
(
B_Id int NOT NULL PRIMARY KEY,
B_NAME VARCHAR(10) NOT NULL,
A_Id int FOREIGN KEY REFERENCES A(A_Id)
)
CREATE TABLE C
(
C_Id int NOT NULL PRIMARY KEY,
C_NAME VARCHAR(10) NOT NULL,
A_Id int FOREIGN KEY REFERENCES A(A_Id)
)

Data for all above 3 tables like below,

INSERT INTO A VALUES (1, 'A1'),(2, 'A2'),(3, 'A3')
INSERT INTO B VALUES (10, 'B1', 1)
INSERT INTO C VALUES (100, 'C1', 3)

Now, as we all know we can't delete Table A's 2 rows with A_Id 1 & 3 as they reference with other's 2 tables.

Question : I want to write a select statement for Table A with below output,

concern

1. i don't know my primary key table reference with one table or more than one table.

May be in future a new table introduce and reference with my primary table.

2. is there any dynamic way to find out all the reference and check?

A_Id        A_Name      CanDelete

 1              A1                No

 2              A2                Yes

 3              A3                No

Please suggest, if possible? Thanks!



Viewing all articles
Browse latest Browse all 23857

Trending Articles