Select TOP 5 only returning one row when called through code, where same script returns 5 rows in SQL Server

Dragonrage

When using the following block of code to pull the top 5 records, I get wildly different results than running the same script in SQL Sever 2008.

DataTable results = new DataTable();
string cmdTxt = String.Format("select top 5 ID, NAME from TABLE where ID like @ID or NAME like @NAME order by ID asc");
using (SqlCommand cmd = new SqlCommand(cmdTxt, Butch.connection))
{
    cmd.Parameters.Add("@ID", SqlDbType.Char, 15).Value = IdBox.Text + "%";
    cmd.Parameters.Add("@NAME", SqlDbType.Char, 65).Value = IdBox.Text + "%";
    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
           adapter.Fill(results);
 }

For example passing the parameter '0' into IdBox, I get one row in results with the Id '01', but in SQL Server I get 5 rows back with Id's '000001', '000002', '000003', '000004', '000005'. Passing '00' instead will return zero rows from my code, but the same 5 from SQL Server. If I pass '00000' I will get the same results from code as I do from SQL sever.

Is there anyway of making my code return the same results of my SQL query? I have tried using more wild card symbols in my code parameters in case it was only checking it against one character, however that didn't work. I have the same code working elsewhere that uses a NVarChar SqlDataType, but I don't know why it isn't working here.

sticky bit

SqlDbType.Char corresponds to CHAR, which is blank padded, if needed, to be exactly the length given.

Your query ends up being something like:

select top 5 ID, NAME from TABLE where ID like '0%             ' or NAME like '%                                                                ' order by ID asc

And that pattern(s) just won't match.

Try SqlDbType.VarChar or SqlDbType.NVarChar (if the corresponding column is N... too).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select TOP row in SQL and get 5 rows of end table

From Dev

SQL Server : select only one row based on a field when there are several rows

From Dev

SQL, only returning rows where at least one more entry with the same value in one column is found

From Dev

SQL, only returning rows where at least one more entry with the same value in one column is found

From Dev

Teradata SQL - select distinct returning duplicate rows where one row has null values

From Dev

sum every 5 rows in one row in sql

From Dev

SQL JOIN with WHERE condition when two rows' values are the same and one row matches to two different rows

From Dev

In SQL how to not select rows where a column value is the same as the one in the row above it

From Dev

JOIN on multilple rows to SELECT only TOP row in SQL Sever

From Dev

sql query select only one row from multiple rows

From Dev

SQL Server pivot query returns only one row

From Dev

Select distinct rows where all values in a column are the same SQL Server

From Dev

SQL: Select only one row of table with same value

From Dev

SQL: Select only one row of table with same value

From Dev

SQL Server - Select rows where one column matches but the other does not

From Dev

How to select top 5 after 20 rows

From Dev

How to select top 5 after 20 rows

From Dev

Select last 5 rows in join query SQL Server 2008

From Dev

sql query returning only one row

From Dev

MySQL SELECT Statement Only Returns One Row

From Dev

Table Valued Function is returning only one row when the actual result contains multiple rows

From Dev

Dynamically select TOP rows in SQL Server

From Dev

SQL Server '=' returns no rows when rows exist

From Dev

Update only the top 1 row in sql server

From Dev

Select the value of a sibling row that can only be found through a parent row in SQL Server

From Dev

SQL View only returns one row

From Dev

Returning one row query where table has multiple rows

From Dev

Laravel 5 scope only returns one result

From Dev

Function SELECT that returns a row in SQL SERVER 2012

Related Related

  1. 1

    Select TOP row in SQL and get 5 rows of end table

  2. 2

    SQL Server : select only one row based on a field when there are several rows

  3. 3

    SQL, only returning rows where at least one more entry with the same value in one column is found

  4. 4

    SQL, only returning rows where at least one more entry with the same value in one column is found

  5. 5

    Teradata SQL - select distinct returning duplicate rows where one row has null values

  6. 6

    sum every 5 rows in one row in sql

  7. 7

    SQL JOIN with WHERE condition when two rows' values are the same and one row matches to two different rows

  8. 8

    In SQL how to not select rows where a column value is the same as the one in the row above it

  9. 9

    JOIN on multilple rows to SELECT only TOP row in SQL Sever

  10. 10

    sql query select only one row from multiple rows

  11. 11

    SQL Server pivot query returns only one row

  12. 12

    Select distinct rows where all values in a column are the same SQL Server

  13. 13

    SQL: Select only one row of table with same value

  14. 14

    SQL: Select only one row of table with same value

  15. 15

    SQL Server - Select rows where one column matches but the other does not

  16. 16

    How to select top 5 after 20 rows

  17. 17

    How to select top 5 after 20 rows

  18. 18

    Select last 5 rows in join query SQL Server 2008

  19. 19

    sql query returning only one row

  20. 20

    MySQL SELECT Statement Only Returns One Row

  21. 21

    Table Valued Function is returning only one row when the actual result contains multiple rows

  22. 22

    Dynamically select TOP rows in SQL Server

  23. 23

    SQL Server '=' returns no rows when rows exist

  24. 24

    Update only the top 1 row in sql server

  25. 25

    Select the value of a sibling row that can only be found through a parent row in SQL Server

  26. 26

    SQL View only returns one row

  27. 27

    Returning one row query where table has multiple rows

  28. 28

    Laravel 5 scope only returns one result

  29. 29

    Function SELECT that returns a row in SQL SERVER 2012

HotTag

Archive