By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

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

P: 1
All,

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.
Expand|Select|Wrap|Line Numbers
  1. select id
  2. from user
  3. where uName='johndoe'
Output
09123a6b
Expand|Select|Wrap|Line Numbers
  1. Delete
  2. from othertable
  3. 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
Jan 12 '13 #1
Share this Question
Share on Google+
2 Replies


NeoPa
Expert Mod 15k+
P: 31,299
The following SQL should work for you :
Expand|Select|Wrap|Line Numbers
  1. DELETE
  2. FROM   [OtherTable]
  3. WHERE  ([UID]=(SELECT [ID]
  4.                FROM   [User]
  5.                WHERE  ([uName]='John Doe')))
Jan 12 '13 #2

Rabbit
Expert Mod 10K+
P: 12,334
As far as automating it, you can use the sqlcmd utility.
Jan 13 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.