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 7585051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:04:45+00:00 2026-05-30T19:04:45+00:00

I have written this stored procedure for one my operations in the database, but

  • 0

I have written this stored procedure for one my operations in the database, but it is prompting with two errors. I am fairly new top this, so it would be great if somebody could correct the mistake.

CREATE PROCEDURE findVersions 
  @seg nvarchar(255),
  @str nvarchar(255)
AS

  DECLARE @UnsegQuery AS nvarchar(255)

  SET @UnsegQuery = SELECT DISTINCT UnsegmQuery 
                      FROM tbData 
                     WHERE SegQuery = @seg 

  SELECT TOP 1 Strategy, Versions, CGNum 
    FROM tbData 
   WHERE Strategy = @str 
     AND SegQuery = @seg 
ORDER BY CGnum DESC
  UNION
  SELECT TOP 1 Strategy, Versions 
    FROM tbData 
   WHERE Strategy = 'BF' 
     AND UnsegmQuery = @UnsegQuery
  UNION
  SELECT Strategy, Versions 
    FROM (SELECT ROW_NUMBER() OVER (ORDER BY nDCG DESC) AS rownumber 
            FROM tbData) AS foo 
   WHERE rownumber > 1

Errors:

Msg 156, Level 15, State 1, Procedure findVersions, Line 10
Incorrect syntax near the keyword ‘SELECT’.
Msg 156, Level 15, State 1, Procedure findVersions, Line 13
Incorrect syntax near the keyword ‘UNION’

Any suggestions?

Update
Eg: of what I have to do with the queries. I have to display the first result from the first query, first result from the second query and then the remaining three from the first query results.

1st class: (has 4 student)

Tom (Has highest score)
Rex (Hss second highest score)
Rambo (HAs 3rd highest score)
Betty (Has least score)

2nd class: (has 1 student)

Spooky (Has the highest score)

Required result order in DataControl:

Tom
Spooky
Rex
Rambo
Betty
  • 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-30T19:04:47+00:00Added an answer on May 30, 2026 at 7:04 pm

    To address the “Incorrect syntax near the keyword ‘SELECT'” error, this statement needs to be written using parenthesis around the SELECT:

    SET @UnsegQuery = (SELECT DISTINCT UnsegmQuery FROM tbData WHERE SegQuery = @seg)
    

    If you use SET to set the value of @UnsegQuery, you also need to be certain that the SELECT will only return one value. Alternatively, you can use:

    SELECT DISTINCT @UnsegQuery = UnsegmQuery FROM tbData WHERE SegQuery = @seg
    

    to set the value of @UnsegQuery. In this case, if more than one record is returned @UnsegQuery will be set to the value of the last record.

    The “Incorrect syntax near the keyword ‘UNION'” error is occurring because you can’t use ORDER BY before a UNION. You can only use an ORDER BY after the last UNION statement (see the MSDN documentation for more info).

    UPDATE
    To answer your question from your last comment, the correct syntax for the last part of the query should look something like this:

    SELECT foo.Strategy, foo.Versions 
    FROM (
        SELECT Strategy, Versions, ROW_NUMBER() OVER (ORDER BY nDCG DESC) AS [rownumber] 
        FROM tbData) foo 
    WHERE foo.rownumber > 1
    

    That statement will select all records from tbData ordered by nDCG descending except the first record. I’m not sure that helps you solve the problem, but the syntax is correct.

    UPDATE 2

    OK, I think I understand the problem. You want to select all of the rows from the table, but you want one specific record to be first, a different specific record to be second, and then all the rest. One approach to doing this would be to use a CASE WHEN statement to assign values to the desired first row, desired second row, and then sort by that value. For example:

    DECLARE @myTable TABLE([ID] INT, [Student] VARCHAR(10))
    INSERT INTO @myTable VALUES(1, 'Tom')
    INSERT INTO @myTable VALUES(2, 'Spooky')
    INSERT INTO @myTable VALUES(3, 'Rex')
    INSERT INTO @myTable VALUES(4, 'Rambo')
    INSERT INTO @myTable VALUES(5, 'Betty')
    DECLARE @firstID INT, @secondID INT
    SET @firstID = 2
    SET @secondID = 4
    
    SELECT * 
    FROM @myTable
    ORDER BY
        CASE 
            WHEN [ID] = @firstID THEN 1 
            WHEN [ID] = @secondID THEN 2
            ELSE 3 
        END,
        [ID]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure which is written in c#: SqlConnection scn = new
I have written a stored procedure, & to execute this procedure I need to
I have written a mysql stored procedure in my magento database to perform a
I have written below stored procedure. In which i need to create Two Temporary
Hello This is a conceptual question mostly. I have written a stored procedure in
In my code I have written this but it fails to compile: In Class1.h:
I have a boolean variable value stored in an SQL Server database. This is
I have written a Stored procedure which return some fields from a temporary table
Scenario I have a stored procedure written in T-Sql using SQL Server 2005. SEL_ValuesByAssetName
I have written a package that has a stored procedure and a REF cursor.

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.