我有一个现有的存储过程,它接受两个单值参数。现在我需要更改其中一个参数以接受多个值。我修改了存储过程如下。
CREATE PROCEDURE [dbo].[GetSpecificationsM1]
@EntityType NVARCHAR(100)
,@EntityId BIGINT
AS
DECLARE @EntityTypeId AS BIGINT
SET @EntityTypeId=ISNULL((SELECT ID
FROM [ObjectTypes] WHERE [Type]=@EntityType),0)
SELECT ISNULL([Specifications].[Id],0) AS [SpecificationId]
,ISNULL([Specifications].[Measure],'') AS [Measure]
,ISNULL([Specifications].[Notes],'') AS [Notes]
,ISNULL([UOM].[UOM],'') As UOM
,ISNULL(SpecificationsTemplate.Name,'') As Specification
,ISNULL(SpecificationsTemplate.Id, 0) AS SpecificationTemplateId
,ISNULL(Specifications.EntityId, 0) AS EntityId
,(CASE WHEN ISNULL([SpecificationsTemplate].FieldTypeId,0)=0
THEN 8 ELSE [SpecificationsTemplate].FieldTypeId END) AS [FieldTypeId]
,ISNULL(SpecificationsTemplate.ListId,0) AS ListId
,ISNULL([Specifications].[ListItemId],0) AS [ListItemId]
FROM [SpecificationsTemplate] LEFT OUTER JOIN [Specifications]
ON [SpecificationsTemplate].[Id]=[Specifications].[SpecificationTemplateId]
AND [Specifications].[EntityTypeId]=@EntityTypeId
LEFT OUTER JOIN [UOM] ON [SpecificationsTemplate].[UOMId]=[UOM].[Id]
WHERE [Specifications].[EntityId] IN (@EntityId)
GO
在上面的代码中,我修改了@EntityId以接受代码最后一行中的多个值。但是当我尝试将多个值作为@ EntityId = 9,10,11传递给参数时,我收到错误,因为无法将Varchar转换为BigINt。
请帮助我解决问题。
谢谢!
答案 0 :(得分:1)
在您的存储过程中更改参数,其中的条件如下所示
例如:
WHERE ([CostCentre]) collate database_default IN(SELECT Value FROM dbo.FnSplit(@CostCentre,','))
完整代码:
ALTER PROCEDURE [dbo].[SomeSP]
@CostCentre NVARCHAR(255)
AS
SELECT
[ProjectCode],[ProjectName], [ProjectManager],SUM([Hours]) AS [Hours MTD]
FROM dbo.Rpt_NRMA_CATS NC
INNER JOIN PeriodID P ON NC.PeriodID = P.PeriodID
WHERE
([CostCentre]) collate database_default IN (SELECT Value FROM dbo.FnSplit(@CostCentre, ','))
答案 1 :(得分:0)
只需将你的状况改为
即可WHERE [Specifications].[EntityId] IN (select item from dbo.fnSplit(@EntityId))
fnSplit函数的代码是here