2011-12-19 135 views
1

和Image一樣,我想從配置文件和登錄表中刪除一條記錄或一行。配置文件表與配置文件表中的Login By Id和Login表中的Profileid具有關係。 enter image description here 我使用此查詢:我如何從2表中刪除有關係的記錄?

DELETE  Profile.Id , Profile.Lastname, Profile.Name, Profile.Phone, Profile.Countryid,  Profile.Email, Profile.[Address], Profile.Typeid, Profile.[Status], Profile.Regdate, 
        Login.[Password] Login.Username, Login.Id AS Loginid, Login.Profileid 
FROM   Login INNER JOIN Profile ON Login.Profileid = Profile.Id 

WHERE  Profile.Id = 4 

但是當我運行此查詢我得到這個錯誤的Microsoft SQL Server 2008:

消息102,15級,狀態1,行 語法錯誤靠近','。

是否適當的查詢來執行此操作刪除作業? 如果不是,我該怎麼做?我的意思是如何查詢應該是?

+1

加油的人。至少查看'DELETE'語句的語法。 http://www.w3schools.com/sql/sql_delete.asp – 2011-12-19 06:54:55

回答

1

爲什麼不這樣做,

delete from profile where profile_id = 4 

delete from login where profile_id = 4 

雖然你可能想有層疊設置的選項,爲您的外鍵。這樣,您只需從配置文件表中刪除條目,其他表中的所有行將自動刪除配置文件id = 4。 (或從配置文件表中刪除的任何標識)。

+0

我不能使用級聯,因爲我的主人說,我應該提到,我想在我的程序中使用此查詢使用Asp.net,我如何使用兩個單獨的查詢當我點擊A按鈕? 。 – 2011-12-19 06:58:30

1

您應該查看delete的語法。

您還需要兩個刪除語句。每個桌子一個。

delete from login where profileid = 4 

delete from profile where id = 4 
+0

我想在我的程序中使用此查詢使用Asp.net和我如何使用兩個單獨的查詢,當我點擊一個按鈕? 。 – 2011-12-19 07:04:03

+0

@rezaasgary - 在一個execute語句中添加這兩行應該完全可以。 – 2011-12-19 07:13:54

+0

當我這樣做:http://uploadnow.org/image/334187-Capture.jpeg我得到這個錯誤:http://uploadnow.org/image/334188-Capture2.jpeg這意味着我必須刪除Senduserid從消息是這樣嗎?如果是在另一方面,我必須刪除所有記錄從與配置文件表相關的所有表,右? – 2011-12-19 07:40:55

0

試試這個:

DELETE FROM Login WHERE Profile.Id = 4 --fist execute delete from child with FK 

DELETE FROM Profile WHERE Profile.Id = 4 -- then execute delete from parent PK 

首先從登錄表中刪除記錄包含[FK] Profile.Id = 4

然後如果沒有[FK] Profile.Id = 4存在於登錄表,那麼你現在可以刪除它在個人資料表[PK] Profile.Id = 4

問候

相關問題