SQL: Struggling with DELETES

I need to delete records in a table that are based on joins
of other tables.
Getting the data via joins is easy, but I'm struggling to
figure out how to
delete items from one table based on a join from multiple
tables.
Heres one example:
I have two tables, items and categories.
To get the rows from items, I can use a join:
SELECT *
FROM items INNER JOIN
categories ON items.categoryID =
categories.categoryID
WHERE (items.categoryID = 9) OR
(categories.categoryParentID = 9)
Now, what if I want to delete the records from items, but not
delete any
records from categories?
Can I just use?:
DELETE FROM items INNER JOIN
categories ON items.categoryID =
categories.categoryID
WHERE (items.categoryID = 9) OR
(categories.categoryParentID = 9)
-Darrel

Lionstone:
Have time to help me with another one? ;o)
So, the next challenge is pretty much the same thing, but
with a
double-join. The query I use to get the data is such:
SELECT * FROM WeFAQsVariations INNER JOIN
WeFAQs faqs ON vars.collectionID = faqs.collectionID
INNER JOIN
We_about_categories cats ON faqs.categoryID =
cats.categoryID
WHERE (faqs.categoryID = 9) OR (cats.categoryParentID = 9)
I want to delete the records from WeFAQsVariations where the
collectionID
matches the colletionID of the record in the FAQs table where
the categoryID
is or is a child of '9'
I didn't get very far on this one, though, as the simple
delete command:
DELETE FROM WeFAQsVariations
WHERE collectionID = ?
won't work, as I'm going to be returning a bunch of different
collectionIDs.
I think I need the EXISTS condition:
DELETE FROM WeFAQsVariations
WHERE EXISTS (
SELECT * FROM WeFAQs faqs INNER JOIN
We_about_categories cats ON faqs.categoryID =
cats.categoryID
WHERE (vars.collectionID = faqs.collectionID) AND
(faqs.categoryID = 9 OR
cats.categoryParentID = 9)
Did I come close?
_Darrel

Similar Messages

  • SQL report with delete capability

    I want to create a SQL report with the capability of delete rows (show checkboxes next to each row). Is this possible?

    You can create a page type of Tabular Form, remove all buttons but delete, change all the edit boxes to display as text (saves state) and use that.

  • Problems trying to insert rows from PL/SQL procedure with existing seq..

    hello. please help me.
    i have a button that is calling an anonymous PL/SQL block and i am struggling with inserting records automatically when this button is clicked. an ordinary before insert DB trigger is launched:
    reate or replace TRIGGER "SPECSYS"."BI_SPEC_INSTRUCTS"
    before insert on "SPEC_INSTRUCTS"
    for each row
    begin
    if :NEW."ID" is null then
    select "SPEC_INSTRUCTS_SEQ".nextval into :NEW."ID" from dual;
    end if;
    end;
    in my PL/SQL I am trying to create a record automatically. stupid question that this undoubtedly is, how do i do this? if i leave the sequence column = null surely the trigger should take care of inserting the sequence into this column automatically?
    i have tried making the sequence column = null, leaving it out altogether (ie specifying all colums except this one on the insert statement) but with no success!

    this is not the problem!

  • Hi there. I hoping for help.   I am struggling with both...

    hi there. I hoping for help.
    I am struggling with both my WRT54G router set-up and i can't seem to use your online chat system. I hope that you can help.
    I have just moved from Hong Kong to New Zealand and I'm trying to install my WRT54G, bought in Hong Kong, to connect to a Telstra braodband internet connection, but when I follow the instruction CD-ROM I get to the point (number 6 - although this point is numbered 7 in the instructions?!?) when I have connected the router it then says it is checking my settings and then it asks me to put in a password. It then checks my system again and then asks me for the password again, and this repeatedly. I can't get past this point. Because this was happening I pushed the reset button on the back of the router, but it still asks me for a password (I have tried using admin as the password without success) and then goes through the check and asks me for the password again. Any help would be great.
    Thanks.
    Michael Anthony

    What windows OS are you running? It may have not installed correctly.
    First go to control panel in start menu, then add/remove programs. it should be in there.
    If you do not see it there then go to programs files and see if it is there. if so you can delete the folder it is in, then run disk clean up.
    By manually setting up did this work? would like to know the end results.

  • Execute SQL Task with Parameter - from DB2 to SQL Server

    I am pulling data from DB2 to SQL Server.
    1st Execute SQL task runs the following against DB2:
    SELECT TIMESTAMP(CHAR(CURRENT_DATE - (DAY(CURRENT_DATE)-1) DAYS - 1 MONTH)) as START_MONTH
    FROM SYSIBM.SYSDUMMY1;
    I'm storing it as a Result Set.
    Next I have a Data Flow Task.  This pulls data from DB2 where the date matches the parameter.
    FROM SCHEMA.TABLE t
    WHERE DATE_TIME_COLUMN= ?
    This works fine. Guessing, because the parameter source is DB2 and the Data Flow source is DB2.
    The problem is, I want to remove existing data for the same date in the SQL table.  IE, if I'm pulling March 2014 data from DB2, I want to be sure there is no March 2014 data in the SQL table.  The main use is for re-runs.
    So, I added another Execute SQL task after the first one that assigns the variable, and before the Data Flow Task. This runs the following statement against SQL Server:
    DELETE FROM
    database.dbo.table
    WHERE DATE_TIME_FIELD >= ?
    The package fails at the new Execute SQL Task with the following error message:
    Error: 0xC002F210 at Execute SQL Task, Execute SQL Task: Executing the query "DELETE FROM
    SBO.dbo.tblHE_MSP_FEE_BUCKETS
    WHERE T..." failed with the following error: "Parameter name is unrecognized.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established
    correctly.
    Task failed: Execute SQL Task
    SSIS package "Load_MSP_Fee_Buckets_SQL.dtsx" finished: Success.
    The program '[14240] Load_MSP_Fee_Buckets_SQL.dtsx: DTS' has exited with code 0 (0x0).
    I am assuming this is something to do with the Parameter source being DB2, and trying to use against SQL?
    Any suggestions on how to make this work??
    Thanks,
    -Al H

    Parameter name is unrecognized
    is the key, how come DB2 is related if it runs against SQL Server
    What I think is happening you do not use the OLEDB connection to SQL Server.
    Likewise, if it is ADO then the query needs to be
    DELETE FROM
    database.dbo.table
    WHERE DATE_TIME_FIELD >= @MyDate
    Arthur My Blog

  • How could I replace hard coded value in my sql query with constant value?

    Hi all,
    Could anyone help me how to replace hardcoded value in my sql query with constant value that might be pre defined .
    PROCEDURE class_by_day_get_bin_data
         in_report_parameter_id   IN   NUMBER,
         in_site_id               IN   NUMBER,
         in_start_date_time       IN   TIMESTAMP,
         in_end_date_time         IN   TIMESTAMP,
         in_report_level_min      IN   NUMBER,
         in_report_level_max      IN   NUMBER
    IS
      bin_period_length   NUMBER(6,0); 
    BEGIN
      SELECT MAX(period_length)
         INTO bin_period_length
        FROM bin_data
         JOIN site_to_data_source_lane_v
           ON bin_data.data_source_id = site_to_data_source_lane_v.data_source_id
         JOIN bin_types
           ON bin_types.bin_type = bin_data.bin_type 
       WHERE site_to_data_source_lane_v.site_id = in_site_id
         AND bin_data.start_date_time     >= in_start_date_time - numtodsinterval(1, 'DAY')
         AND bin_data.start_date_time     <  in_end_date_time   + numtodsinterval(1, 'DAY')
         AND bin_data.bin_type            =  2
         AND bin_data.period_length       <= 60;
      --Clear the edr_class_by_day_bin_data temporary table and populate it with the data for the requested
      --report.
      DELETE FROM edr_class_by_day_bin_data;
       SELECT site_to_data_source_lane_v.site_id,
             site_to_data_source_lane_v.site_lane_id,
             site_to_data_source_lane_v.site_direction_id,
             site_to_data_source_lane_v.site_direction_name,
             bin_data_set.start_date_time,
             bin_data_set.end_date_time,
             bin_data_value.bin_id,
             bin_data_value.bin_value
        FROM bin_data
        JOIN bin_data_set
          ON bin_data.bin_serial = bin_data_set.bin_serial
        JOIN bin_data_value
          ON bin_data_set.bin_data_set_serial = bin_data_value.bin_data_set_serial
        JOIN site_to_data_source_lane_v
             ON bin_data.data_source_id = site_to_data_source_lane_v.data_source_id
            AND bin_data_set.lane = site_to_data_source_lane_v.data_source_lane_id
        JOIN (
               SELECT CAST(report_parameter_value AS NUMBER) lane_id
                 FROM report_parameters
                WHERE report_parameters.report_parameter_id    = in_report_parameter_id
                  AND report_parameters.report_parameter_group = 'LANE'
                  AND report_parameters.report_parameter_name  = 'LANE'
             ) report_lanes
          ON site_to_data_source_lane_v.site_lane_id = report_lanes.lane_id
        JOIN (
               SELECT CAST(report_parameter_value AS NUMBER) class_id
                 FROM report_parameters
                WHERE report_parameters.report_parameter_id    = in_report_parameter_id
                  AND report_parameters.report_parameter_group = 'CLASS'
                  AND report_parameters.report_parameter_name  = 'CLASS'
             ) report_classes
          ON bin_data_value.bin_id = report_classes.class_id
        JOIN edr_rpt_tmp_inclusion_table
          ON TRUNC(bin_data_set.start_date_time) = TRUNC(edr_rpt_tmp_inclusion_table.date_time)
       WHERE site_to_data_source_lane_v.site_id = in_site_id
         AND bin_data.start_date_time     >= in_start_date_time - numtodsinterval(1, 'DAY')
         AND bin_data.start_date_time     <  in_end_date_time   + numtodsinterval(1, 'DAY')
         AND bin_data_set.start_date_time >= in_start_date_time
         AND bin_data_set.start_date_time <  in_end_date_time
         AND bin_data.bin_type            =  2
         AND bin_data.period_length       =  bin_period_length;
    END class_by_day_get_bin_data;In the above code I'm using the hard coded value 2 for bin type
    bin_data.bin_type            =  2But I dont want any hard coded number or string in the query.
    How could I replace it?
    I defined conatant value like below inside my package body where the actual procedure comes.But I'm not sure whether I have to declare it inside package body or inside the procedure.
    bin_type     CONSTANT NUMBER := 2;But it does't look for this value. So I'm not able to get desired value for the report .
    Thanks.
    Edited by: user10641405 on May 29, 2009 1:38 PM

    Declare the constant inside the procedure.
    PROCEDURE class_by_day_get_bin_data(in_report_parameter_id IN NUMBER,
                                        in_site_id             IN NUMBER,
                                        in_start_date_time     IN TIMESTAMP,
                                        in_end_date_time       IN TIMESTAMP,
                                        in_report_level_min    IN NUMBER,
                                        in_report_level_max    IN NUMBER) IS
      bin_period_length NUMBER(6, 0);
      v_bin_type     CONSTANT NUMBER := 2;
    BEGIN
      SELECT MAX(period_length)
        INTO bin_period_length
        FROM bin_data
        JOIN site_to_data_source_lane_v ON bin_data.data_source_id =
                                           site_to_data_source_lane_v.data_source_id
        JOIN bin_types ON bin_types.bin_type = bin_data.bin_type
       WHERE site_to_data_source_lane_v.site_id = in_site_id
         AND bin_data.start_date_time >=
             in_start_date_time - numtodsinterval(1, 'DAY')
         AND bin_data.start_date_time <
             in_end_date_time + numtodsinterval(1, 'DAY')
         AND bin_data.bin_type = v_bin_type
         AND bin_data.period_length <= 60;
      --Clear the edr_class_by_day_bin_data temporary table and populate it with the data for the requested
      --report.
      DELETE FROM edr_class_by_day_bin_data;
      INSERT INTO edr_class_by_day_bin_data
        (site_id,
         site_lane_id,
         site_direction_id,
         site_direction_name,
         bin_start_date_time,
         bin_end_date_time,
         bin_id,
         bin_value)
        SELECT site_to_data_source_lane_v.site_id,
               site_to_data_source_lane_v.site_lane_id,
               site_to_data_source_lane_v.site_direction_id,
               site_to_data_source_lane_v.site_direction_name,
               bin_data_set.start_date_time,
               bin_data_set.end_date_time,
               bin_data_value.bin_id,
               bin_data_value.bin_value
          FROM bin_data
          JOIN bin_data_set ON bin_data.bin_serial = bin_data_set.bin_serial
          JOIN bin_data_value ON bin_data_set.bin_data_set_serial =
                                 bin_data_value.bin_data_set_serial
          JOIN site_to_data_source_lane_v ON bin_data.data_source_id =
                                             site_to_data_source_lane_v.data_source_id
                                         AND bin_data_set.lane =
                                             site_to_data_source_lane_v.data_source_lane_id
          JOIN (SELECT CAST(report_parameter_value AS NUMBER) lane_id
                  FROM report_parameters
                 WHERE report_parameters.report_parameter_id =
                       in_report_parameter_id
                   AND report_parameters.report_parameter_group = 'LANE'
                   AND report_parameters.report_parameter_name = 'LANE') report_lanes ON site_to_data_source_lane_v.site_lane_id =
                                                                                         report_lanes.lane_id
          JOIN (SELECT CAST(report_parameter_value AS NUMBER) class_id
                  FROM report_parameters
                 WHERE report_parameters.report_parameter_id =
                       in_report_parameter_id
                   AND report_parameters.report_parameter_group = 'CLASS'
                   AND report_parameters.report_parameter_name = 'CLASS') report_classes ON bin_data_value.bin_id =
                                                                                            report_classes.class_id
          JOIN edr_rpt_tmp_inclusion_table ON TRUNC(bin_data_set.start_date_time) =
                                              TRUNC(edr_rpt_tmp_inclusion_table.date_time)
         WHERE site_to_data_source_lane_v.site_id = in_site_id
           AND bin_data.start_date_time >=
               in_start_date_time - numtodsinterval(1, 'DAY')
           AND bin_data.start_date_time <
               in_end_date_time + numtodsinterval(1, 'DAY')
           AND bin_data_set.start_date_time >= in_start_date_time
           AND bin_data_set.start_date_time < in_end_date_time
           AND bin_data.bin_type = v_bin_type
           AND bin_data.period_length = bin_period_length;
    END class_by_day_get_bin_data;

  • How to connect sql database with Flash

    Dear Friends,
    Iam working on a quessinarie (assesment) for elearning software. i want to connect sql database with flash and save my records in back end like add, delete, modification of student result and details.
    Kindly help me to connect the sql database. or dot net server. I know how to connect PHP and mysql. But my client dont want php. only sql database and do the add, del, modificaiton.
    Thanks in advance,
    Syed Abdul Rahim

    unfortunately no, i do not know ASP well enough to give any code advice, other than the basic concept:  make a request on a server-side script (such as ASP) via URLLoader - the script receives variable values (if required), runs the DB queries you wish to invoke, and 'returns' the results - pick them up in the URLVariables class via the event handler in Flash and assign them to local properties if needed, etc.
    there are quite a few threads that you could find more specific information on the ASP side, here and elsewhere around the net.

  • I'm sorry to bother you guys, but this is all new to me! i'm wanting to place my face on another persons photo as a gag. i've been struggling with this all day! could someone explain to me how this is done, in language i can understand???  thanks in advan

    I'm really sorry to bother you guys, but this is all new to me! i'm trying to put my face on another persons photo as a gag! i have been struggling with this all day! can anyone explain to me how  this is done in language i can understand???  thanks in advance!

    What software do you own?  Give details, please.
    Photoshop is a professional level application that makes no apologies for its very long and steep learning curve.
    It's not something you can learn in a forum, question by question.  Teaching someone Photoshop from scratch exceeds the scope of this or any other forum.
    The process involves isolating your face, by selecting it and deleting the unwanted parts around your face or with layer masks, then placing it as a new layer on top of your target image, making sure it's the right size, color, lighting, etc.  Google for tutorials on any of those elements you are unfamiliar with,

  • Count total number of record of table with deletion from archiving object

    Does anyone know is there any SAP standard program to count the total number of record of the table with deletion from Archiving Object and display in repprt?

    Not sure of the question. Are you looking to get the sql "select count(*) from table" from using the TopLink expression framework or are you getting that SQL already and want something else?
    If you are looking just to get the count from a table/class, you can use a ReportQuery:
    ReportQuery rquery = new ReportQuery(ClassToQueryOn.class);
    rquery.addCount(); //equivalent to count(*);
    session.executeQuery(rquery);
    You can use a report query to return data instead of objects, and use selection criteria just like a normal read query.
    Best Regards,
    Chris

  • SQL database with CDR

    Here is my problem. I get a database error with I try to use the phone services on our 7960 phones. I've been able to replicate this problem at my desk but now even if I delete the user profile and phone from CCM and create a new profile with a different extension number, I still have the same missed/received calls. That leads me to think that the phone itself is tied to the SQL database. If that is the case, would there be a way to go into SQL and just delete this phone?

    Actually, I've resolved the problem. The issue was that our SQL database wasn't being updated. We are using a 3rd party application for Long Distance calls and for some reason the link between SQL and CCM was broken and the script wasn't running. This is why when we created new extensions in CCM, they were not implimented in the SQL database and we had a Database error under the services on the phones. But thanks for the info.

  • [Excel] Running a SQL Query to delete rows

    Hello Experts,
    Background: I am attempting to use a dba of my companies time keeping system and implement it with Power BI tools. Given the file size restrictions within Power Bi itself I need to lower my file size by removing all time logs from
    inactive employees.
    I have a question regarding whether or not you can use a sql query to delete rows in excel. I have roughly 200,000+ rows in my excel spreadsheet. I am attempting to delete all rows where an employee equals inactive. I have attempted to
    delete these rows by sorting them and doing a bulk delete and clear contents, but it seems to crash my excel every time.  My thought process is that using a query that does a timed delete might put less of a burden on deleting the massive amount of data.
    I would like to use this: DELETE * FROM [Table_(...)_Actual$] WHERE [Current] = "Inactive" (Will add more once I know it is possible to use sql queries in Excel.
    Any information on whether or not this is possible would be appreciated.
    Regards,
    Link

    Running SQL Query in Excel is possible, however, the delete query is not supported in Excel.
    You are more restricted in deleting Excel data than   data from a relational data source. In a relational database, "row" has no   meaning or existence apart from "record"; in an Excel worksheet, this is not   true. You can delete values
    in fields (cells). Please see:http://support.microsoft.com/kb/257819/en-us
    One workaround : Use update query to set the rows as null, then use select query.
    e.g. 
    SQL = "update [sheet2$A1:B5] set name=null,age=null where name='andy'"
    cnn.Execute SQL
    SQL = "select name,age from [sheet2$A1:B5] where name is not null"
    Wind Zhang
    TechNet Community Support

  • SQL Query to Delete SAP* user DB level

    Hi Everyone,
                   please tell me the SQL Querry to delete a User SAP* from the Table. so that, i can login SAP* using default password PASSS. Because, my SAP*  user is locked due 2 incorrect password. i dont have anyother User id in the Particular Client.
                   please help me out to resolve it.
    Thanks&Regards,
    Siva

    Please use with schema SAPR3 or SAPSR3 as version, like this:
    SQL> select UFLAG from SAPR3.USR02 where MANDT=000 and BNAME='DDIC';
         UFLAG
             0
    SQL>
    so include table name with schema in below command
    Delete from usr02 where bname = 'SAP*' and mandt = 'XXX';
    thanks
    Bhudev

  • OS takes up over 3 GB of space. Can I minimize this as I am struggling with constantly managing the sync which is making me think the droid alternative with iTunes app is the better approach.

    I am restricted to a 16 GB phone and am constantly struggling with space when I sync or update. I noticed today that the OS takes up over 3 GB of space as the capacity is listed at 12.68 GB. Any suggestions on this other than get an android and use the iTunes app instead? Frustrated.

    No, the OS doesn't take up over 3GB of space.
    The difference between 16GB and 12.68GB is primarily attributable to the difference between decimal math (used by every computer and component manufacturer on Earth for stating capacity in customer facing documentation) and binary math, used by processors and operating systems. This has been an issue since the 1970's.
    Think more about managing the content and apps you actually keep on the phone. If you haven't used an app in months, you probably don't need it. Don't let your phone get cruffed up with games and apps you don't use. You also probably don't need to keep your entire media library on it for when you're on the go.
    Extract photos from the phone to your computer on a regular basis then delete them from the phone as you would from any other digital camera. Sync the photos you actually want with you to the phone. iCloud Photo Library may give some relief from this once it's out of beta.

  • Struggling with one iTunes 10 Library and two kids

    I'm struggling with iTunes 10 and the music Library.  My two kids both have ipod touches, and each wants a different subset of whats in the library.  Whats the best way to manage this when each kid wants to add/delete music? 
    Should each kid be running iTunes from their own profile?  But then could I keep the one libary or will I need seperate copies?   Could we instead make seperate playlists for each kid with their selection and somehow sync to those songs only?
    My iTunes library consisting of a large collection of mp3 files, which I'd rather not have multiple copies of on the same computer.  In the preferences > Advanced settings I've specified the media folder location path and selected the checkboxes to keep the folder organized and to copy media when added to the library.  In Preferences > Devices, I've selected to prevent automatic syncs and somewhere else I've selected to manually manage music selections.

    Jon,
    You should be able to do what you want. However, keep in mind these caveats, by design, the iphone will sync itunes content with ONE computer at a time. Thus, if you start syncing such content to your wife's phone from your account if you decide to switch to her account, all itunes content on her phone will first be erased & then replaced with the content from her account. You can sync as many devices as you want to the same itunes account, there is no limit. So yes, you can sync itunes content from your account to your wife's phone.
    First, disable auto sync, under preferences, in both your accounts.
    What you would do is make sure that there are no checks in any boxes regarding itunes content under your wife's login...the only thing you'll be syncing under her login is contacts & calendars. When she connects her phone under your login, make sure there are no checks in any boxes regarding contacts or calendars, the only thing you'll be syncing is itunes content.
    Since you can sync as many devices as you want to the same itunes account, itunes will keep everything straight by device. Thus, once you set things up, from that point on itunes will recognize what device is what & sync accordingly.

  • HT204266 My iPad (version 1, IOS 5.1) has quit connecting with the store. I am unable to update or buy any app. I did a reboot and a reset with deleting the data. I can not find anything in support for this problem. Any help will be appreciated.

    My iPad (version 1, IOS 5.1) has quit connecting with the store. I am unable to update or buy any app. I did a reboot and a reset with deleting the data. I can not find anything in support for this problem. Any help will be appreciated.

    My iPad (version 1, IOS 5.1) has quit connecting with the store. I am unable to update or buy any app. I did a reboot and a reset with deleting the data. I can not find anything in support for this problem. Any help will be appreciated.

Maybe you are looking for