我在以下查询中找不到语法错误:
USE [Contact Manager]
GO
-- Define the Procedure
ALTER PROCEDURE [dbo].[sp_delete_Contact]
@contactID INT
AS
BEGIN
-- SET NOCOUNT OFF added enable Counting
SET NOCOUNT OFF;
-- Determine all Phone Numbers with a Single Link to the specified Contact
CREATE TABLE #PhoneNumbers (phone_number INT NOT NULL PRIMARY KEY)
INSERT #PhoneNumbers (phone_number)
SELECT phone_number
FROM Contact_PhoneNumber
WHERE phone_number IN
(
SELECT phone_number
FROM Contact_PhoneNumber
GROUP BY phone_number
HAVING COUNT(*) = 1
)
AND contact = @contactID
-- Declare Loop Variables
DECLARE @i int = 0
DECLARE @count int = (SELECT COUNT(*) FROM #PhoneNumbers)
-- Delete all Phone Numbers with a Single Link to the specified Contact
WHILE @i < @count
EXEC sp_delete_PhoneNumber
(
SELECT phone_number, ROW_NUMBER()
OVER (ORDER BY phone_number) AS row_num
FROM #PhoneNumbers
WHERE row_num = @i
)
SET @i = @i + 1
END
-- Determine all Addresses with a Single Link to the specified Contact
CREATE TABLE #Addresses ([address] INT NOT NULL PRIMARY KEY)
INSERT #Addresses ([address])
SELECT [address]
FROM Contact_Address
WHERE [address] IN
(
SELECT [address]
FROM Contact_Address
GROUP BY [address]
HAVING COUNT(*) = 1
)
AND contact = @contactID
-- Reset Loop Variables
SET @i = 0
SET @count = (SELECT COUNT(*) FROM #Addresses)
-- Delete all Addresses with a Single Link to the specified Contact
WHILE @i < @count
EXEC sp_delete_Address
(
SELECT [address], ROW_NUMBER()
OVER (ORDER BY [address]) AS row_num
FROM #Addresses
WHERE row_num = @i
)
SET @i = @i + 1
END
-- Determine all Emails with a Single Link to the specified Contact
CREATE TABLE #Emails (email INT NOT NULL PRIMARY KEY)
INSERT #Emails (email)
SELECT email
FROM Contact_Email
WHERE email IN
(
SELECT email
FROM Contact_Email
GROUP BY email
HAVING COUNT(*) = 1
)
AND contact = @contactID
-- Reset Loop Variables
SET @i = 0
SET @count = (SELECT COUNT(*) FROM #Emails)
-- Delete all Emails with a Single Link to the specified Contact
WHILE @i < @count
EXEC sp_delete_Email
(
SELECT email, ROW_NUMBER()
OVER (ORDER BY email) AS row_num
FROM #Emails
WHERE row_num = @i
)
SET @i = @i + 1
END
-- Determine all Groups with a Single Link to the specified Contact
CREATE TABLE #Groups ([group] INT NOT NULL PRIMARY KEY)
INSERT #Groups ([group])
SELECT [group]
FROM Member
WHERE [group] IN
(
SELECT [group]
FROM Member
GROUP BY [group]
HAVING COUNT(*) = 1
)
AND contact = @contactID
-- Reset Loop Variables
SET @i = 0
SET @count = (SELECT COUNT(*) FROM #Groups)
-- Delete all Groups with a Single Link to the specified Contact
WHILE @i < @count
EXEC sp_delete_Group
(
SELECT [group], ROW_NUMBER()
OVER (ORDER BY [group]) AS row_num
FROM #Emails
WHERE row_num = @i
)
SET @i = @i + 1
END
END
对此的任何帮助将不胜感激。
评论应该让你知道我想要做什么,但我很确定代码背后的逻辑是可靠的。我只是不知道语法错误在哪里。
当我尝试运行查询以更改为存储过程时,出现以下错误:
Msg 156, Level 15, State 1, Procedure sp_delete_Contact, Line 81
Incorrect syntax near the keyword 'CREATE'.
Msg 156, Level 15, State 1, Procedure sp_delete_Contact, Line 113
Incorrect syntax near the keyword 'CREATE'.
Msg 156, Level 15, State 1, Procedure sp_delete_Contact, Line 143
Incorrect syntax near the keyword 'END'.