我有2个表机组和机器。
计算机组具有值:
MachinegroupID
MachineGroupName
MAchineGroupDesc
And Machines有价值观:
MachineGroupID(FK)
机号
MachineName Machinedesc
现在我想删除am machinegroup但是它给了我一个错误,因为它已经存在值。
所以我想删除那些没有机器的值,并在特定机器组中预设机器时给出错误消息。
我尝试使用查询,但它不起作用..
System.Data.SqlClient.SqlConnection dataConnection = new SqlConnection();
dataConnection.ConnectionString =
@"Data Source=JAGMIT-PC\SQLEXPRESS;Initial Catalog=SumooHAgentDB;Integrated Security=True";
System.Data.SqlClient.SqlCommand dataCommand = new SqlCommand();
dataCommand.Connection = dataConnection;
long MachineGroupID = Convert.ToInt64(Request.QueryString["node"]);
//tell the compiler and database that we're using parameters (thus the @first, @last, @nick)
**dataCommand.CommandText = ("Delete from [MachineGroups] where [MachineGroupID]=@MachineGroupID not in ( select distinct MachineGroupId from Machines )");**
//add our parameters to our command object
dataCommand.Parameters.AddWithValue("@MachineGroupName", MachineGroupName);
dataCommand.Parameters.AddWithValue("@MachineGroupDesc", MachineGroupDesc);
dataCommand.Parameters.AddWithValue("@TimeAdded", TimeAdded);
dataCommand.Parameters.AddWithValue("@MachineGroupID", MachineGroupID);
dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close();
这里我试图删除一个特定的机器组......
如果还有其他方法,请建议。
答案 0 :(得分:3)
Delete from MachineGroups
where MachineGroupId not in
(select distinct MachineGroupId from Machines);
答案 1 :(得分:0)
我没有尝试过这部分,但它应该给你一个想法
Delete from MachineGroups
WHERE NOT EXISTS ( SELECT TOP 1 MachineID FROM Machines
WHERE Machines.MachineGroupID= MachineGroups.MachineGroupID )