stop execution of a recursive loop on some validation [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am having a recursive loop of a stored procedure and i want to stop the execution of that recursive loop on a validation and return an empty table.
How can i do that?
This is some sample code :
alter procedure Searching
(parameters)
begin
do something
if(validation)
--exit here
else
exec Searching(parameters);
end
and i have another question of how to declare a global variable in sql? which should not lose it's scope from one stored procedure to another.

T-SQL is a really poor choice for recursion. It won't allow more than 32 levels of nesting, see ##NESTLEVEL:
When the maximum of 32 is exceeded, the transaction is terminated.
As always, the answer is to think in sets, use a SELECT instead of a stored procedure. Read Recursive Queries Using Common Table Expressions.
As to why your example doesn't work: you obviously made a mistake in the code which you did not post.

you must break down the condition of the recursion so that it don't continue after you return. you can make use off an integer that is decremented after each loop when it is negative u can stop execution.

Related

Declaration of variables improve or decrease performance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
So I am trying to improve my SQL, one topic that arose, does declaration help with performance or not so like for instance lets say you want to do a if else statement but you need to know if the count is higher than 0 for example right
SELECT #COUNTER = COUNT(ID) FROM tblSome WHERE NUMBER > TOTAL
IF(#COUNTER > 0)
OR would it be better than something like this
IF((SELECT #COUNTER = COUNT(ID) FROM tblSome WHERE NUMBER > TOTAL)>0)
I am just trying to minimize the time it takes, but also it would be nice to know
For now I cannot really find a difference with the small amounts of data I am using and I am not sure how to test it further or go test it to the next level
Use of variables can help or hinder performance dependent on the exact circumstances. There isn't a single rule.
In this case the use of the separate variable assignment step can be harmful.
It gives the optimiser no choice but to count all the rows as it doesn't look ahead to how you use that variable in future statements.
using an IF (SELECT COUNT(*) ...) > 0 allows the optimiser to see that it is true as long as the count is >=1 and can sometimes be optimised to an IF EXISTS (semi join) and stop reading after a single row.
But you are better off just writing it as EXISTS anyway rather than relying on that.
(The discussion on this internet archived blog post has more about the circumstances where this optimisation might happen)

Is there valid statement that don’t return a recordset? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In the question Difference between a statement and a query in SQL, the accepted answer says:
A statement is any text that the database engine recognizes as a valid command.
and
A query is a statement that returns a recordset (possibly empty).
For example, I know that update and insert can return a recorset when they use a returning clause, so I guess they return an empty returnset in the other case. Also, according to the postgres documentation, the update command returns a “command tag”. But I don’t understand if the tag is attached to an empty recordset or if it’s really all that is returned.
My question is: Considering this list of SQL commands, how do I know which one does not a return a resultset (not even an empty one) and what are they returning instead exactly?
Generally accepted version is that
Query(ies) are select statement which may or may not return rows
but does not make any changes to database.
Statement(s) are instructions when executed and successful will make
changes in the database(after commit)

Fill with zero to complete a defined number in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to complete cards numbers in sql. I have the prefix =11111 and the number of the card which is variable, therefore it could be '25' or '2130' but at the end I must have 14 numbers. So I need to fill spaces with zeros.
I've read about 'LPAD' but I don't understand very well this method.
You could use lpad, but if you're starting with a number you could use a 9-digit format model instead, and concatenate that onto your prefix:
select '11111' || to_char(25, 'FM000000000') from dual;
11111000000025
The FM format modifier stops Oracle adding a space for a potential +/- sign indicator.
SQL Fiddle demo
Use the ZEROFILL attribute.
But your database should only be responsible for saving data and not changing it before saving.
The best way would be to send the zerofilled data to the database server.

Wrong Number of Arguments Used With Function in Query Expression [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have an SQL in Access 2010 that was written by someone else that gives a Wrong Number of Arguments error when I try and run it. It's supposed to filter a report from a search page.
SELECT Activity.[ProjNo], Activity.[Code], Activity.[Type], Activity.[ProjNoStatus],
Activity.[Preliminary], Activity.[Planner], Activity.[Designer],
Activity.[Officer], Activity.[Manager], Activity.[Staff], Activity.[Analyst],
Activity.[Manager], Activity.[DeptHead], Activity.[ContractNumber],
Activity.[InfoOfficer],Activity.[ProjNoDesigner]
FROM Activity
WHERE Activity.ProjNo=Index.ProjNo AND (((IIf([Forms]![SearchForm]![txtCode]="",
"*",[Activity].[Code]=[Forms]![SearchForm]![txtCode]))<>False)
AND ((IIf([Forms]![SearchForm]![txtType]="","*",[Activity].[ Type]="",
"*", [Activity].[Type]=[Forms]![SearchForm]![txtType]))<>False) AND
((IIf([Forms]![SearchForm]![txtProjNoStatus]="","*",
[Activity].[ProjNoStatus]<=[Forms]![SearchForm]![txtProjNoStatus]))<>False));
I'm not very experienced with SQL and, like I said, I didn't write this code (the person who did has long since retired) so any help would be great.
That query defines just one data source (table or query):
FROM Activity
But then the WHERE clause appears to reference another data source named Index:
WHERE Activity.ProjNo=Index.ProjNo
Since Index is not included in the FROM clause, Access will object when you try to use it in the WHERE clause.
However, I'm not sure that is the cause of the first error Access complains about. It may help to show us the full text of that error message.

Replaces Nulls with 0 without using a function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to replace null values in a table but without using a function such as isnull because its dealing with a large amount of data and slowing it down.
everywhere online says isnull and coalesce but is there any way without using such functions.
I need this because the query
OPENING_OTHER + OPENING_FEE + OPENING_INT AS TOTAL_BALANCE
If one value is NULL then the total balance is always null
Cheers
No, how can you do something without doing anything?
You could permanenetly replace the NULL values with 0 but that would waste a lot of storage.
Transforming your data in a SELECT statement is not terribley costly if you use built in functions designed for that purpose.
The use of coalesce will be the quickest, most effiecient and expediant way to do this.
coalesce(OPENING_OTHER, 0) + coalesce(OPENING_FEE, 0) +
coalesce(OPENING_INT, 0) AS TOTAL_BALANCE
In fact, I'd suggest the actual cost of coalesce is so small that its hard to measure.