我创建了一个存储过程,其中使用了一个表对象并在其中插入了一些列。以下是程序:
CREATE Procedure [dbo].[usp_Security] (@CredentialsList dbo.Type_UserCredentialsList ReadOnly) As
Begin
Declare
@Result Table
(
IdentityColumn Int NOT NULL Identity (1, 1) PRIMARY KEY,
UserCredentials nVarChar(4000),
UserName nVarChar(100),
UserRole nVarChar(200),
RoleID Int,
Supervisor Char(3),
AcctMaintDecn Char(3),
EditPendInfo Char(3),
ReqInstID Char(3)
)
Insert Into @Result
Select Distinct UserCredentials, 'No', D.RoleName, D.RoleID,'No', 'No', 'No' From @CredentialsList A
Join SecurityRepository.dbo.SecurityUsers B On CharIndex(B.DomainAccount, A.UserCredentials) > 0
Join SecurityRepository.dbo.SecurityUserRoles C On C.UserID = B.UserID
Join SecurityRepository.dbo.SecurityRoles D On D.RoleID = C.RoleID
Where D.RoleName Like 'AOT.%' And B.IsActive = 1 And D.IsActive = 1
Update A
Set A.UserName = B.UserName
From @Result A
Join @CredentialsList B On A.UserCredentials = B.UserCredentials
-- "Supervisor" Column
Update A
Set A.Supervisor = 'Yes'
From @Result A
Join SecurityRepository.dbo.SecurityUsers B On CharIndex(B.DomainAccount, A.UserCredentials) > 0
Join SecurityRepository.dbo.SecurityUserRoles C On C.UserID = B.UserID
Join SecurityRepository.dbo.SecurityRoles D On D.RoleID = C.RoleID
Where D.RoleName In ('AOT.Manager', 'AOT.Deps Ops Admin', 'AOT.Fraud Manager', 'AOT.Fulfillment Manager')
And B.IsActive = 1 And D.IsActive = 1
-- Return Result
Select * From @Result Order By UserName, UserRole
End
在上述过程中,我使用了 Table 对象,然后在该表对象上创建了一个聚集索引。
但是,如果我创建一个临时表,然后在 SP 中处理上述信息,是否会比使用表对象而不是临时表更快。我尝试在表对象的列上创建单独的聚集索引,但它不允许我创建它,因为我们无法在表对象上创建索引。
我想在上述存储过程中使用临时表,但与使用表对象相比,它会降低成本。