Pasar múltiples parámetros a una función con valores de tabla

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)) 
INSERT INTO @Users(Id,Contact)
  ,ISNULL([FirstName],'')+' ' +ISNULL([LastName],'') AS [Contact]
FROM [dbo].[CompanyAddressesContacts]
WHERE  [CompanyId]=@CompanyId
ORDER BY ISNULL([FirstName],'')+' ' +ISNULL([LastName],'')



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)))

preguntado el 27 de noviembre de 13 a las 06:11

Have you tried to use a join like this? INNER JOIN dbo.GetCompanyUsers(@CompanyId) GCU ON GCU.Id = Document_RFIs.CreatedBy -

how many parameters are you passing? -

@Raphael Yes I tried . But I am getting the same error "Procedure or Function has too many arguments specified" -

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) -

How are the companyid values being passed into sql? Number of parameters cannot vary in SQL. Where are they being selected? -

2 Respuestas

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=''

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

Haría algo como esto:

First convert your @CompanyId to rows

WITH CompanyIds AS (
    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 or haz tu propia pregunta.