views:

558

answers:

9

I have a web app where I am calling a SELECT DISTINCT query on two joined tables. If I try to run the query from the web page, I get the following error: "The text, ntext, or image data type cannot be selected as DISTINCT". When I run it from within SQL Management Studio, the query runs fine - no error. Even more interestingly, there are no text, ntext, or image data types in either of the tables.

It should be noted that I'm stepping through the code, and right before the query is executed, I'm copying the query from the "watch" window into Mgmt Studio, and it runs, when I step through and let .NET run it, the error is thrown. I'm using .NET 2, and the System.Data.SqlClient namespace.

Here is my query:

SELECT DISTINCT ResponseFormParent.* 
FROM ResponseFormParent 
INNER JOIN ResponseForm 
  ON ResponseFormParent.ResponseFormParentId = ResponseForm.ResponseFormParentId 
WHERE ResponseForm.RegistrationDeadline >= '5/1/2009'

ResponseFormParent has 3 ints, 1 datetime, and 1 nvarchar(50). ResponseForm has 4 ints, 1 datetime, 1 bit, and 1 nvarchar(255).

What is causing the problem? The error doesn't make sense.


UPDATE: code to call query

public DataSet ExecuteQuery(string sql)
{
 DataSet ds = null;
 try
 {
  using (SqlConnection dbconn = new SqlConnection(connectionString))
  using (SqlCommand dbcmd = new SqlCommand(sql, dbconn))
  {
   SqlDataAdapter dbadapter = new SqlDataAdapter(dbcmd);
   ds = new DataSet();
   dbadapter.Fill(ds);
  }
 }
 catch (Exception ex)
 {
  Utility.LogError(sql, ex);
 }
 return ds;
}
+7  A: 

You might be querying two different databases. Have you checked your Connection in your app to make sure you're actually querying the same database that you're querying against in SQL Mgmt Studio? It's also possible that you're pointing to different servers. You might want to check that in your Connection as well.

If either is the case, then it's most likely a database schema mismatch between the two databases you're querying against.

Joseph
There is only one database right now, so no, I'm not querying two different databases.
Rick
@Rick and you don't have multiple servers either?
Joseph
Just 1 web server, and 1 database server.
Rick
A: 

Are you sure the web app is connecting to the same database? (i.e. dev vs. production)?

Maybe someone changed that nvarchar(255) to a text column in one database but not the other...

Eric Petroelje
There is only one database right now, so no, I'm not querying two different databases.
Rick
A: 

Instead of stepping through the code and copying the output into Management Studio, run Profiler against the database and capture the exact query sent to the server. Then copy that (including the sp_executesql if it's there) and run it in Management Studio.

This will not only give you the exact query, but it will also confirm that it's running against the correct database.

Robin Day
Okay, just ran it with the profiler, and the query is exactly as I typed it above (and it also confirms all the questions about me using the proper database - which I am).
Rick
Can you try replacing the * with the actual column names in the table and see if it also gives the same error?
Robin Day
+1  A: 

Just tested a DISTINCT query on a table that contains an ntext column. It works as long as you avoid selecting the ntext column. When you include the ntext column, you get the error you name.

So are you sure your code is doing:

 SELECT DISTINCT ResponseFormParent.*

And not

 SELECT DISTINCT *
Andomar
I am sure. Either way, it shouldn't matter (with respect to the error I'm getting) since neither table has a text, ntext, or image column.
Rick
Have you tried replacing * with explicit column names? Experiment with just a single column to see if the error persists.
Andomar
After replacing * with the column names, the query was able to run. I am voting up your answer as helpful, but unfortunately it doesn't answer the original question - why it works in one place and not in the other, so I'm not marking it as the approved answer (yet).
Rick
Ok, cool it wors now. One possible explanation is that Sql Server has cached an old query plan, where * did include an ntext/image column. Those queries are cached for specific clients, so your app might end up with the old cached version, while Management Studio uses the new * expansion. Not sure.
Andomar
A: 

If try to select one column at a time the problem still persists ?

João Guilherme
None of the columns should be giving me a problem - there aren't any text, ntext, or image columns in either table to cast.
Rick
+1  A: 

What happens if you wrap your selection code into a stored procedure & call that from your C# code?

Garrett
As with Andomar's answer above, this does allow the query to run, so I'm marking this as helpful, but not marking it as the approved answer because it doesn't answer the original question - why the query works in one place, but not from code.
Rick
A: 

Have you checked if the same credentials are being used in both connections?

Badaro
Well you can't run the profiler from a non-admin account, but other than that, I am using the same credentials to connect to the Query Analyzer and Managment Studio as I am for the web app.
Rick
A: 

You're sure it hasn't got anything to do with using a DataAdapter? Have you tried your code using a reader ==> dbcmd.ExecuteReader() ?

fretje
A: 

Try specifying the columns instead of using select * (which should not be used on prod anyway). Could be the Data Adapter is just assuming you might have that type of column (just guessing here) when you use select *.

HLGEM