什么会导致我的 SQL 服务器权限从一个存储过程的执行更改为下一个?

What could cause my SQL Server permissions to change from one execution of a stored procedure to the next?

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

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

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) 事情是存储过程根本 运行 失败,告诉我要么 tables 被删除(所有都在存储过程中删除,依次列出每个)要么不存在,要么我没有权限使用它们。他们确实都存在。所以权限似乎是问题所在。

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

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

IOW,我收到的错误信息(在 "Query completed with errors" 之后)现在是:

Msg 3701, Level 11, State 5, Procedure sp_zDBP_pella, Line 80
Cannot drop the table 'zContractDBPBase', because it does not exist or you do not have permission.

...虽然以前是同一条消息,但对于所有丢弃的 table,而不仅仅是一个。

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

我需要做什么(除了更改存储过程本身,这超出了我的职责和专业范围)让存储过程允许 "me" 删除 tables?

Pikoh 的评论让我想起了我之前想知道的事情——如果同时使用 LINQPad 运行 是有问题的——LINQPad "user" 和 Visual Studio "user" 都是都挂在那些连接上,或者 SP,或者导致混乱的东西上。

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

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