Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to solve this puzzle?

Author  Topic 

allan8964
Posting Yak Master

249 Posts

Posted - 2012-03-05 : 15:01:09
Hi there,

Table1 has PK on col CodeID and Table2 has FK on CodeId. Each time a row is added to Table1 then CodeID is added to Table2 with same nunmber. If I try to delete a row in Table1 then I need to delete the row with same CodeId in Table2 first. How can I do that by running scripts (not manually)? Thanks in advance.

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-05 : 15:04:38
Cascade DELETE? A Trigger?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2012-03-05 : 15:55:25
Could you please be more specific? Thanks.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-05 : 16:20:51
Can you post the DDL for your tables?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-03-05 : 16:43:45
quote:
Originally posted by X002548

Cascade DELETE?


You'll get taken outside and shot for suggesting that in my shop.

Write a stored procedure to do it, first removing record(s) from the child table, then the parent.

Cascading deletes and triggers to mimic cascading deletes allow data to ambiguously be deleted. Huge no no. Just begging for trouble.
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2012-03-05 : 19:28:42
Thanks. But my scenario is I need to start from parent table. For this situation:

table1 has cols
table1ID | Code
11 | TTG
22 | TTY

table2 has cols
table2ID | table1ID | ...
111 | 11
113 | 14

Now I can only access table1.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-06 : 09:33:01
quote:
Originally posted by russell

quote:
Originally posted by X002548

Cascade DELETE?


You'll get taken outside and shot for suggesting that in my shop.

Write a stored procedure to do it, first removing record(s) from the child table, then the parent.

Cascading deletes and triggers to mimic cascading deletes allow data to ambiguously be deleted. Huge no no. Just begging for trouble.



You won't find them here either...but some people do, and have no problem doing it...don't ask me why



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -