Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6930835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:29:57+00:00 2026-05-27T11:29:57+00:00

Suppose I want to get student summaries from a two tables: student, grade: CREATE

  • 0

Suppose I want to get student summaries from a two tables: student, grade:

CREATE PROCEDURE prc_get_student_grade_summaries
    @studentIds [Integer_udtt] READONLY
AS
BEGIN
SELECT Name,
    func_GetGradeAForStudent()
FROM tbl_student AS tS
    INNER JOIN
    @studentIds AS tSI
    ON tS.Id = tSI.studentId   

Integer udtt is defined as this:

CREATE TYPE [Integer_udtt] AS  TABLE (
    [Id] INT NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC));

func_GetGradeAForStudent is something like select COUNT(*) from student where studentId = id AND grade = 1 — 1: A

The result I want is list of summary:

StudentId  Number of A
101   5
102   4
103   2

What is the correct way to pass in the studentId from @studentIds to the func_GetGradeAForStudent?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:29:58+00:00Added an answer on May 27, 2026 at 11:29 am

    You can’t pass table-value parameters to UDF’s:

    MSDN:

    Limitations of Table-Valued Parameters

    There are several limitations to table-valued parameters:

    You cannot pass table-valued parameters to user-defined functions.

    Table-valued parameters can only be indexed to support UNIQUE or PRIMARY KEY constraints. SQL Server does not maintain statistics on
    table-valued parameters.

    Table-valued parameters are read-only in Transact-SQL code. You cannot update the column values in the rows of a table-valued
    parameter and you cannot insert or delete rows. To modify the data
    that is passed to a stored procedure or parameterized statement in
    table-valued parameter, you must insert the data into a temporary
    table or into a table variable.

    You cannot use ALTER TABLE statements to modify the design of table-valued parameters.

    Since these are just a list of student Ids, one possibility would be to pass those Ids as a comma-separeted list of ids and use a split function to recreate a table from the comma-separeted list. There’s a ton of examples here on SO and elsewhere where you can find sample implementation of a split function.

    Or even better, do everything inside your proc. I don’t really see the need for that function if all is doing is a select count(*)... You should be able to do everything inline, perhaps using a subselect as so:

    CREATE PROCEDURE prc_get_student_grade_summaries
        @studentIds [Integer_udtt] READONLY
    AS
    BEGIN
    SELECT Name,
        (select COUNT(*) from student where studentId = tSI.studentId AND grade = 1 ) as NumberOfAs
    FROM tbl_student AS tS
        INNER JOIN
        @studentIds AS tSI
        ON tS.Id = tSI.studentId   
    

    UPDATE

    Sample split function that takes a comma-separated list and returns a table:

    CREATE FUNCTION [dbo].[fnSplit](
        @sInputList VARCHAR(8000) -- List of delimited items
      , @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
    ) RETURNS @List TABLE (item VARCHAR(8000))
    
    BEGIN
    DECLARE @sItem VARCHAR(8000)
    WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
     BEGIN
     SELECT
      @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
      @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
    
     IF LEN(@sItem) > 0
      INSERT INTO @List SELECT @sItem
     END
    
    IF LEN(@sInputList) > 0
     INSERT INTO @List SELECT @sInputList -- Put the last item in
    RETURN
    END
    

    And you call it like this: select * from fnSplit(@CommaSeparetedList,',');

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get values from two tables like in the following example: Suppose
Suppose you want to get a record from database which returns a large amount
Suppose I want to redirect all GET queries from one site to another, using
Suppose you want to get from point A to point B. You use Google
I want to get the people whose age is between these two values (suppose
I want to know about the best practices here. Suppose I want to get
Suppose I have a XmlNode and I want to get the value of an
Suppose I have table Person table Employee, which inherits Person. I want to get
Suppose I have a list of sets and I want to get the union
Suppose I have the string 1:2:3:4:5 and I want to get its last field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.