我有AA過程,從我的數據庫中刪除用戶:調用列中每個值的過程?
CREATE PROCEDURE [dbo].[sp_removeUser]
@UserId uniqueidentifier
,@result int OUT
AS
BEGIN
SET NOCOUNT ON
SET @Result=0
-- whether User exists
IF NOT EXISTS(SELECT 1 FROM [dbo].[Users] (nolock) WHERE UserId = @UserId)
RETURN
-- Prevent to remove admin
IF EXISTS(SELECT 1 FROM [dbo].[aspnet_Users] (nolock) WHERE UserId= @UserId AND UserName = 'admin')
RETURN
BEGIN TRAN
-- Remove User Addresses
DELETE FROM [dbo].[AddressesUserAccess] WHERE AddressId IN (SELECT AddressId FROM Addresses WHERE UserId = @UserId)
DELETE FROM [dbo].[AddressesInGroups] WHERE AddressId IN (SELECT AddressId FROM Addresses WHERE UserId = @UserId)
DELETE FROM [dbo].[Addresses] WHERE UserId = @UserId
-- Remove User Numbers
DELETE FROM [dbo].[NumbersUserAccess] WHERE NumberId IN (SELECT NumberId FROM Numbers WHERE UserId = @UserId)
DELETE FROM [dbo].[NumbersInGroups] WHERE NumberId IN (SELECT NumberId FROM Numbers WHERE UserId = @UserId)
DELETE FROM [dbo].[Numbers] WHERE UserId = @UserId
...
... and lots more below...
...
我要刪除一大堆的用戶,所以我首先想到的是讓所有我想刪除的用戶ID的一列,然後迭代,依次刪除每個用戶。
然而,在閱讀了一些關於SO的其他問題後,我知道這不是最好的方式,而應該尋找更多基於集合的解決方案。
那麼什麼是更好的方式來實現這個過程以更基於集合的方式工作?
什麼版本的SQL Server?如果2008年你可以在TVP中傳遞一個用戶名單。 –
我的開發機器是2008年,但我們的現場服務器是2005年,這是一種惱人的。 – Oliver
[2005年在這裏還有其他一些傳遞值的方法](http://www.sommarskog.se/arrays-in-sql-2005.html) –