我需要SQL中的变量来获取Select语句的值

时间:2015-09-23 13:25:19

标签: sql sql-server database variables sql-delete

我试图让这个与@FileIntID一起使用,从表中提取多个值。我尝试了很多不同的方法。我唯一要做的就是计数循环并选择一个不同的行直到null,但必须有一个更简单的方法。任何帮助,将不胜感激。如果我设置@FileIntID并删除临时删除表,代码工作得很好,但我不想为超过3000个条目执行此操作。我也不想将值输出到excel并为每个场景添加逗号。

DECLARE @AccountID as Int, @FileIntID as BigInt

Set @AccountID = 758231

Declare @DeletionTable Table (FileIntId BigInt);
Insert Into @DeletionTable
(
    FileIntID 
)
SELECT FileIntID
FROM [MDSCommonDB1].[Document].[Metadata] m
INNER JOIN Document.entities e on e.EntityIntID = m.EntityIntID and  e.EntityType  = m.EntityType
inner join document.EntityStorageArea s on s.EntityIntID = e.EntityIntID and s.EntityType  = m.EntityType and s.StorageType = 'E'
where StrgCatagory = 'E' and m.FilePath NOT LIKE '%'+ e.EntityName+'%'

Select @FileIntID = (Select FileIntID from @DeletionTable)

DECLARE @FirmIntID as BigInt

Select @firmIntID = FirmIntID from [Common].[FirmAccount] with (nolock)
where AccountID = @AccountID

Select @FileIntID = m.FileIntId from [Document].[Metadata] m with (nolock)
where m.[FirmIntID]= @firmIntID and m.FileStatus = 'T' and m.FileIntId = @FileIntID

Delete FROM [Document].FileAudit
Where  FileIntID = @FileIntID and FirmIntID =@firmIntID 

Delete [Document].FileAuditLatest
Where FileIntID = @FileIntID                                      

Delete FROM [Document].FileVersion
Where  FileIntID = @FileIntID

Delete FROM [Document].[PublishDetail] 
Where  FileIntID = @FileIntID and FirmIntID = @firmIntID

Declare @fileDiscussion TABLE (FileDiscussionIntID bigint)
Declare @userGroupFilePermission TABLE (UserGroupFilePermissionIntID bigint)

INSERT INTO @fileDiscussion
(
     FileDiscussionIntID
)
SELECT [FileDiscussion].[FileDiscussionIntID] 
FROM [Document].[FileDiscussion] WITH (NOLOCK)
WHERE [FileDiscussion]. FileIntID = @FileIntID and [FileDiscussion].FirmIntID = @firmIntID

DELETE FROM [Document].[FileDiscussionNotification] 
WHERE [FileDiscussionNotification].[FileDiscussionIntID] in (SELECT     FileDiscussionIntID FROM @fileDiscussion)

DELETE FROM [Document].[FileDiscussion] 
WHERE [FileDiscussion].[FileDiscussionIntID] in (SELECT FileDiscussionIntID FROM @fileDiscussion)

INSERT INTO @userGroupFilePermission
(
    UserGroupFilePermissionIntID
)
SELECT [UserGroupFilePermission].[UserGroupFilePermissionIntID] 
FROM [Document].[UserGroupFilePermission] WITH (NOLOCK)
WHERE [UserGroupFilePermission].FileIntID = @FileIntID and [UserGroupFilePermission].FirmIntID = @firmIntID

DELETE FROM [Document].[UserGroupFilePermissionDetail] 
WHERE [UserGroupFilePermissionDetail].[UserGroupFilePermissionIntID] in (SELECT UserGroupFilePermissionIntID FROM @userGroupFilePermission)

DELETE FROM [Document].[UserGroupFilePermission] WHERE
[UserGroupFilePermission].[UserGroupFilePermissionIntID] in (SELECT  UserGroupFilePermissionIntID FROM @userGroupFilePermission)

DELETE FROM [Document].[Filegroupdetail] 
WHERE [FileIntID] = @FileIntID


DELETE FROM [Document].TaskAssignment
Where  FileIntID = @FileIntID

DELETE  FROM [Document].[PortalFileAudit]
WHERE [FileIntID] = @FileIntID and FirmIntID = @firmIntID

/* Start FK Reference Delete */

Delete [Document].[MultiuserDirectEdit]
Where FileIntID = @FileIntID  AND FirmIntID = @firmIntId

Delete [Document].[NamingChangeFiles]
Where FileIntID = @FileIntID  AND FirmIntID = @firmIntId

Delete [Document].[OfflineSyncFiles]
Where FileIntID = @FileIntID

Delete [Document].[RecycleBin]
Where FileIntID = @FileIntID  AND FirmIntID = @firmIntId

Delete [Document].[RoutingStatus]
Where FileIntID = @FileIntID  AND FirmIntID = @firmIntId

/* End FK Reference Delete */  

Delete FROM [Document].SearchMetadata
Where  FileIntID = @FileIntID and FirmIntID = @firmIntID

Delete FROM [Document].Metadata
Where FileIntID = @FileIntID and FirmIntID = @firmIntID

1 个答案:

答案 0 :(得分:0)

你甚至不需要变量。您已在@DeletionTable中拥有ID。只需加入Deletion表而不是使用变量。

例如,在第一种使用变量的情况下:

Delete FROM [Document].FileAudit
Where  FileIntID = @FileIntID and FirmIntID =@firmIntID 

请改为:

Delete FROM fa
FROM [Document].FileAudit fa
INNER JOIN @DeletionTable dt
  ON fa.FileIntID = dt.FileIntID 
Where   FirmIntID =@firmIntID 

在所有其他案件中等等。

我甚至不确定你为什么要这样做:

Select @FileIntID = m.FileIntId from [Document].[Metadata] m with (nolock)
where m.[FirmIntID]= @firmIntID and m.FileStatus = 'T' and m.FileIntId = @FileIntID

如果要限制变量中的文件ID,那么只需将其作为填充@DeletionTable的第一个查询的一部分