what is the return value of Delete Method? - return-value

In PetaPOCO ( http://www.toptensoftware.com/petapoco ) Delete method returns an integer value.
What I want to know is, what is the return value of Delete Method? Is it "Number of Records" affected? or is it some "error-code"?
I basically want to confirm that the DELETE operation was successful or not.

It's the Number of Records affected. If there's an error it will throw an exception

Related

Pass Column to Function where the column can be empty

I am trying to pass a column to a function where sometimes the column passed can be empty or blank.
For example
def test (df,segment):
score_df = df \
.withColumn('model_segment', when(lit(segment) =='',lit('')).otherwise(col(segment)))
return score_df
This works
test(df,'my_existing_column').show()
However this errors
test(df,'').show()
This errors with the message cannot resolve '``' given input columns
I get why it is doing that, but how would I go about handling this kind of scenario?
You can get a list of dataframe fields by df.columns,then check whether the incoming parameter exists in the field list, if it exists, execute show action, otherwise trigger a custom exception that the field does not exist.

Sql case stetement to check existing records and taking one

I have two parameters X and Y
Rules for these are only one of them can be null. They both can be existing, it's ok but they both can't be null.
I'm using this to check if they exist in database so I can assign one and rest of the SP can continue inserting.
SELECT #Id=id FROM Table WHERE (No = #x) OR (No = #y)
What I want to add is if they are both existing I want the Id to be the Id of #x.
I can't get the Case Statement right in my mind. Normally this is a no brainer but somehow I managed to get stuck.
ISNULL() will take the first non null value it finds.
SELECT #Id=id FROM Table WHERE No = ISNULL(#x, #y)

How do i get the ordinal of a column in a DataReader

How can i find out if a column exists in a DataReader's results set?
i try:
int columnOrdinal = reader.GetOrdinal("LastName");
columnExists = (columnOrdinal < 0);
but GetOrdinal throws an exception if the column does not exist. My case is not exceptional. It's the opposite. It's...ceptional.
Note: Not related to my question but, the real reason i want to know if a column exists is because i want to get the ordinal position of a column, without throwing an exception if the column doesn't exist:
int columnOrdinal = reader.GetOrdinal("Lastname");
Note: Not related to my question but, the real reason i want to know if a column exists, because i want to know if the column contains null:
itIsNull = reader.IsDBNull(reader.GetOrdinal("Lastname"));
Unfortunately IsDBNull only takes an ordinal, and GetOrdinal throws an exception. So i'm left with:
if (ColumnExists(reader, "Lastname"))
{
itIsNull = reader.IsDBNull(reader.GetOrdinal("Lastname"));
}
else
itIsNull = false;
Note: Not related to my question but, the real reason i want to know if a column exists is because there will be times where the column will not be present in the results set, and i don't want to throw an exception processing database results, since it's not exceptional.
There is a limit to what you can do since the IDataReader doesn't expose much that helps. Using the loop as shown in the answer to a similar question
Check for column name in a SqlDataReader object
You could, with the first row you process, build a simple dictionary that is keyed by column name with ordinals as values (or a HashSet if you don't care about the ordinal values). Then you can just use columnDictionary.ContainsKey("LastName") as your test. You would only build the dictionary once, for the first row encountered, then all the subsequent rows would be fast.
But to be honest, compared with database time, the time consumed by using as-is the solution in that other stackoverflow qeustion would probably be negligible.
Edit: additional possibilities here: Checking to see if a column exists in a data reader

Verify if insert was successful in Symfony2/Doctrine/MongoDB

My application is not alerting me to a failed insert when adding a record to a MongoDB collection with a unique index...
$dm->flush()
... does not complain. I'm trying to figure out what the array parameter to flush should look like to see if that helps but getting nowhere. flush does not return anything on success or failure.
Any ideas on how I can verify, in my PHP/Symfony2 application, whether the insert worked without needing to query the db immediately after inserting?
Got it. Per this link, must provide array("safe" => true) as a parameter to the write operation.
$dm->flush(array('safe'=>true));
So when using the code above and trying to insert into a unique index an exception will be thrown.

What is the best way to return an error from a TSQL Proc?

Here’s the scenario:
You load a page, which renders based on the data in the database (call these the “assumptions”)
Someone changes the assumptions
You submit your page
ERROR!
The general pattern to solve this problem is this (right?):
In your save proc, inside a begin and commit transaction, you validate your assumptions first. If any of them changed, you should return a graceful error message, something like an XML list of the ID’s you had problems with, that you handle in the page rather than let it be handled by the default error handling infrastructure.
So my question is what is the best way to do that?
Return the xml list and an error flag in out parameters that are unset and 0 if it completes correctly?
Use an out parameter to return an error status, and the result of the proc be either the error list or the valid results of the proc?
Something else? (I should note that raiseerror would cause the proc to be in error, and get picked up by the default error handling code)
Update: I would like to return a maniplatable list of IDs that failed (I plan to highlight those cells in the application). I could return them in CSV format in RAISEERROR, but that seems dirty.
I agree - I like RAISEERROR:
-- Validate #whatever
IF #whatever >= '5'
BEGIN
RAISERROR ('Invalid value for #whatever - expected a value less than 5, but received %s.', 10, 1, #whatever)
RETURN 50000
END;
Use the RAISERROR function with the appropriate severity and/or wait level. If you use a low severity this does not necessarily cause an exception, as you contend, and with .Net at least its pretty simple to retrieve these messages. The only downside is that with the StoredProcedure command type in .Net messages are only pumped in groups of 50.
Stored procedures can return multiple result sets. Based on your update, you could also insert errored ids into a temporary table, and then at the end of your procedure select the records from that table as an additional result set you can look at.
I would do an output parameter with a message, the return will already have something which is not 0 if there is an error
also be careful with doomed transaction and check with xact_error, see Use XACT_ABORT to roll back non trapable error transactions