• The following should work.

    CREATE PROCEDURE [dbo].[Get_Ques_id_for_can](

    @candidate_id varchar(max),

    @Exam_id varchar(max),

    @sec_id bigint,

    @Q_id varchar(max) OUTPUT

    )

    AS

    declare @table_query nvarchar(max);

    declare @table nvarchar(max);

    declare @sparam nvarchar(max);

    set @sparam = N'@SecID bigint, @QID varchar(max) OUTPUT';

    set @table = @candidate_ID + @Exam_id;

    declare @id varchar(50);

    declare @ques_id varchar(50);

    set @table_query = --N'SELECT @ID = [ID], @QID = [Question_ID] FROM [' + @table + N'] WHERE [Section_ID] = ' + CONVERT(varchar(50),@sec_id)

    N'select

    @QID = stuff((select

    ''|'' + cast(ID as varchar) + '';'' + cast(Question_ID as varchar)

    from

    [' + @table + N']

    where

    Section_id = @SecID

    order by

    ID

    for xml path(''''),TYPE).value(''.'',''varchar(max)''),1,1,'''')'

    exec sp_executesql @table_query, @sparam, @SecID = @sec_id, @QID = @Q_id OUTPUT;

    GO

    /* how this procedure should be invoked. The ?? represent values passed into the procedure */

    declare @QuesID varchar(max);

    exec dbo.Get_Ques_id_for_can @candidate_id = ??, @Exam_id = ??, @sec_id = ??, @Q_id = @QuesID OUTPUT;

    go