当用户在Excel表搜索栏中输入值时遇到参数时,我试图以动态格式将存储过程的结果显示为枢轴形式。我的代码末尾出现错误,其中我调用了@cols参数。请帮忙。
USE SDCManilaLDTracker
GO
ALTER PROC GW1 (
@trainingName NVARCHAR(MAX),
--@gateway NVARCHAR(max),
@gatewayService NVARCHAR(max),
@userStatus NVARCHAR(max),
@userID INT,
@cluster NVARCHAR(max)
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @lTrainingName NVARCHAR(max),
@lgatewayService NVARCHAR(max),
@luserStatus NVARCHAR(max),
@luserID INT,
@lcluster NVARCHAR(max),
@dateHireRestriction NVARCHAR(max),
@clusterRestriction INT,
@processorRestriction INT,
@cols NVARCHAR(max)
SET @lTrainingName = LTRIM(RTRIM(@trainingName))
SET @lgatewayService = LTRIM(rtrim(@gatewayService))
SET @luserStatus = LTRIM(RTRIM(@userStatus))
SET @luserID = LTRIM(RTRIM(@userID))
SET @lcluster = LTRIM(RTRIM(@cluster))
SET @dateHireRestriction = NULL
SET @clusterRestriction = 0
SET @processorRestriction = 0
SET @cols = STUFF((
SELECT DISTINCT ',' + QUOTENAME(c.training_name)
FROM dbo.cornerstone_trainings c
FOR XML PATH(''),
TYPE
).value('.', 'VARCHAR(MAX)'), 1, 1, '')
SELECT *
FROM (
SELECT cast(ec.user_id AS INT) [User ID],
ec.last_name [Last Name],
ec.first_name [First Name],
ei.user_status [User Status],
ei.ROLE [Role],
convert([datetime], ei.date_of_joining, 101) [Date of Joining],
ei.cluster [Cluster],
ec.training_name [Training Name],
--ec.training_type,
cr.gateway_service [Gateway Service]
--cr.gateway,
--cr.date_hired_restriction,
--cr.processor_restriction,
--cr.cluster_restriction_status
FROM dbo.employee_cornerstone ec
INNER JOIN dbo.employee_information ei WITH (NOLOCK) ON ei.employee_id = ec.user_id
INNER JOIN dbo.cornerstone_trainings cr WITH (NOLOCK) ON cr.training_name = ec.training_name
--LEFT JOIN dbo.cluster_restriction_list c on c.training_name = ec.training_name
WHERE (
@lTrainingName = '' -- parameter lgateway is null
OR (
cr.training_name LIKE '%' + @lTrainingName + '%'
AND cr.date_hired_restriction IS NULL
AND cr.cluster_restriction_status = @clusterRestriction
AND cr.processor_restriction = @processorRestriction
) -- no date hired, cluster and processor restriction
OR (
cr.training_name LIKE '%' + @lTrainingName + '%'
AND cr.date_hired_restriction IS NOT NULL
AND ei.date_of_joining IS NOT NULL
AND ei.date_of_joining > cr.date_hired_restriction
AND cr.cluster_restriction_status = @clusterRestriction
AND cr.processor_restriction = @processorRestriction
) -- has date hired restriction
OR (
ec.training_name LIKE '%' + @lTrainingName + '%'
AND cr.date_hired_restriction IS NULL
AND cr.cluster_restriction_status <> @clusterRestriction
--AND ei.cluster NOT LIKE '%' + cr.cluster_restriction_list + '%'
AND ei.cluster NOT IN (
SELECT cluster_name
FROM dbo.[cluster_restriction_list] c
WHERE c.training_name = ec.training_name
)
AND ec.training_name = @lTrainingName
AND cr.processor_restriction = @processorRestriction
) -- has cluster restriction
OR (
cr.training_name LIKE '%' + @lTrainingName + '%'
AND cr.date_hired_restriction IS NOT NULL
AND cr.cluster_restriction_status <> @clusterRestriction
AND ei.date_of_joining IS NOT NULL
AND ei.date_of_joining > cr.date_hired_restriction
AND ei.cluster NOT IN (
SELECT cluster_name
FROM dbo.[cluster_restriction_list] c
WHERE c.training_name = ec.training_name
)
AND cr.processor_restriction = @processorRestriction
) -- has date hired restriction and cluster restriction
OR (
cr.training_name LIKE '%' + @lTrainingName + '%'
AND cr.date_hired_restriction IS NULL
AND cr.cluster_restriction_status = @clusterRestriction
AND cr.processor_restriction <> @processorRestriction
AND (
ei.ROLE NOT LIKE 'processor%'
OR ei.ROLE IS NULL
)
) -- processor_restriction = 1
)
AND (
@lgatewayService = '' -- parameter lgatewayService is null
OR (
cr.gateway_service LIKE '%' + @lgatewayService + '%'
AND cr.date_hired_restriction IS NULL
AND cr.cluster_restriction_status = @clusterRestriction
AND cr.processor_restriction = @processorRestriction
) -- no date hired, cluster and processor restriction
OR (
cr.gateway_service LIKE '%' + @lgatewayService + '%'
AND cr.date_hired_restriction IS NOT NULL
AND ei.date_of_joining IS NOT NULL
AND ei.date_of_joining > cr.date_hired_restriction
AND cr.cluster_restriction_status = @clusterRestriction
AND cr.processor_restriction = @processorRestriction
) -- has date hired restriction
OR (
cr.gateway_service LIKE '%' + @lgatewayService + '%'
AND cr.date_hired_restriction IS NULL
AND cr.cluster_restriction_status <> @clusterRestriction
AND ei.cluster NOT LIKE '%' + cr.cluster_restriction_list + '%'
AND cr.processor_restriction = @processorRestriction
) -- has cluster restriction
OR (
cr.gateway_service LIKE '%' + @lgatewayService + '%'
AND cr.date_hired_restriction IS NOT NULL
AND cr.cluster_restriction_status <> @clusterRestriction
AND ei.cluster NOT LIKE '%' + cr.cluster_restriction_list + '%'
AND cr.processor_restriction = @processorRestriction
) -- has date hired restriction and cluster restriction
OR (
cr.gateway_service LIKE '%' + @lgatewayService + '%'
AND cr.date_hired_restriction IS NULL
AND cr.cluster_restriction_status = @clusterRestriction
AND cr.processor_restriction <> @processorRestriction
AND (
ei.ROLE NOT LIKE 'processor%'
OR ei.ROLE IS NULL
)
) -- processor_restriction = 1
)
AND (
@luserStatus = '' -- parameter lgateway is null
OR (ei.user_status LIKE '%' + @luserStatus + '%')
)
AND (
@luserID = ''
OR (cast(ei.employee_id AS VARCHAR(max)) LIKE '%' + cast(@lUserID AS [varchar](max)) + '%')
)
AND (
@lcluster = ''
OR (ei.cluster LIKE '%' + @lcluster + '%')
)
GROUP BY cast(ec.user_id AS INT),
ec.first_name,
ec.last_name,
ei.user_status,
ec.training_name,
cr.gateway_service,
ei.ROLE,
ei.cluster,
ei.date_of_joining
) AS s
pivot(max([Gateway Service]) FOR [Training Name] IN (' + @cols + ')) AS pvt
END
GO