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

  • Home
  • SEARCH
  • 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 8868923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:22:03+00:00 2026-06-14T17:22:03+00:00

this is my query Create FUNCTION [dbo].[CountUses](@couponid INT) RETURNS INT AS BEGIN RETURN (

  • 0

this is my query

Create FUNCTION [dbo].[CountUses](@couponid INT)
RETURNS INT
AS
   BEGIN
   RETURN
    (
    SELECT  c.Id,
     c.Name,
     c.CreateDate,
     Count(cu.id) NofUses
  FROM   Coupon as  c
     JOIN CouponUse as cu
       ON c.id = cu.couponid
 GROUP  BY c.Id,
     c.Name,
     c.CreateDate
     )
 END 

its giving the error Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. where is the problem ?
Aside from the main answer, I’d also appreciate any comments you may have about optimizing my query…

  • 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-06-14T17:22:04+00:00Added an answer on June 14, 2026 at 5:22 pm

    If you want your function to return more than one value, then you need to look at Table-Valued Functions.

    These types of functions return a table and not just one value. Your current function is set up as a scalar function so it can only return one value.

    If you want a scalar value – let’s say just the count then your function will be similar to this:

    Create FUNCTION [dbo].[CountUses](@couponid INT)
    RETURNS INT
    AS
       BEGIN
       RETURN
        (
          SELECT Count(cu.id) NofUses  --- this can only return one column
          FROM   Coupon as  c
          JOIN CouponUse as cu
            ON c.id = cu.couponid
          WHERE cu.couponid = @couponid
         )
       END 
    

    If you intention is to return a table of data, then your function will be similar to this:

    Create FUNCTION [dbo].[CountUses](@couponid INT)
    RETURNS @new_table table
    (
      id int,
      name varchar(50),
      CreateDate datetime,
      NofUsers int
    )
    AS
    BEGIN
      INSERT INTO @new_table
      SELECT c.Id,
           c.Name,
           c.CreateDate,
           Count(cu.id) NofUses
          FROM   Coupon as  c
          JOIN CouponUse as cu
            ON c.id = cu.couponid
          WHERE cu.couponid = @couponid
          GROUP  BY c.Id,  c.Name,  c.CreateDate
    
       RETURN
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this working properly: CREATE FUNCTION dbo.GetLiveStream(@UserName NVARCHAR(MAX)) RETURNS TABLE AS RETURN
I have created the following function in my database Create FUNCTION [dbo].[CountUseer](@couponid INT) RETURNS
I have created a function in my database using this query: CREATE FUNCTION dbo.fnSplit(
I want to make this kind of query: create procedure something @name varchar(13) as
Query was this: CREATE TABLE `query` ( `id` int(11) NOT NULL auto_increment, `searchquery` varchar(255)
This is my query, CREATE VIEW employee_vu AS( SELECT employee_id,last_name employee,department_id FROM employees); I
In PostgreSQL, there is this very useful string_agg function, that allows query like: SELECT
I have a view name vw_AllJobsWithRecruiter. ALTER VIEW dbo.vw_AllJobsWithRecruiter AS SELECT TOP(SELECT COUNT(iJobID_PK) FROM
I have this query: SELECT name, lastname FROM contestant WHERE name= 'John' AND lastname
I've used the following split function: CREATE FUNCTION dbo.Splitfn(@String varchar(8000), @Delimiter char(1)) returns @temptable

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.