Showing posts with label writing. Show all posts
Showing posts with label writing. Show all posts

Friday, March 30, 2012

RaisError in a function? Any ideas

Writing a function and need to raise an error under certain conditions. This is a gross over-simplification of the function. But you get the idea....I need to use @.@.ROWCOUNT

Code Snippet

create Function GetTheValue

(@.Input INT)

returns Bigint

as

begin

declare @.Output bigint

select TheValue

from TheTable

where RowID = @.Input

IF @.@.ROWCOUNT = 0

RAISERROR('Oops',15,1)

return @.INput

END

Basically, the way this usually works is that you return an invalid value and check for it. Like in this case, you can never get a negative value for @.@.rowcount, just return -1 or something like this.

You can force an error to occur by doing something like 0/1, but I was never fond of that method since the error message doesn't correspond to the actual error.

|||

As you noticed, RAISERROR is NOT allowed in a FUNCTION.

A FUNCTION cannot have any output EXCEPT the FUNCTION value. Of course, it will 'crash' when unacceptable actions occur, such as divide by zero.

Louis indicated an acceptable method of handling a data anomaly.

|||

Thanks....

I'll probably use:

Code Snippet

if @.@.rowcount =0

select 1/0

RAISEERROR from CLR stored procedures

I'm writing:

SqlContext.Pipe.ExecuteAndSend(new SqlCommand("RAISERROR ( '" + e.Message + "', 11, 1)"));

to execute a RAISEERROR from a CLR stored procedure.

It works, but in the SqlError list I get in the client, there are two more errors that say something like '{System.Data.SqlClient.SqlError: A .NET Framework error occurred during execution of user defined routine or aggregate 'Customer__Update':
System.Data.SqlClient.SqlException: noupdate ...' ...

Is there any way to just get the message I'm sending?

Thanks

The issue here is that by ExecuteAndSend a command which executes a RAISERROR, your RAISERROR will be called in the SQL layer, and an error will be raised to the client (which is good and is what you see as the first error). However, this error will be caught by the CLR when ExecuteAndSend returns., and it will be seen as a unhandled error and wrapped by SQL when your method returns as error 6522 (CLR Error).
The way to handle this is to have your Pipe.ExecuteAndSend in a try block and then have an empty catch block which will eat the error coming back from SQL:


try { p.ExecuteAndSend(cmd); } catch { }


Hope this helps!
Niels


|||Thanks Neils.

It got better, now I get to SqlErrors in the Errors collection one with my message and another saying 'The statement has been terminated.'

However, with or without the try/catch, I got a 50000 error, not the 6522.

I got the 6522 when I created the SqlCommand and executed it directly. If I execute using the SqlPipe.ExecuteAndSend, I got 50000|||

aaguiar wrote:


It got better, now I get to SqlErrors in the Errors collection one with my message and another saying 'The statement has been terminated.'

Hmm, I don't see that here, I only get one message. However if I execute the CLR proc from inside a T-SQL proc I get all kind of weird errors - that's a bug in this build, and will be fixed. Can you post your code, both server side as well as client side?

aaguiar wrote:


However, with or without the try/catch, I got a 50000 error, not the 6522.
I got the 6522 when I created the SqlCommand and executed it directly. If I execute using the SqlPipe.ExecuteAndSend, I got 50000

Sure, but when you do ExecuteAndSend (without the dummy try/catch), I'm willing to bet your second error is 6522. 50000 is expected as that is "user defined error".
Niels|||

I tried this but the error is not caught by a begin try/end try ... begin catch/end catch block in transact sql code. Is there a way to send a custom error which can be caught in TSQL code?

|||I am having this problem too. The RAISERROR call from my CLR Routine is not being caught in the TRY/CATCH T-SQL block. Can anyone tell me why?sql

RAISEERROR from CLR stored procedures

I'm writing:

SqlContext.Pipe.ExecuteAndSend(new SqlCommand("RAISERROR ( '" + e.Message + "', 11, 1)"));

to execute a RAISEERROR from a CLR stored procedure.

It works, but in the SqlError list I get in the client, there are two more errors that say something like '{System.Data.SqlClient.SqlError: A .NET Framework error occurred during execution of user defined routine or aggregate 'Customer__Update':
System.Data.SqlClient.SqlException: noupdate ...' ...

Is there any way to just get the message I'm sending?

Thanks

The issue here is that by ExecuteAndSend a command which executes a RAISERROR, your RAISERROR will be called in the SQL layer, and an error will be raised to the client (which is good and is what you see as the first error). However, this error will be caught by the CLR when ExecuteAndSend returns., and it will be seen as a unhandled error and wrapped by SQL when your method returns as error 6522 (CLR Error).
The way to handle this is to have your Pipe.ExecuteAndSend in a try block and then have an empty catch block which will eat the error coming back from SQL:


try { p.ExecuteAndSend(cmd); } catch { }


Hope this helps!
Niels


|||Thanks Neils.

It got better, now I get to SqlErrors in the Errors collection one with my message and another saying 'The statement has been terminated.'

However, with or without the try/catch, I got a 50000 error, not the 6522.

I got the 6522 when I created the SqlCommand and executed it directly. If I execute using the SqlPipe.ExecuteAndSend, I got 50000|||

aaguiar wrote:


It got better, now I get to SqlErrors in the Errors collection one with my message and another saying 'The statement has been terminated.'

Hmm, I don't see that here, I only get one message. However if I execute the CLR proc from inside a T-SQL proc I get all kind of weird errors - that's a bug in this build, and will be fixed. Can you post your code, both server side as well as client side?

aaguiar wrote:


However, with or without the try/catch, I got a 50000 error, not the 6522.
I got the 6522 when I created the SqlCommand and executed it directly. If I execute using the SqlPipe.ExecuteAndSend, I got 50000

Sure, but when you do ExecuteAndSend (without the dummy try/catch), I'm willing to bet your second error is 6522. 50000 is expected as that is "user defined error".
Niels|||

I tried this but the error is not caught by a begin try/end try ... begin catch/end catch block in transact sql code. Is there a way to send a custom error which can be caught in TSQL code?

|||I am having this problem too. The RAISERROR call from my CLR Routine is not being caught in the TRY/CATCH T-SQL block. Can anyone tell me why?

RAISEERROR from CLR stored procedures

I'm writing:

SqlContext.Pipe.ExecuteAndSend(new SqlCommand("RAISERROR ( '" + e.Message + "', 11, 1)"));

to execute a RAISEERROR from a CLR stored procedure.

It works, but in the SqlError list I get in the client, there are two more errors that say something like '{System.Data.SqlClient.SqlError: A .NET Framework error occurred during execution of user defined routine or aggregate 'Customer__Update':
System.Data.SqlClient.SqlException: noupdate ...' ...

Is there any way to just get the message I'm sending?

Thanks

The issue here is that by ExecuteAndSend a command which executes a RAISERROR, your RAISERROR will be called in the SQL layer, and an error will be raised to the client (which is good and is what you see as the first error). However, this error will be caught by the CLR when ExecuteAndSend returns., and it will be seen as a unhandled error and wrapped by SQL when your method returns as error 6522 (CLR Error).
The way to handle this is to have your Pipe.ExecuteAndSend in a try block and then have an empty catch block which will eat the error coming back from SQL:


try { p.ExecuteAndSend(cmd); } catch { }


Hope this helps!
Niels


|||Thanks Neils.

It got better, now I get to SqlErrors in the Errors collection one with my message and another saying 'The statement has been terminated.'

However, with or without the try/catch, I got a 50000 error, not the 6522.

I got the 6522 when I created the SqlCommand and executed it directly. If I execute using the SqlPipe.ExecuteAndSend, I got 50000

|||

aaguiar wrote:


It got better, now I get to SqlErrors in the Errors collection one with my message and another saying 'The statement has been terminated.'

Hmm, I don't see that here, I only get one message. However if I execute the CLR proc from inside a T-SQL proc I get all kind of weird errors - that's a bug in this build, and will be fixed. Can you post your code, both server side as well as client side?

aaguiar wrote:


However, with or without the try/catch, I got a 50000 error, not the 6522.
I got the 6522 when I created the SqlCommand and executed it directly. If I execute using the SqlPipe.ExecuteAndSend, I got 50000

Sure, but when you do ExecuteAndSend (without the dummy try/catch), I'm willing to bet your second error is 6522. 50000 is expected as that is "user defined error".
Niels|||

I tried this but the error is not caught by a begin try/end try ... begin catch/end catch block in transact sql code. Is there a way to send a custom error which can be caught in TSQL code?

|||I am having this problem too. The RAISERROR call from my CLR Routine is not being caught in the TRY/CATCH T-SQL block. Can anyone tell me why?

Wednesday, March 28, 2012

RAID 5 better for read only databases than RAID 10 ?

We have a farm of servers that we replicate data to as a scale out farm. The
only thing writing to it is from replication. Its like our read only farm.
Is it better to go RAID 5 from a performance perspective as compared to RAID
10 ? We are not concerned about saving on space, but want to squeeze every
ounce of performance from a well configured server.
Thanks
General opinion says RAID5 provides redundancy and good read performance.
RAID1 is good for write performance + redundancy. RAID10 is good for mixed
environments (read\write).
However, the results of these tests may change according to your hardware.
So it would be the best if you could try this out in your own specific
environment. You decide which is the best one for you.
Ekrem nsoy
"Hassan" <hassan@.test.com> wrote in message
news:eJEWgIrRIHA.5136@.TK2MSFTNGP04.phx.gbl...
> We have a farm of servers that we replicate data to as a scale out farm.
> The only thing writing to it is from replication. Its like our read only
> farm.
> Is it better to go RAID 5 from a performance perspective as compared to
> RAID 10 ? We are not concerned about saving on space, but want to squeeze
> every ounce of performance from a well configured server.
> Thanks
|||For the same number of disk spindles, RAID 5 should outperform RAID 10 for
read-only databases.
Kevin G. Boles
TheSQLGuru
Indicium Resources, Inc.
kgboles a earthlink dt net
"Hassan" <hassan@.test.com> wrote in message
news:eJEWgIrRIHA.5136@.TK2MSFTNGP04.phx.gbl...
> We have a farm of servers that we replicate data to as a scale out farm.
> The only thing writing to it is from replication. Its like our read only
> farm.
> Is it better to go RAID 5 from a performance perspective as compared to
> RAID 10 ? We are not concerned about saving on space, but want to squeeze
> every ounce of performance from a well configured server.
> Thanks
|||If saving space is not an issue, I'd go with RAID10 and configure sufficient
number of spindles to get the required performance.
Linchi
"Hassan" wrote:

> We have a farm of servers that we replicate data to as a scale out farm. The
> only thing writing to it is from replication. Its like our read only farm.
> Is it better to go RAID 5 from a performance perspective as compared to RAID
> 10 ? We are not concerned about saving on space, but want to squeeze every
> ounce of performance from a well configured server.
> Thanks
>

RAID 5 better for read only databases than RAID 10 ?

We have a farm of servers that we replicate data to as a scale out farm. The
only thing writing to it is from replication. Its like our read only farm.
Is it better to go RAID 5 from a performance perspective as compared to RAID
10 ? We are not concerned about saving on space, but want to squeeze every
ounce of performance from a well configured server.
ThanksGeneral opinion says RAID5 provides redundancy and good read performance.
RAID1 is good for write performance + redundancy. RAID10 is good for mixed
environments (read\write).
However, the results of these tests may change according to your hardware.
So it would be the best if you could try this out in your own specific
environment. You decide which is the best one for you.
--
Ekrem Önsoy
"Hassan" <hassan@.test.com> wrote in message
news:eJEWgIrRIHA.5136@.TK2MSFTNGP04.phx.gbl...
> We have a farm of servers that we replicate data to as a scale out farm.
> The only thing writing to it is from replication. Its like our read only
> farm.
> Is it better to go RAID 5 from a performance perspective as compared to
> RAID 10 ? We are not concerned about saving on space, but want to squeeze
> every ounce of performance from a well configured server.
> Thanks|||For the same number of disk spindles, RAID 5 should outperform RAID 10 for
read-only databases.
--
Kevin G. Boles
TheSQLGuru
Indicium Resources, Inc.
kgboles a earthlink dt net
"Hassan" <hassan@.test.com> wrote in message
news:eJEWgIrRIHA.5136@.TK2MSFTNGP04.phx.gbl...
> We have a farm of servers that we replicate data to as a scale out farm.
> The only thing writing to it is from replication. Its like our read only
> farm.
> Is it better to go RAID 5 from a performance perspective as compared to
> RAID 10 ? We are not concerned about saving on space, but want to squeeze
> every ounce of performance from a well configured server.
> Thanks|||If saving space is not an issue, I'd go with RAID10 and configure sufficient
number of spindles to get the required performance.
Linchi
"Hassan" wrote:
> We have a farm of servers that we replicate data to as a scale out farm. The
> only thing writing to it is from replication. Its like our read only farm.
> Is it better to go RAID 5 from a performance perspective as compared to RAID
> 10 ? We are not concerned about saving on space, but want to squeeze every
> ounce of performance from a well configured server.
> Thanks
>

RAID 5 better for read only databases than RAID 10 ?

We have a farm of servers that we replicate data to as a scale out farm. The
only thing writing to it is from replication. Its like our read only farm.
Is it better to go RAID 5 from a performance perspective as compared to RAID
10 ? We are not concerned about saving on space, but want to squeeze every
ounce of performance from a well configured server.
ThanksGeneral opinion says RAID5 provides redundancy and good read performance.
RAID1 is good for write performance + redundancy. RAID10 is good for mixed
environments (read\write).
However, the results of these tests may change according to your hardware.
So it would be the best if you could try this out in your own specific
environment. You decide which is the best one for you.
Ekrem nsoy
"Hassan" <hassan@.test.com> wrote in message
news:eJEWgIrRIHA.5136@.TK2MSFTNGP04.phx.gbl...
> We have a farm of servers that we replicate data to as a scale out farm.
> The only thing writing to it is from replication. Its like our read only
> farm.
> Is it better to go RAID 5 from a performance perspective as compared to
> RAID 10 ? We are not concerned about saving on space, but want to squeeze
> every ounce of performance from a well configured server.
> Thanks|||For the same number of disk spindles, RAID 5 should outperform RAID 10 for
read-only databases.
Kevin G. Boles
TheSQLGuru
Indicium Resources, Inc.
kgboles a earthlink dt net
"Hassan" <hassan@.test.com> wrote in message
news:eJEWgIrRIHA.5136@.TK2MSFTNGP04.phx.gbl...
> We have a farm of servers that we replicate data to as a scale out farm.
> The only thing writing to it is from replication. Its like our read only
> farm.
> Is it better to go RAID 5 from a performance perspective as compared to
> RAID 10 ? We are not concerned about saving on space, but want to squeeze
> every ounce of performance from a well configured server.
> Thanks|||If saving space is not an issue, I'd go with RAID10 and configure sufficient
number of spindles to get the required performance.
Linchi
"Hassan" wrote:

> We have a farm of servers that we replicate data to as a scale out farm. T
he
> only thing writing to it is from replication. Its like our read only farm.
> Is it better to go RAID 5 from a performance perspective as compared to RA
ID
> 10 ? We are not concerned about saving on space, but want to squeeze every
> ounce of performance from a well configured server.
> Thanks
>sql

Tuesday, March 20, 2012

quicker way of writing a LIKE query

Hi

I have two product tables in two different databases, both contain thousands of records. I have to write a query that suggests matches on similar codes, and have come up with:

SELECT TB1.product, TB2.product
FROM TB1
JOIN (select distinct product
from db2.dbo.TB2) as TB2 --this table has PK of product and warehouse
ON TB2.product LIKE '%' + TB1.product+'%'

which DOES work, but because the table have many rows,takes time to do it... is there a way of rewritting this query, so it gives a faster result?

Thanks in advance...The problem that I see is that you are using a definition that requires a table scan for TB2 in order to determine row-by-row if the value of TB1.product exists anywhere in TB2.product.

This type of search is an ugly process to implement using just a set based language like SQL. This kind of problem is why Full Text Search (http://msdn2.microsoft.com/en-us/library/ms142571.aspx) was added to MS-SQL. Beware, in that Full Text Search is definitely NOT a "free lunch", there is definitely an overhead cost associated with it.

There are other ways to speed up the process, but none of them are very pretty. My first thought is to evaluate the cost/benefit of using Full Text Search, and only to pursue other answers if you decide not to use it and really need something else.

-PatP|||i would like to see the WHERE clause using full text, please

WHERE CONTAINS( ... ??

your guidance here, pat, will, as usual, be deeply appreciated|||I'd like to see some sample data, with further clarification on what he considers a partial match.|||who said partial match?

here's some sample data showing columns which match
TB1.product TB2.product
shampoo Kerastase Resistance Bain Volumactive Shampoo Volumizing
philosophy cinnamon buns shampoo, conditioner, & shower gel
H2O Plus Sea Marine Revitalizing Shampoo
shaving Proraso Eucalyptus & Menthol Shaving Cream 150 ml.
The Art of Shaving Unscented Pre-Shave Oil
Tweezerman Badger Hair Shaving Brush|||who said partial match?LIKE implies partial matches, whether he wishes or not. And where did you get his data, or did I miss a smiley somewhere?|||And where did you get his datai made it up

his first post said that his query works

this data fits that query

are you smiley-deprived? here, have a few: :) ;) :blush: :rolleyes:|||Thanks. I needed those.|||i would like to see the WHERE clause using full text, pleaseI know... As you are fond of reminding me, you are so NOT a DBA. This one falls outside of the scope of solutions in which you like to play, it is one of those tasks where you just get the job done and move on with life.

The CONTAINS function doesn't work the way you are implying, I don't know of a completely set-based solution for this kind of problem. I would retrieve the rows from the smaller table to a client (such as VBA within a DTS package), and build a temp table of the matches or partial matches so that I could return that. You could also do it with a cursor and dynamic SQL, but that strikes me as even uglier. This is ugly, but it will perform better than the "brute force" of the LIKE approach.

-PatP|||hey

Thanks for all the replies...

I had advanced a wee bit...
Basically I have been able to cut down the amount of rows in TB1 on some factors, and dumped it into a temp table...

I will look into the Full Text Search : )

Thanks again

Saturday, February 25, 2012

Quick and Easy SQL Question . . .

I don't have much experience with writing Sql, which is why i'm not sure.

But basically, I have a MSSQL 2005 Database that a person can list a property with,

and if the property listing expires by date, the user can relist the listing.

The records on the search function are automatically sorted by date, and I want the relisted records to update the datecreated.

I added "DateCreated" to the relisting storedprocedure to change DateCreated to todays date, but I am guessing.

Specials = @.Specials,

ExpirationDate = @.ExpirationDate ,

DateCreated =GetDate(),

DateApproved = @.DateApproved,

This seems to work, but I just wanted to check to make sure it's in the proper format.

Thank you

Daniel Meis

darkknight187:

DateCreated =GetDate()

That is the right way to do it.

|||

Thank you for your reply, just wanted to be sure.