The EXEC using the declared variable @ApptID works.
The EXEC using the SELECT that populates the @ApptID to provide the same value does not.
I get the following error message: Msg 102, Level 15, State 1, Line 19
Incorrect syntax near ','.
What am I missing? My intent is to get multiple records of output using the SELECT.
Also, I have tried to use temp tables to store these multiple rows but with no success.
you cant do that use a select as a parameter to pass to a stored procedure.
but are you trying to see if you can pass more than one row as parameter?
if so you can leverage TYPE
--sample data
drop table Appointments;
create table Appointments(id int identity(1,1), start_date date,
patient_person_id int)
insert into Appointments
select top 100 DATEADD(d, column_id, GETDATE()),column_id
from sys.all_columns
where column_id between 1 and 100
CREATE TYPE [dbo].[AppointmentType] AS TABLE(
ApptID [int] NOT NULL
)
alter proc GetAppointmentEfficiency
@AppointmentType AppointmentType READONLY
as
begin
select *
from appointments a
join @AppointmentType b on a.id = b.ApptID
end
DECLARE @AppointmentType AppointmentType
insert into @AppointmentType
select 1 union
select 99 union
select 33;
exec GetAppointmentEfficiency @AppointmentType
go
alter proc GetAppointmentEfficiency
@AppointmentType nvarchar(max)
as
begin
;with src
as
(
select *
from string_split(@AppointmentType,',')
)
select *
from appointments a
join src b on a.id = b.value
end
go
DECLARE @Appointments nvarchar(max)
select @Appointments = '1,99,33'
exec GetAppointmentEfficiency @Appointments
go
I would recommend not using comma separated list. There are other ways to accomplish this. You can use a user defined table variable or XML as the input parameter
alter proc GetAppointmentEfficiency
@AppointmentType xml
as
begin
;with src
as
(
SELECT AppointmentId = C.value('(.)[1]', 'int')
FROM @AppointmentType.nodes('/Appointments/*') AS T(C)
)
select *
from appointments a
join src b on a.id = b.AppointmentId
end
go
DECLARE @AppointmentType xml
select @AppointmentType = '<Appointments>
<AppointmentId>1</AppointmentId>
<AppointmentId>33</AppointmentId>
<AppointmentId>99</AppointmentId>
</Appointments>'
exec GetAppointmentEfficiency @AppointmentType
go