Why is my count returning a syntax error?

Sir AskA Lot

This is my code:

ALTER PROCEDURE [db_owner].[get_checkin_likes]
@likes integer
AS
Select count(*) 
from
(
  select checkin_id as Checkin, users_id as 'Liked by'
  from checkin_likes
  where @likes = checkin_id
)

When I execute this command I expect two things:

  1. I expect to get a list of all the users that like the particular checkin I selected
  2. I expect to get a count of how many users like the particular checkin

However, when I try to run the command it returns this:

Msg 102, Level 15, State 1, Procedure get_checkin_likes, Line 9
Incorrect syntax near ')'.

Any suggetions as to how to fix this or why it's happening?

LordBaconPants

You need to name your subquery; try putting something after the final parenthesis:

ALTER PROCEDURE [db_owner].[get_checkin_likes]
@likes integer
AS
Select count(*) 
from
(
  select checkin_id as Checkin, users_id as 'Liked by'
  from checkin_likes
  where @likes = checkin_id
) a

I probably should mention that when you run this query you will only get the count back. I'm not sure of your use-case here, but you can do an OVER and return everything with a count at the end

EDIT:

I would personally do this and have it all in the one record set:

ALTER PROCEDURE [db_owner].[get_checkin_likes]
@likes integer
AS

  select checkin_id as Checkin
    , users_id as 'Liked by'
    , COUNT(checkin_id) OVER () AS total_rows, 
  from checkin_likes
  where @likes = checkin_id

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Syntax error in 'AS' in my count query

Why Is My Count Statement Returning 0 Rows

Postgres COUNT FILTER syntax error - why?

Why is my implementation of IExceptionHandler not returning the error response?

Not sure why my function keeps returning an error?

Why is my code returning an error for this problem?

Why is my CASE expression returning an error?

Why is there a syntax error in my database table?

Why is there a syntax error in my Python function?

Why is my prepared statement causing this syntax error?

Why is my function returning full list instead of just a total count?

Why is my query method for my SQLite returning an error?

Promise not returning error - returning Syntax error

Syntax error in mysql count(*)

Why is my email check variable returning an error as undefined

Why is my code not returning an error message when @@ROWCOUNT=0?

Why is my calculated column using CASE returning an error?

Why is my fs error returning an empty object in Node.js?

Why my google search macro is returning error 424 - Object required?

Why is my SQL FOREIGN KEY clause returning an error?

Insert Into returning a syntax error in SQL

Sympy/Numpy returning syntax error

Why am I getting a syntax error at my $in command?

Why does the syntax error in my dictionary point to the ":" and suggest to use a comma?

I'm getting a syntax error on my IF statement, not sure why?

Why is my script working but it shows a syntax error in console?

Why are my End values giving me a syntax error?

Why do I keep getting a syntax error with my hash?

Why my code not executing before syntax error causing line in JavaScript