2

我正在使用一个存储过程,它在第一次执行时运行而不会抱怨权限。存储过程只有一个 UID/PWD 设置(没有不同的 UID/PWD 集用于授予不同的权限级别)。单对提供对所有内容的许可。

我调用存储过程的代码是这样的:

DataTable dtPriceComplianceResults 
SQLDBHelper.ExecuteSQLReturnDataTable(PriceComplianceConstsAndUtils.SUMMARY_STOREDPROC, CommandType.StoredProcedure,
        new SqlParameter()
        {
            ParameterName = "@BegDate",
            SqlDbType = SqlDbType.VarChar, 
            Value = _begDateStr
        },
        new SqlParameter()
        {
            ParameterName = "@EndDate",
            SqlDbType = SqlDbType.VarChar,
            Value = _endDateStr
        },
        new SqlParameter()
        {
            ParameterName = "@Member",
            SqlDbType = SqlDbType.VarChar,
            Value = _member
        },
        new SqlParameter()
        {
            ParameterName = "@Unit",
            SqlDbType = SqlDbType.VarChar,
            Value = _unit
        });

        public static DataTable ExecuteSQLReturnDataTable(string sql, CommandType cmdType, params SqlParameter[] parameters)
        {
            using (DataSet ds = new DataSet())
            using (SqlConnection connStr = new SqlConnection(PriceComplianceConstsAndUtils.CPSConnStr))

            using (SqlCommand cmd = new SqlCommand(sql, connStr))
            {
                cmd.CommandType = cmdType;
                cmd.CommandTimeout = EXTENDED_TIMEOUT;

                foreach (var item in parameters)
                {
                    cmd.Parameters.Add(item);
                }

                try
                {
                    cmd.Connection.Open();
                    new SqlDataAdapter(cmd).Fill(ds);
                }
                catch (SqlException sqlex)
                {
                    for (int i = 0; i < sqlex.Errors.Count; i++)
                    {
                        var sqlexDetail = String.Format("From ExecuteSQLReturnDataTable(), SQL Exception #{0}{1}Source: {2}{1}Number: {3}{1}State: {4}{1}Class: {5}{1}Server: {6}{1}Message: {7}{1}Procedure: {8}{1}LineNumber: {9}",
                            i + 1, // Some users would get the fantods if they saw #0
                            Environment.NewLine,
                            sqlex.Errors[i].Source,
                            sqlex.Errors[i].Number,
                            sqlex.Errors[i].State,
                            sqlex.Errors[i].Class,
                            sqlex.Errors[i].Server,
                            sqlex.Errors[i].Message,
                            sqlex.Errors[i].Procedure,
                            sqlex.Errors[i].LineNumber);
                        MessageBox.Show(sqlexDetail);
                    }
                }
                catch (Exception ex)
                {
                    String exDetail = String.Format(PriceComplianceConstsAndUtils.ExceptionFormatString, ex.Message, Environment.NewLine, ex.Source, ex.StackTrace);
                    MessageBox.Show(exDetail);
                }
                return ds.Tables[0];
            }
        }

这是存储过程的第一部分:

ALTER procedure [dbo].[sp_duckbilled_platypus]
    @BegDate varchar(10),
    @EndDate varchar(10),
    @Member varchar(max),
    @Unit varchar(max)
AS
    drop table zDistDBPExceptions 

    select (ph.memberno), TotalDesExceptions=1
    into zDistDBPExceptions
    from    priceexceptionshistory ph
    inner join MasterUnits MU on ph.Unit=MU.Unit
    Inner Join Members M on ph.memberno = M.MemberNo
    where   ph.memberNo not in ('04501','04503')    --,'111','B140') 
    and ph.memberno in (select [value] from dbo.split(@member,','))
    and  ph.Unit in (select [value] from dbo.Split(@Unit,','))
    and     invoicedate between @BegDate and @EndDate
    and     filtered=0
    and     abs(contractprice) = 0
    and     abs(ph.pricepush) = 0
    and     bidprice > 0
    and     abs(MU.TruTrack) = 1 
    and     abs(ph.pricesheet) = 1

    drop table zContractDBPExceptions

    select (ph.memberno), TotalContractExceptions=1
    into zContractDBPExceptions
    from    priceexceptionshistory ph
        inner join MasterUnits MU on ph.Unit=MU.Unit
        Inner Join Members M on ph.memberno = M.MemberNo
    where ph.memberNo not in ('04501','04503')  --,'111','B140') 
        and     ph.memberno in (select [value] from dbo.split(@member,','))
        and     ph.Unit in (select [value] from dbo.Split(@Unit,','))
        and     invoicedate between @BegDate and @EndDate
        and     filtered=0
        and     abs(contractprice) = 1
        and     abs(ph.pricepush) = 1
        and     bidprice > 0
        and     abs(MU.TruTrack) = 1 

    drop table zDBPExceptions

    select (ph.memberno), TotalPriceSheetExceptions=1--, invoicedate
    into zDBPExceptions
    from    priceexceptionshistory ph
    inner join MasterUnits MU on ph.Unit=MU.Unit
        Inner Join Members M on ph.memberno = M.MemberNo
    where   ph.memberNo not in ('04501','04503')    --,'111','B140')
    and ph.memberno in (select [value] from dbo.split(@member,','))
    and  ph.Unit in (select [value] from dbo.Split(@Unit,','))
    and     invoicedate between @BegDate and @EndDate
    and     filtered=0
    and     abs(contractprice) = 0
    and     abs(ph.pricepush) = 1
    and     bidprice > 0
    and     abs(MU.TruTrack) = 1 

    drop table zSumtDBPExceptions
    select (ph.memberno), TotalSumExceptions=1
    into zSumtDBPExceptions
    from    priceexceptionshistory ph
    inner join MasterUnits MU on ph.Unit=MU.Unit
    Inner Join Members M on ph.memberno = M.MemberNo
    where   ph.memberNo not in ('04501','04503')    --,'111','B140') 
    and ph.memberno in (select [value] from dbo.split(@member,','))
    and  ph.Unit in (select [value] from dbo.Split(@Unit,','))
    and     invoicedate between @BegDate and @EndDate
    and     filtered=0
    and     bidprice > 0
    and     abs(MU.TruTrack) = 1 
    and     abs(ph.pricesheet) = 1

    --this gets all invoice data
    --insert into PriceExceptionsHistory
    -- *** zContractDBPBase *** 
    drop table zContractDBPBase
    . . .

发生的非常奇怪(ISTM)的事情是昨天存储过程根本无法运行,告诉我要么被删除的表(全部被删除在存储过程中,依次列出)要么不存在,要么我做了没有他们的许可。它们存在。所以权限似乎是个问题。

然而,今天早上,在没有更改代码或数据库的情况下,存储过程在第一次运行时没有抱怨权限问题(仅限第一次)。

但是,在第二次执行时,它抱怨我没有 zContractDBPBase 表的权限。我想我现在有权删除前四个,但没有这个......?!?

IOW,我得到的错误消息(在“查询完成并出现错误”之后)现在是:

消息 3701,级别 11,状态 5,过程 sp_zDBP_pella,第 80 行
无法删除表“zContractDBPBase”,因为它不存在或您没有权限。

...而以前它是相同的消息,但对于所有删除的表,而不仅仅是一个。

那么为什么权限是可变的呢?我刷新了表列表,仍然看到我显然没有权限的表(“zContractDBPBase”);在 Visual Studio IDE 中的服务器资源管理器和 LINQPad 中刷新时都可以看到它。

我需要做什么(除了更改存储过程本身,这超出了我的职责和专业知识范围)才能让存储过程允许“我”删除表?

4

1 回答 1

1

Pikoh 的评论让我想起了我之前想知道的事情——如果同时运行 LINQPad 有问题——LINQPad“用户”和 Visual Studio“用户”都挂在这些连接上,或 SP,或导致松露。

我关闭了 LINQPad,不再有这些问题——既没有通过 VS 中的服务器资源管理器运行 SP,也没有通过应用程序中的 C# 代码。

我喜欢 LINQPad,但它显然不能很好地与 SQL Server 配合使用,反之亦然。

于 2015-12-15T17:22:32.190 回答