简体   繁体   English

当具有多个具有相同ID的单元格时,如何仅删除所选行?

[英]How can I delete only the selected row, when having several cells with the same ID?

I was setting a stored procedure to delete a specific row based on the option that the user picks. 我正在设置存储过程以根据用户选择的选项删除特定行。 The stored procedure recived 2 id and check if they are in the table. 存储过程重新获得2个id并检查它们是否在表中。 But i can´t figure it out. 但我无法弄明白。 Can you help? 你能帮我吗?

My stored procedure looks like this 我的存储过程看起来像这样

@idcliente numeric,
@codmedia numeric
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON; 
    IF (EXISTS(SELECT COD_MEDIA FROM MEIOS_CLIENTES WHERE ID_CLIENTE = @idcliente AND COD_MEDIA = @codmedia))
    BEGIN
        DELETE FROM MEIOS_CLIENTES WHERE ID_CLIENTE = @idcliente AND COD_MEDIA = @codmedia
    END     
END

The table in the database looks like this 数据库中的表格如下所示

COD_SERVICO | ID_CLIENTE
     1            2
     26           2
     1            1
     32           1

The table has alot of records. 该表有很多记录。

Imagine if user picks to delete COD_SERVICO 1 from ID_CLIENTE 2. 想象一下,如果用户选择从ID_CLIENTE 2中删除COD_SERVICO 1。

EDIT: 编辑:

I'm doing the request of the action by jquery, the code looks like this: 我正在通过jquery执行操作的请求,代码如下所示:

function removeMeioCliente(idcliente ,idservico) {


    bootbox.dialog({
        message: "Irá eliminar o serviço selecionado. Deseja continuar?",
        title: "Alerta - Eliminação do Serviço",
        buttons: {

            danger: {
                label: "Cancelar",
                className: "btn-danger",
                callback: function () {
                }
            }, success: {
                label: "Continuar",
                className: "btn-success",
                callback: function () {

                    var options = {
                        url: baseurlrequest + 'cliente/deleteservico',
                        params: {
                            id2: idcliente,
                            id: idservico,
                        },
                        onsuccess: displaydeleteservico
                    }

                    $.fn.ajaxpostjson(options);

                }
            }
        }
    });
}


function displaydeleteservico(data) {
    if (data.status == 200) {
        toastr.info("Serviço eliminado com sucesso!", 'Cision Corpora');
    }

    if (data.status == 500) {
        toastr.error(data.html, 'Cision Corpora');
    }
}

In my view my table looks like this. 在我看来,我的表看起来像这样。 The item is to string because i was testing if some ID's was being passed, both are null. 该项目是字符串,因为我正在测试是否正在传递某些ID,两者都为空。

       <table class="table table-striped" cellspacing="0">
                            <thead>
                                <tr>
                                    <th class="th-sm">Meios do Cliente</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                            <tbody>

                                @foreach (var item in Model.Meios)
                                {
                                    <tr>
                                        <td>
                                            @Html.Raw(item.Item3)
                                        </td>
                                        <td onclick="removeMeioCliente(@item.Item1.ToString(),@item.Item2.ToString());"><i class="fa fa-times"></i></td>
                                    </tr>
                                }
                            </tbody>
                        </table>

Why are both ID's null? 为什么两个ID都为空?

EDIT 2: 编辑2:

I dit it! 我点了!

I will post the code if someone want's to know the solution. 如果有人想知道解决方案,我会发布代码。

In my view the code looks like this 在我看来,代码看起来像这样

 <td><a id="servicoidcliente" data-id="@item.Item1" onclick="removeMeioCliente(@item.Item2);"><i class="fa fa-times"></i></a></td>

And in Jquery added a simple line: 在Jquery中添加了一个简单的行:

var idcliente = $('#servicoidcliente').data("id");

Controller: 控制器:

public ActionResult DeleteServico(string id2,string id)
        {
            var statusreq = this.StatusOk;
            var html = "";    
            try
            {
               BusinessService.Cliente.RemoverServicoCliente(id2,id);
            }
            catch (Exception ex)
            {
                statusreq = this.StatusError;
                html = ex.Message;
            }
            return Json(new
            {
                id = id,
                id2 = id2
            },
            JsonRequestBehavior.AllowGet);
        }

This should work, what is your issue ? 这应该有效,你的问题是什么?

You could however omit the "if" part, If there is no match for your WHERE condition delete will do nothing, but not fail. 但是,您可以省略“if”部分,如果您的WHERE条件不匹配,则删除将不执行任何操作,但不会失败。

You can create a variable to accept user input and remove the selected column based on what the COD_SERVICO value is. 您可以创建一个变量来接受用户输入,并根据COD_SERVICO值删除所选列。

-- Create demonstration table
IF OBJECT_ID('tempdb.dbo.#_test') IS NOT NULL DROP TABLE #_test
CREATE TABLE #_test (
    [ID] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [COD_SERVICO] VARCHAR(25) NULL,
    [ID_CLIENTE] VARCHAR(25) NULL,
)
INSERT INTO #_test ([COD_SERVICO], [ID_CLIENTE])
VALUES ('1','2'),
        ('26','2'),
        ('45','1'),
        ('32','1')

Table values (with ID column): 表值(带ID列):

ID  COD_SERVICO ID_CLIENTE
1     1           2
2     26          2
3     45          1
4     32          1


-- Declare variable for input
DECLARE @_cod_servico_to_remove VARCHAR(25)

-- Set variable to whatever the user wants to remove.  This will impact the COD_SERVICO column
SET @_cod_servico_to_remove = '1'

-- Run your delee query (Transact-SQL)
DELETE FROM #_test
WHERE [COD_SERVICO] = @_cod_servico_to_remove

Result: 结果:

ID  COD_SERVICO ID_CLIENTE
2     26             2
3     45             1
4     32             1

EDIT: Here's a procedure that is largely the same as OP's, but considers missing values. 编辑:这是一个与OP大致相同的程序,但考虑缺失值。

--Clear any prior procedures
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'dbo.RemoveRecords') AND OBJECTPROPERTY(id,  N'IsProcedure') = 1) DROP PROCEDURE dbo.RemoveRecords;
GO

--Set two variables to null; Evaluate passed arguments inside the procedure.
CREATE PROCEDURE dbo.RemoveRecords
    @idcliente FLOAT = NULL,
    @codmedia FLOAT = NULL
AS
BEGIN

    SET NOCOUNT ON;
    SET XACT_ABORT ON;

    DECLARE @_sql NVARCHAR(MAX)

    IF @idcliente IS NOT NULL AND @codmedia IS NOT NULL
        BEGIN
            IF (SELECT COUNT(*) FROM MEIOS_CLIENTES WHERE CONVERT(FLOAT, ID_CLIENTE) = @idcliente AND CONVERT(FLOAT, COD_MEDIA) = @codmedia) > '0'
                BEGIN
                    DELETE FROM MEIOS_CLIENTES
                    WHERE CONVERT(FLOAT, [ID_CLIENTE]) = @idcliente
                    AND CONVERT(FLOAT, [COD_SERVICO]) = @codmedia
                END;
        END;

END;
GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当行 header 单击选择 datagridview 行时,如何停止输入单元格? - How can I stop cells from being entered when a datagridview row is selected by a row header click? 使用 Xamarin... 如何对同一行或多行的网格内的两个单元格求和? - With Xamarin ... How do I SUM two cells inside the grid for the same row or for several rows? 如何从数据库中删除选定的DataGridView行? - How can I delete a selected DataGridView row from the database? 我如何在datagrid wpf中删除所选行? - How I can Delete Selected Row in datagrid wpf? 如果选择了多个 datagridview 行,如何防止选择“新”datagridview 行中的单元格? - How can I prevent cells in the “new” datagridview row from being selected if there are multiple datagridview rows selected? 用户在GridView ASP.NET中选择一行时如何自动选择同一ID的另一行 - How to automatically select other row of same ID when user selected a row in GridView ASP.NET DatagridView:如何删除选定的单元格 - DatagridView: How to delete selected cells 如何获取选定的行ID并将其用于LINQ中的删除查询? - How to get the selected row ID and use it for a delete query in LINQ? 如何在选项中显示属性,但在选择时获得ID - How can I display a property in options but get the id when selected 选择后如何显示和显示为连续图像? - How can I show and image as on row when is selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM