Pasar múltiples parámetros a una función con valores de tabla
Frecuentes
Visto 6,032 veces
1
I have a table valued function as below. When I am trying to pass more than one parameter at the same time I am getting a error like "Function has too many arguments specified" .
CREATE FUNCTION [dbo].[GetCompanyUsers](@CompanyId BIGINT)
RETURNS @Users TABLE (Id BIGINT,Contact NVarchar(4000))
AS
BEGIN
INSERT INTO @Users(Id,Contact)
SELECT [Id]
,ISNULL([FirstName],'')+' ' +ISNULL([LastName],'') AS [Contact]
FROM [dbo].[CompanyAddressesContacts]
WHERE [CompanyId]=@CompanyId
ORDER BY ISNULL([FirstName],'')+' ' +ISNULL([LastName],'')
RETURN
END
What modifications I require in the above code so that it allows multiple values and I need to use the function in a "WHERE" condition in my dataset.
WHERE(Document_RFIs.CreatedBy IN
(SELECT Id FROM dbo.GetCompanyUsers(@CompanyId)))
2 Respuestas
0
This may help (but the fundamental problem is - passing a comma delimited string is something to be avoided unless absolutely necessary - which explains why you have received so few answers):-
--set nocount on
--create table #Document_RFIs (
-- CreatedBy varchar(50),
-- columna varchar(50),
-- columnb varchar(50),
-- columnc varchar(50)
--)
--insert into #Document_RFIs values
-- ('albert einstein','another','value',null),
-- ('marie curie','some',null,'tuna'),
-- ('isaac newton','why','not','provide'),
-- ('kepler','some','test','data'),
-- ('robert boyle','with','your','question'),
-- ('john dalton','it',null,'would'),
-- ('enrico fermi','make','helping','you'),
-- ('peter higgs','so','much','easier')
--create table #CompanyAddressesContacts (
-- companyid int,
-- firstname varchar(50),
-- lastname varchar(50)
--)
--insert into #CompanyAddressesContacts values (22,'albert','einstein')
--insert into #CompanyAddressesContacts values (23,'marie','curie')
--insert into #CompanyAddressesContacts values (23,'isaac','newton')
--insert into #CompanyAddressesContacts values (24,null,'kepler')
--insert into #CompanyAddressesContacts values (25,'robert','boyle')
--insert into #CompanyAddressesContacts values (25,'enrico','fermi')
--insert into #CompanyAddressesContacts values (26,'peter','higgs')
declare @ids varchar(1024)
set @ids='23,24,25'
create table #id (
companyid int
)
declare @pos int
while DATALENGTH(@ids)>0 begin
set @pos=charindex(',',@ids)
if @pos>0 begin
insert into #id values (left(@ids,@pos-1))
set @ids=SUBSTRING(@ids,@pos+1,DATALENGTH(@ids))
end else begin
insert into #id values (@ids)
set @ids=''
end
end
select d.*
from #Document_RFIs d
where exists(
select cac.*
from #CompanyAddressesContacts cac
join #id i on i.companyid=cac.companyid
where isnull(cac.firstname+' ','')+isnull(cac.lastname,'')=d.CreatedBy
)
--drop table #id
--drop table #Document_RFIs
--drop table #CompanyAddressesContacts
respondido 27 nov., 13:09
0
Haría algo como esto:
First convert your @CompanyId to rows
WITH CompanyIds AS (
SELECT Id
FROM CompanyTable -- Same as the source of the @CompanyId
WHERE Id IN (@CompanyId)
)
Then extract all users
,Users AS (
SELECT UserId
FROM CompanyIds
CROSS APPLY (
SELECT Id AS UserId
FROM dbo.GetCompanyUsers(CompanyIds.Id)
) AS CA1
)
And then use it in the where statement
WHERE Document_RFIs.CreatedBy IN (SELECT UserId
FROM Users)
respondido 28 nov., 13:07
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas sql sql-server-2008 tsql reporting-services ssrs-2008 or haz tu propia pregunta.
Have you tried to use a join like this? INNER JOIN dbo.GetCompanyUsers(@CompanyId) GCU ON GCU.Id = Document_RFIs.CreatedBy - Raphael
how many parameters are you passing? - Albert Laure
@Raphael Yes I tried . But I am getting the same error "Procedure or Function has too many arguments specified" - user1699025
Number of parameters can vary depending on the no of companies selected . It can vary between 1 to many e.g. dbo.GetCompanyUsers(23,24,25) - user1699025
How are the companyid values being passed into sql? Number of parameters cannot vary in SQL. Where are they being
selected
? - dav1dsm1th