如果查询具有某个值,则更新表

时间:2016-09-08 10:02:02

标签: c# asp.net oracle

我有一个查询,它给了我一个值,低于

protected void CmdUpdate_Click(object sender, EventArgs e)
{
    string query = "select mkey from xxacl_pN_LEASES_ALL where project_id = '" + 
                        ddlProject.SelectedValue + 
                        "' and " +
                        "building_id = '" +
                        ddlBuilding.SelectedValue + 
                        "' and SALES_USER_ID = '" + 
                        ddlSalesUser.SelectedValue + 
                        "'";
}

现在我想要的是,如果查询有一些值,那么写一下我知道的update....语句。如果它没有,那就不要更新表格。

我想知道如何检查查询是否有值的条件?

更新

我的更新声明

Update xxacl_pn_leases_all set ASSIGNED_TO = '' where mkey = query

5 个答案:

答案 0 :(得分:3)

Update xxacl_pn_leases_all set ASSIGNED_TO = '' where your_all_condition_here

e.g

Update xxacl_pn_leases_all set ASSIGNED_TO = '' where project_id = '" + ddlProject.SelectedValue + "' and "+
                    "building_id = '" + ddlBuilding.SelectedValue + "' and SALES_USER_ID = '" + ddlSalesUser.SelectedValue + "'";

以上查询将更新满足您条件的行。

  

N.B:内联查询不适合练习,请尝试使用参数化查询。

答案 1 :(得分:0)

根据OP的问题编辑进行编辑。

方法1:前端

protected void CmdUpdate_Click(object sender, EventArgs e)
{
    string query = "select mkey from xxacl_pN_LEASES_ALL where project_id = '" + ddlProject.SelectedValue + "' and "+
                "building_id = '" + ddlBuilding.SelectedValue + "' and SALES_USER_ID = '" + ddlSalesUser.SelectedValue + "'";
    SqlConnection con = new SqlConnection(ConStr);
    con.Open();
    SqlCommnad cmd = new SqlCommand(query, con);
    int mkey = Convert.ToInt32(cmd.ExecuteScalar()); //if mkey is numeric
    if (mkey > 0) 
    {
        //Update execution
        query = "Update xxacl_pn_leases_all set ASSIGNED_TO = '' where mkey = mkey";
        SqlCommnad cmd1 = new SqlCommand(query, con);
        cmd1.ExecuteNonQuery();
    }
    cmd.Dispose();
    cmd1.Dispose();
    con.Close();
}

方法2:后端

UPDATE xxacl_pn_leases_all 
   SET ASSIGNED_TO = '' 
   WHERE mkey IN (SELECT mkey FROM xxacl_pN_LEASES_ALL 
                   WHERE project_id = @project_id 
                   AND building_id = @building_id
                   AND SALES_USER_ID = @SALES_USER_ID)

只需通过前端代码执行上述查询即可。

答案 2 :(得分:0)

您可以使用-(IBAction)iCloudDriveFullFolder:(id)sender { UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.data"] inMode:UIDocumentPickerModeImport]; documentPicker.delegate = self; documentPicker.modalPresentationStyle = UIModalPresentationFormSheet; [self presentViewController:documentPicker animated:YES completion:nil]; } #pragma mark - iCloud files - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url { if (controller.documentPickerMode == UIDocumentPickerModeImport) { NSString *alertMessage = [NSString stringWithFormat:@"Successfully imported %@", [url lastPathComponent]]; NSLog(@"alertMessage %@",alertMessage); } 使用IF EXISTS.

非常重要
SQL Parameters

答案 3 :(得分:0)

我会使用where exists (select 1 from xxacl_pN_LEASES_ALL where --your query here) 因此看起来像这样的kike:

Update xxacl_pn_leases_all a set ASSIGNED_TO = '' where exists (select 1 from from xxacl_pN_LEASES_ALL b where a.id=b.id and project_id ....)

检查此documentation

答案 4 :(得分:0)

            string _QueryResult = "your query result";

            if (String.IsNullOrEmpty(_QueryResult))
            {
                //Query has no value

            }
            else
            {
                //Query has some value.

            }