1

I have a problem with query in C#.

I have this part of code :

string query1 = @"
            SELECT TOP @howManyRows * FROM
            (
            SELECT
               PRODUCTCODE_.ID_ AS PRODUCTCODE_ID_,
               PRODUCTCODE_.CATEGORY_ AS CATEGORY_,
               PRODUCTCODE_.DESCRIPTION_ AS DESCRIPTION_,
               PRODUCTCODE_.MANUFACTURER_ AS MANUFACTURER_,
               PRODUCTLINE_.CREATION_DATE_ AS CREATION_DATE_,
               ROW_NUMBER() OVER (ORDER BY PRODUCTCODE_.CATEGORY_) AS ROWNUMBER_,
               TOTALROWS_ = COUNT(*) OVER()
            FROM
               PRODUCTCODE_
            INNER JOIN
               PRODUCTLINE_ ON PRODUCTLINE_.ID_ = PRODUCTCODE_.PRODUCTLINE_ID_        
            ) _tmpList
            WHERE 
               ROWNUMBER_ >= @startingWith
               ORDER BY CATEGORY_
            ";


SqlParameter param1 = new SqlParameter();
param1.ParameterName = "@howManyRows";
param1.Value = resultPerPage; //`resultPerPage` is an integer function parameter

SqlParameter param2 = new SqlParameter();
param2.ParameterName = "@startingWith";
param2.Value = startsWith;  //`startWith` is an integer function parameter

SqlCommand cmd = new SqlCommand( query1, connect );
cmd.Parameters.Add( param1 );
cmd.Parameters.Add( param2 );

When debug arrived to SqlDataReader reader = cmd.ExecuteReader(); then the exception is thrown:

Incorrect syntax near @howManyRows ...

Why ? I defined and added howManyRows with Parameters property.

Where is my mistakes ?

7 Answers 7

7

Change your top query syntax from

SELECT TOP @howManyRows * FROM

to

SELECT TOP (@howManyRows) * FROM
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome. Never knew you could do this.
@Fraz: the () is needed to @startingWith from WHERE clause ?
3

You need parenthesis to parametrise TOP

SELECT TOP (@howManyRows) * FROM

Comments

2

Try wrapping the parameter specified next to TOP in parentheses, like so:

SELECT TOP (@howManyRows) * FROM

Comments

1

This will work.

SELECT
           PRODUCTCODE_.ID_ AS PRODUCTCODE_ID_,
           PRODUCTCODE_.CATEGORY_ AS CATEGORY_,
           PRODUCTCODE_.DESCRIPTION_ AS DESCRIPTION_,
           PRODUCTCODE_.MANUFACTURER_ AS MANUFACTURER_,
           PRODUCTLINE_.CREATION_DATE_ AS CREATION_DATE_,
           ROW_NUMBER() OVER (ORDER BY PRODUCTCODE_.CATEGORY_) AS ROWNUMBER_,
           TOTALROWS_ = COUNT(*) OVER()
        FROM
           PRODUCTCODE_
        INNER JOIN
           PRODUCTLINE_ ON PRODUCTLINE_.ID_ = PRODUCTCODE_.PRODUCTLINE_ID_        
        ) _tmpList
        WHERE 
           ROWNUMBER_ between @startingWith and (@startingWith + @howManyRows)
           ORDER BY CATEGORY_

Comments

1

You need to add a couple of parenthesis to make it work.

SELECT TOP (@howManyRows) * FROM

Comments

1

Use SELECT TOP(@howManyRows) syntax

Comments

1

You can do

SELECT TOP (@howManyRows) * FROM

But this really depends on the Database Server you are using. For example, this is only supported from MSSQL Server 2005 upwards

If this does not work you can do the following...

You can include this into your query string. But this can result in Sql Injection if you don't check the value of your variable.

Sample

int top = 10;
Int32.TryParse(howManyRows.ToString(), out top);

string query1 = "SELECT TOP " + top.ToString() + @" * FROM
(
SELECT
    PRODUCTCODE_.ID_ AS PRODUCTCODE_ID_,
    PRODUCTCODE_.CATEGORY_ AS CATEGORY_,
    PRODUCTCODE_.DESCRIPTION_ AS DESCRIPTION_,
    PRODUCTCODE_.MANUFACTURER_ AS MANUFACTURER_,
    PRODUCTLINE_.CREATION_DATE_ AS CREATION_DATE_,
    ROW_NUMBER() OVER (ORDER BY PRODUCTCODE_.CATEGORY_) AS ROWNUMBER_,
    TOTALROWS_ = COUNT(*) OVER()
FROM
    PRODUCTCODE_
INNER JOIN
    PRODUCTLINE_ ON PRODUCTLINE_.ID_ = PRODUCTCODE_.PRODUCTLINE_ID_        
) _tmpList
WHERE 
    ROWNUMBER_ >= @startingWith
    ORDER BY CATEGORY_
";

1 Comment

Yes, I used that but at every line of SQL syntax (described above) I have to use " " and + symbol that is very difficult.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.