0

您好,这是我编写的第一个光标程序。

在成功影响我想要使用此过程执行的过程中的表行后,我很惊讶地看到错误。

我正在将菜单分配给帖子,当我从特定帖子中删除某些菜单时,我需要为属于该帖子的所有员工更新(添加/删除)菜单。下面的过程可以做所有事情,但我收到一个错误:

错误:数据库中已有一个名为“EmployeeMenuMst”的对象。

执行后。

存储过程是:

ALTER PROCEDURE [sUpdateEmpMenuMst]
    (@EmployeePostID INT)
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @EMPID INT
    DECLARE @EMPMENUID INT

    BEGIN TRAN
    BEGIN TRY
    DECLARE EmployeeTable CURSOR FOR

    SELECT EmployeeID FROM EMPLOYEEMST WHERE EmployeePostID=@EmployeePostID

    OPEN EmployeeTable

    FETCH NEXT FROM EmployeeTable INTO @EMPID
    WHILE @@FETCH_STATUS = 0
    BEGIN
    DELETE FROM EmployeeMenuMst WHERE EmployeeID=@EMPID  

    FETCH NEXT FROM EmployeeTable INTO @EMPID
    END
    CLOSE EmployeeTable
    DEALLOCATE EmployeeTable

        SELECT   EmployeeMst.EmployeeID, EmployeePostMenuMst.MenuID, EmployeePostMenuMst.AccessLevelID,1 AS ISVISIBLE
        INTO EmployeeMenuMst
        FROM     EmployeePostMenuMst 
        INNER JOIN EmployeeMst 
        ON EmployeePostMenuMst.EmployeePostID = EmployeeMst.EmployeePostID
        WHERE EmployeeMst.EMPLOYEEPOSTID=@EmployeePostID ORDER BY EmployeeMst.EMPLOYEEID
        COMMIT TRAN
    END TRY

    BEGIN CATCH
        ROLLBACK TRAN
        IF @@TRANCOUNT > 0
        DECLARE @ErrMsg nvarchar(4000), @ErrSeverity INT
        SELECT @ErrMsg = ERROR_MESSAGE(),
        @ErrSeverity = ERROR_SEVERITY()
        RAISERROR(@ErrMsg, @ErrSeverity, 1)

    END CATCH

   END
GO

aspx.cs文件后面的代码中:

private void UpdateMenus()
    {
        SqlCommand cmd = new SqlCommand();
        SqlConnection con = new SqlConnection(conStr);
        try
        {
            con.Open();
            cmd.CommandText = "sUpdateEmpMenuMst";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@EmployeePostID", Convert.ToInt32(Session["postID"]));
            cmd.Connection = con;
            int i = cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            con.Close();
        }
        finally
        {
            con.Close();
        }
    }

错误ExecuteNonQuery在线。为什么它会引发错误 - 任何想法?

在得到错误行之后,我需要的方式成功地受到影响!

帮助表示赞赏!

4

1 回答 1

2
SELECT   EmployeeMst.EmployeeID, EmployeePostMenuMst.MenuID, EmployeePostMenuMst.AccessLevelID,1 AS ISVISIBLE
        INTO EmployeeMenuMst

将尝试创建一个表 EmployeeMenuMst ans fill ist,也许您正在搜索

Insert into EmployeeMenuMst
SELECT   EmployeeMst.EmployeeID, EmployeePostMenuMst.MenuID, EmployeePostMenuMst.AccessLevelID,1 AS ISVISIBLE
于 2012-12-19T11:49:35.367 回答