必须声明标量变量“@MyDivision_1”

Posted

技术标签:

【中文标题】必须声明标量变量“@MyDivision_1”【英文标题】:Must declare the scalar variable "@MyDivision_1" 【发布时间】:2018-12-14 05:27:23 【问题描述】:

这很好用:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:dbFOConnectionString %>" 
     SelectCommand="SELECT sProductName,sDivision,sStockistName,Qty
                    FROM TblPresentStock 
                    WHERE sDivision = @MyDivision_1 " >
     <SelectParameters>
         <asp:ControlParameter ControlID="ddlDivision"
              PropertyName="SelectedValue"
              Name="MyDivision_1"
              Type="String" />
     </SelectParameters>
</asp:SqlDataSource>

这也很好用:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:dbFOConnectionString %>" 
     SelectCommand="/* COLUMNS HEADERS */
                DECLARE @columnHeaders NVARCHAR (MAX)

                SELECT @columnHeaders  = COALESCE (@columnHeaders   
                       + ',[' + sStockistName + ']', '[' + sStockistName + ']')
                FROM   TblPresentStock  
                GROUP BY sStockistName
                ORDER BY sStockistName

                /* GRAND TOTAL COLUMN */
                DECLARE @GrandTotalCol  NVARCHAR (MAX)
                SELECT @GrandTotalCol = COALESCE (@GrandTotalCol + 'ISNULL ([' + 
                       CAST (sStockistName AS VARCHAR) +'],0) + ', 'ISNULL([' + CAST(sStockistName AS VARCHAR)+ '],0) + ')
                FROM TblPresentStock 
                GROUP BY sStockistName
                ORDER BY sStockistName

                SET @GrandTotalCol = LEFT (@GrandTotalCol, LEN (@GrandTotalCol)-1)

                /* GRAND TOTAL ROW */
                DECLARE @GrandTotalRow  NVARCHAR(MAX)
                SELECT @GrandTotalRow = COALESCE(@GrandTotalRow + ',ISNULL(SUM([' + 
                CAST(sStockistName AS VARCHAR)+']),0)', 'ISNULL(SUM([' + CAST(sStockistName AS VARCHAR)+']),0)')
                FROM     TblPresentStock  
                    GROUP BY sStockistName
                  ORDER BY  sStockistName
                ----------------------------------------------

                 -- DROP TABLE  temp_MatchesTotal

                /* MAIN QUERY */
                DECLARE @FinalQuery NVARCHAR (MAX)
                SET @FinalQuery =   'SELECT *, (' + @GrandTotalCol + ') 
                AS [Grand Total] INTO  #temp_MatchesTotal
                            FROM
                                (SELECT sProductName,sDivision,sStockistName,Qty
                                    FROM  TblPresentStock 
                                )A
                            PIVOT
                                (
                                 sum (Qty)
                                 FOR sStockistName
                                 IN ('  +@columnHeaders +  ') 
                                ) B 

                ORDER BY sProductName,sDivision
                SELECT * FROM  #temp_MatchesTotal 
                UNION ALL
                SELECT ''Grand Total'','''','+@GrandTotalRow +',  
                ISNULL (SUM([Grand Total]),0) FROM  #temp_MatchesTotal
                  DROP TABLE  #temp_MatchesTotal'
                 -- PRINT 'Pivot Query '+@FinalQuery
                 -- SELECT @FinalQuery
                 EXECUTE(@FinalQuery)">
    <SelectParameters>
        <asp:ControlParameter ControlID="ddlDivision"
             PropertyName="SelectedValue"
             Name="MyDivision_1"
             Type="String" />
    </SelectParameters>
</asp:SqlDataSource>  

在上面的查询中当我替换这一行时:

SELECT ProductName, sDivision, sStockistName, Qty
FROM TblPresentStock 

用这个:

SELECT sProductName, sDivision, sStockistName, Qty 
FROM TblPresentStock 
WHERE sDivision = @MyDivision_1

发生错误:

唯一的补充是

WHERE sDivision = @MyDivision_1

如果我像下面这样特别提到部门名称,那么它工作正常并且可以毫无错误地获取该特定部门的数据。

WHERE sDivision = ''Div1''

【问题讨论】:

是否在查询运行时为 DropDownList ddlDivision 选择了值? 是的,但如果我没有错,即使下拉列表中没有值,那么查询结果应该是空白的。第一个查询工作正常,它针对相同的下拉菜单。 我之前肯定遇到过参数默认值的问题。 嗯...但是第一个查询工作正常,它也针对相同的下拉列表。 要调试添加事件处理程序OnSelecting 并检查已创建的命令。您将能够看到 a) 参数是否存在,以及 b) 是否为其分配了有意义的值。参考:docs.microsoft.com/en-us/dotnet/api/… 【参考方案1】:

我只需要替换这个

WHERE sDivision = @MyDivision_1

WHERE sDivision = ''' + @MyDivision_1 + '''

This 文章帮助我解决了这个问题。

【讨论】:

以上是关于必须声明标量变量“@MyDivision_1”的主要内容,如果未能解决你的问题,请参考以下文章

必须声明标量变量@Id?

表变量和错误“必须声明标量变量”

不正确的语法必须声明标量变量

例外 |必须声明标量变量@IdAlbum

查询“必须声明标量变量”

Dapper:必须在 Select 命令中声明标量变量