--迭代方法
DECLARE @ManagerStringCode VARCHAR(100)
DECLARE @TeamStringCode VARCHAR(100)
declare @cSalesUserID NVARCHAR(12)
SET @ManagerStringCode = ''
SET @TeamStringCode = ''
--SET @cSalesUserID = 'AndyLau'
SET @cSalesUserID = 'LAWRENCEKOK'
SELECT @ManagerStringCode = @ManagerStringCode +
(CASE WHEN CHARINDEX(SalesManger.dmb_cMemberUserID, @ManagerStringCode) >0 THEN '' ELSE (SalesManger.dmb_cMemberUserID + ';') end)
, @TeamStringCode = @TeamStringCode + RTRIM(SalesManger.dmb_cTeamCode) + ';'
FROM DeptTeamMember_dmb Sales WITH(NOLOCK)
LEFT JOIN DeptTeamMember_dmb SalesManger on SalesManger.dmb_cDeptCode = 'SAL'
WHERE Sales.dmb_cMemberUserID =@cSalesUserID
AND Sales.dmb_cTeamCode = SalesManger.dmb_cTeamCode and SalesManger.dmb_cMemberType = 'L'
AND Sales.dmb_cDeptCode = 'SAL'
IF LEN(@ManagerStringCode)>0
SET @ManagerStringCode = LEFT(@ManagerStringCode,LEN(@ManagerStringCode)-1)
SELECT @ManagerStringCode, @TeamStringCode
----迭代方法
--Cursor方法
Declare @SalesManger Nvarchar(12)
Declare @ManagerStringCode VARCHAR(100)
Declare @Separator varchar(2)
SET @Separator = ';'
declare @cSalesUserID NVARCHAR(12)
SET @cSalesUserID = 'LAWRENCEKOK'
Declare SalesMangerCursor Cursor For
Select SalesManger.dmb_cMemberUserID
From DeptTeamMember_dmb Sales
left join DeptTeamMember_dmb SalesManger on SalesManger.dmb_cDeptCode = 'SAL'
Where Sales.dmb_cMemberUserID = @cSalesUserID
and Sales.dmb_cTeamCode = SalesManger.dmb_cTeamCode and SalesManger.dmb_cMemberType = 'L'
AND Sales.dmb_cDeptCode = 'SAL'
Open SalesMangerCursor
FETCH NEXT FROM SalesMangerCursor INTO @SalesManger
WHILE (@@FETCH_STATUS = 0)
Begin
Select @ManagerStringCode = dbo.ufn_SerialString(@ManagerStringCode,@SalesManger,@Separator)
FETCH NEXT FROM SalesMangerCursor INTO @SalesManger
END
CLOSE SalesMangerCursor
DEALLOCATE SalesMangerCursor
SELECT @ManagerStringCode
--Cursor方法