Query table 1 for a value and use that value to delete a row from another table

  • I am new to creating scripts to run on SQL. My goal is to create a script that allows me to query one table for an "id" value and then run a delete statement on another table to delete a row.

    These are the SQL commands I run manually and I would like to automate this so I don't have to get into SQL MGMT Studio each time.

    select id from*user where*uName='johndoe'

    Output

    09123a6b

    Delete from othertable where uid='09123a6b'

    However.....I want to script this so I can run it like a batch file with a variable. Is this possible? Where do I start?

    I would like something like this....this is just an example of how I think it should work.

    c:\deleteuser.bat johndoe

  • Use joins http://msdn.microsoft.com/en-us/library/ms189835.aspx section "C. Using joins and subqueries to data in one table to delete rows in another table"

    delete from othertable

    from othertable o join user u on u.uid=o.uid where u.Name='johndoe';

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply