473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Delete help

MC

i have table Controller (ControllerID, ControllerNAme, isActive)
and tables SendControllerForCitizenClaim (SendID, ControllerID, Region),
table SendControllerForCompanyClaim (SendID, ControllerID, REgion)..
and so on .. i have 14 tables of SendController each for a certain
purpose.

I have a procedure for deleting controllers. I want that if a controller
is sent (i.e its id is in at least one of the tables) then isActive=0
otherwise if the controller id isn't present in neither of the 14 tables
then delete controler record.

How do i do that wihout having to write 14 If Not exists statements??

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
4 1551
MC (an*******@discussions.microsoft.com) writes:
i have table Controller (ControllerID, ControllerNAme, isActive)
and tables SendControllerForCitizenClaim (SendID, ControllerID, Region),
table SendControllerForCompanyClaim (SendID, ControllerID, REgion)..
and so on .. i have 14 tables of SendController each for a certain
purpose.

I have a procedure for deleting controllers. I want that if a controller
is sent (i.e its id is in at least one of the tables) then isActive=0
otherwise if the controller id isn't present in neither of the 14 tables
then delete controler record.

How do i do that wihout having to write 14 If Not exists statements??


If you have foreign key constraints to the referring tables you can
do this:

DELETE Controller WHERE id = @id
IF @@error = 547
UPDATE Controller SET isactive = 0 WHERE Id = @id

However, there is no way to suppress the error message from the
constraint violation, so you will have to tame the client to swallow
them.

It is unfortunately so that 547 is a very generic message about
constraint violation, so it is not a good idea to have the client
to always drop 547 on the floor, since in many cases constraint
violations are due to bugs in the application.

As long as you are in SQL2000, I would go for the 14 NOT EXISTS clauses -
or always set isactive = 0. The next version of SQL Server which is
beta now, offers improved error handling.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
On 2004-08-05, MC wrote:
...
I have a procedure for deleting controllers. I want that if a controller
is sent (i.e its id is in at least one of the tables) then isActive=0
otherwise if the controller id isn't present in neither of the 14 tables
then delete controler record.

How do i do that wihout having to write 14 If Not exists statements??


You can change your procedure that inserts ControlersID into SendControlers
tables (if only you can) in way that also updates Controlers table setting isActive to 1.
Then you do not have a problem while trying to delete specified controler.
DELETE FROM Controlers WHERE Id = @id AND isActive = 0;

Trociu
--
Plain-text over all!!!
/**********/
write me : trociu@autonom ict pwr wroc pl
search me : gg: 1382729
Jul 20 '05 #3
MC <an*******@discussions.microsoft.com> wrote in message news:<41**********************@news.newsgroups.ws> ...
i have table Controller (ControllerID, ControllerNAme, isActive)
and tables SendControllerForCitizenClaim (SendID, ControllerID, Region),
table SendControllerForCompanyClaim (SendID, ControllerID, REgion)..
and so on .. i have 14 tables of SendController each for a certain
purpose.

I have a procedure for deleting controllers. I want that if a controller
is sent (i.e its id is in at least one of the tables) then isActive=0
otherwise if the controller id isn't present in neither of the 14 tables
then delete controler record.

How do i do that wihout having to write 14 If Not exists statements??

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


You can do it with a single NOT EXISTS

IF NOT EXISTS (
SELECT controllerID
FROM (
SELECT controllerID FROM tab1
UNION SELECT controllerID FROM tab2
UNION SELECT controllerID FROM tab3
) tab
WHERE controllerID = @controller_to_check
)
Jul 20 '05 #4
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications.
Ii have 14 tables of SendController each for a certain purpose. <<
That is a <verb><object> name, not an entity/table name -- appropriate
for a procedure but not a table. Have you read ISO-11179 or a book on
data modeling? And these 14 tables are completely different from a
logical view (that also means different in structure -- read Date and
Mcgovern)? You cannot have a "purpose_code" in one table?
I have a procedure for deleting controllers. I want that if a

controller is sent (i.e its id is in at least one of the tables) then
isActive=0 otherwise if the controller id isn't present in neither of
the 14 tables then delete controler record [sic] <<

Okay, you write code with assembly language style bit flags and don't
know that a row is not a record. I am now pretty sure, even without
DDL, that you have a serious design flaw.

It is a called splitting attributes; you have a single attribute and
instead of putting it into a column (columns are not fields, by the
way) you create an array of tables for each value. The result is
complex queries like this -- they are usually expensive queries and
lose data integrity.

Would you like to try again with specs? Ther are wasy to model a
class hierarchy in SQL, if that is what you are trying to do.
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
5
by: Mike | last post by:
I am not a sql person and could use some help with a delete...here is what I want: I have the following tables/fields (only including necessary fields) answers result_id results result_id
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
6
by: Paul T. Rong | last post by:
Dear all, Here is my problem: There is a table "products" in my access database, since some of the products are out of date and stopped manufacture, I would like to delete those PRODUCTS from...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
9
by: Dejan | last post by:
Hy, Sorry for my terreble english I have this simple code for deleting rows in mysql table... Everything works fine with it. So, what do i wanna do...: my sql table looks something like...
6
by: radishcarrot | last post by:
Hi, I am rather new to C++ and an invalid exception has occurs in my system which I have absolutely no idea as to why it happens. Please help! Thank you. I have created a method which takes in...
9
by: learning | last post by:
hi I am trying to make some simple app to learn exception safety coding. I know that new and delete can throw bad_alloc but how can I force them to throw by a flag "change" at run time? I am...
10
by: amiga500 | last post by:
Hello, I have one basic simple question. When I have multiple records in the datagrid as follows: Code Product 1 Product 2 Product 3 11111 A B C...
1
by: jmarcrum | last post by:
Hello all! i have a “monitor-type” program, where my program recognizes my defined "commands”, and executes the given command, or produces an error message that says “unrecognized command”. After...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.