Npgsql : how to get PQntuples? - npgsql

I'm trying to get the number of rows returned by a SELECT command.
RowAffected works only for insert, update and so on, but not for select.
PostgreSQL has the PQntuples() function but it doesn't exist in Npgsql and I can't get the answer out of any of the various versions of the Rows property (or Statement.Rows, or... ?).
Is there something I'm missing, or is it not possible at all ?
Thank you.
Nynn

Why not simply count the rows yourself as you're processing them with NpgsqlDataReader? In other words, simply increment a counter every time you call NpgsqlDataReader.Read()...

Related

Replace is not working for weird character

I use UPDATE a SET GR_P = REPLACE(GR_P,'','') FROM mytable a to replace things.
But replace function is not working for below charter:
In Query analyzer it works but when I used SSIS Execute SQL task or OLEDB Source then it is giving me error:
No Connection manager is specified.
In Toad against Oracle (since that's one of your tags), I issued this (pressing ALT-12 to get the female symbol) and got 191 as a result. note selecting it back using CHR(191) shows an upside-down question mark though.
select ascii('♀') from dual;
Given that, this worked but it's Oracle syntax, your mileage may vary.
UPDATE mytable SET GR_P = REPLACE(GR_P, CHR(191));
Note if it does not work, that symbol could be for another control character. You may need to use a regular expression to eliminate all characters not in a-zA-Z0-9, etc. I suspect you'll need to update your tags to get a more accurate answer.
Maybe this info will help anyway. Please post back what you find out.

sql query replace all specific values with a different value

I am attempting to replace specific values in one column with a new value but it does not work (no errors, no replacement of values, nothing happen).
UPDATE Components
SET Unit='kg'
WHERE Unit='КГ'
How can I replace all values "КГ" with "kg" in column Unit?
I thing that your Unit column is NVarChar() data type. Try following query:
UPDATE Components
SET Unit=N'kg'
WHERE Unit=N'КГ'
Another reason: If you have instead of update trigger on Components table and not update this column on it, your update not affected and no raise error too.
I suggest to use Quotename to resolve this, it is used for this type of string.
UPDATE Components
SET Unit=QUOTENAME('kg')
WHERE Unit=QUOTENAME('КГ')
It is simple and direct query which you run, then its ok. Other wise as #Mehdi said, I also fever that statement.
http://www.c-sharpcorner.com/Blogs/7515/quotename-function-in-sql-server.aspx

SqlDataReader does not return all records

I am experiencing a problem with ADO.NET SqlDataReader. When I run underlying stored procedure directly in SSMS - it returns 1.7 million of records. When I run a related VB.NET code that fills an ADO.NET DataTable - I also get 1.7 million of records.
But when I run a loop to fill a Generic list like this
While i_oDataReader.Read
m_aFullIDList.Add(i_oDataReader.GetInt32(0))
End While
It returns a lot less records and that number can vary. At the end of the loop if I check m_aFullIDList.Count it ca be 100000 or 500000 etc. Any idea why and how to fix it? Thanks!
Thanks to reference pointed by #Tim Schemlter I found the option "CommandBehavior.SequentialAccess" for DataReader creation. That fixed the issue. E.g. instead of
drReader = oCommand.ExecuteReader();
use
drReader = oCommand.ExecuteReader(CommandBehavior.SequentialAccess);
and it works correctly.
Have you tried using the GetInt64() method instead? I realize that with only 1.7 million records, the GetInt32() method should be large enough; I was more just curious.
Also, if you think that SQL cannot keep up with the DataReader, have you tried adding a wait in the loop to allow it to catch up?

SQL Server Optimization: Function in SELECT calculated before or after WHERE Clause?

I was just wondering if in SQL Server if in a statement like this:
SELECT A.Field1, dbo.someFunction(A.IdentifierID) As Field
FROM Table A WHERE A.IdentifierID = 1000
Will it call someFunction for all the rows in the table, or will it call it once?
Thanks!
It will be called for every row of result
the count of calls depends on the resulted rows, i.e, if the output of the query 100 row the it will be called 100 times, not for all rows in the table
but if it were in the where clause, the it will evaluated for each row
Will call it for every row, but since you are selecting a specific one (or so it seems), it will be called once on your example.
As vlad commented you should try it and see. There are of course two options:
It could call it just once knowing IdentifierID will always be 1000.
but that might not be the right thing...
It could call it for each row - maybe the function has side-effects?
I strongly suspect #2.
Compliant SQL servers are supposed to appear as if they apply the WHERE clause before they apply the SELECT clause. So it should appear to evaluate the function at most once for each row that satisfies WHERE A.IdentifierID = 1000.
But database engines are free to optimize however they like, as long as they give you the same results the standards require. So in your case, since you're selecting a single ID number, it might only have to evaluate the function once.

MySQL Query Nightmare with RETs data

For those of you who have actually delt with RETS may be able to give me a hand here. The problem occurs when multiple properties are tied into the RETS data even though the property is sold. Basically what I need is to be able to check the database with the SELECT statement against three fields. The fields in question would be C_StreetName, C_StreetNumber, and C_PostalCode.
To make this clear what I want is some type of way to check for duplicates while gathering the dataset, this can't be done in php because of how the data is returned through the application. So if it finds another record with the same C_StreetName, C_StreetNumber, and C_PostalCode it will remove them from the dataset. Ideally it would be nice if it could also check the Status of the two to find out if one is Expired or Sold before removing them from the data.
I'm not familiar with complex SQL functions, I was looking at the IF statement until I found that can only be used while storing data not the other way around. And the CASE statement but it just doesn't seem like that would work.
If you guys have any suggestions on what I should use I'd appreciate it. Hopefully there is a way to do this and keep in mind this is only one table I am accessing I don't have any Joins.
Thanks in advance.
Here's something to get you going in the right direction. I haven't tested this, and am not sure you can nest a case expression inside max() in mysql.
What this accomplishes is to output one row per unique combination of street name, number and postcode, with a status of 'Expired' or 'Sold' taking precedence over other values. That is, if there's a row with 'Expired' it will be output in preference to non-expired and non-sold, and a row with 'Sold' will be output if it exists, regardless of what other rows exist for that property. The case statement just converts the status codes into something orderable.
select
C_StreetName,
C_StreetNumber,
C_PostalCode,
max(
case status
when 'Expired' then 1
when 'Sold' then 2
else 0
end) as status
group by
C_StreetName,
C_StreetNumber,
C_PostalCode;

Resources