使用选择参数动态更改JOIN的结果

时间:2013-07-25 19:12:15

标签: c# asp.net sql sql-server

我有一个我正在使用的选择查询涉及网格视图显示的连接:

SELECT [Theme].[Name], [ThemeType].[Type] 
FROM [Theme] 
Left Outer Join [ThemeType] ON [Theme].[ThemeTypeId] = [ThemeType].[PK_ThemeType] 
ORDER BY CASE WHEN [ThemeType].[Type] IS NULL THEN 1 ELSE 0 END, [ThemeType].[Type]

目前返回所有内容。我需要能够显示无null返回或只显示nullr eturns,并且来自同一查询,它将通过SelectParameters动态获取不同的输入。

基本上,我有一个包含3个选项的下拉列表,所有这些选项都在上面正确显示,但是我需要一个select参数来更改输入值以产生3个选项。

我试过了:

SELECT [Theme].[Name], [ThemeType].[Type] FROM [Theme] 
Left Outer Join [ThemeType] ON [Theme].[ThemeTypeId] = [ThemeType].[PK_ThemeType] 
WHERE [Theme].[ThemeTypeId] LIKE '%' 
ORDER BY CASE WHEN [ThemeType].[Type] IS NULL THEN 1 ELSE 0 END, [ThemeType].[Type]

%可以改变,但所有这些都是非空的......

这是数据源:

<asp:SqlDataSource ID="SqlDataSource6" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
    SelectCommand="SELECT [Theme].[Name], [ThemeType].[Type] FROM [Theme] Left Outer Join [ThemeType] ON [Theme].[ThemeTypeId] = [ThemeType].[PK_ThemeType] ORDER BY CASE WHEN [ThemeType].[Type] IS NULL THEN 1 ELSE 0 END, [ThemeType].[Type]">
     <SelectParameters>
            <asp:QueryStringParameter Name="changeParam" Type="String" />
</SelectParameters>

3 个答案:

答案 0 :(得分:1)

更新你的where子句以检查参数的值或参数IS NULL的位置; [Theme].[ThemeTypeId] = @changeParam OR @changeParam IS NULL

SELECT
     [Theme].[Name],
     [ThemeType].[Type]
FROM
     [Theme] LEFT OUTER JOIN [ThemeType]
          ON
     [Theme].[ThemeTypeId] = [ThemeType].[PK_ThemeType] 
WHERE
     [Theme].[ThemeTypeId] = @changeParam
           OR
      (COALESCE([Theme].[ThemeTypeId], N'NULL') = N'NULL' AND @changeParam IS NULL)
ORDER BY
     CASE WHEN [ThemeType].[Type] IS NULL THEN 1 ELSE 0 END,
     [ThemeType].[Type]

这是一个简短的示例,可以尝试查看它是否可以正常工作。

DECLARE @t TABLE
(
    Theme       nvarchar(100),
    ThemeType   nvarchar(100)
)

INSERT INTO
    @t
    (
        Theme,
        ThemeType
    )
--I use the union since I am working on SQL Server 2005
SELECT
    N'1',
    N'1111'
UNION
SELECT
    N'1',
    N'----'
UNION
SELECT
    N'2',
    N'2222'
UNION
SELECT
    null,
    N'2222'

DECLARE @s  nvarchar(100)
SET @s = N'1' -- change this line for the criteria
--SET @s = null

SELECT
    *
FROM
    @t
WHERE
    Theme = @s
        OR
    (COALESCE(Theme, N'NULL') = N'NULL' AND @s IS NULL)

答案 1 :(得分:1)

正如我在评论中试图指出的那样,COALESCE允许你想要的......

SELECT [Theme].[Name], [ThemeType].[Type] FROM [Theme] 
Left Outer Join [ThemeType]
             ON [Theme].[ThemeTypeId] = [ThemeType].[PK_ThemeType] 
WHERE COALESCE([Theme].[ThemeTypeId], 'null') LIKE '%' 
ORDER BY CASE 
         WHEN [ThemeType].[Type] IS NULL 
         THEN 1 
         ELSE 0 
         END, [ThemeType].[Type]

您将拥有一个整数或一个由'null'组成的字符串的ThemeTypeId,您可以将其与LIKE进行比较,例如:'null''%'或者{{1} }。

答案 2 :(得分:0)

我没有弄清楚如何以我想要的方式去做,但我采取了另一种方法并以编程方式进行。我有一个方法,当我的下拉选项菜单触发时触发,并且根据选择,gridview获取不同的选择查询。

protected void viewThemeTypeAssociationsDropDown_OnSelectedIndexChanged(object sender, EventArgs e)
{
    if (viewThemeTypeAssociationsDropDown.SelectedIndex == 0)
    {
        SqlDataSource6.SelectCommand = "SELECT [Theme].[Name], [ThemeType].[Type] FROM [Theme] Left Outer Join [ThemeType] ON [Theme].[ThemeTypeId] = [ThemeType].[PK_ThemeType] ORDER BY [Theme].[Name] ASC";
    }
    else if (viewThemeTypeAssociationsDropDown.SelectedIndex == 1)
    {
        SqlDataSource6.SelectCommand = "SELECT [Theme].[Name], [ThemeType].[Type] FROM [Theme], [ThemeType] WHERE [Theme].[ThemeTypeId] = [ThemeType].[PK_ThemeType] ORDER BY [Theme].[Name] ASC";
    }
    else
    {
        SqlDataSource6.SelectCommand = "SELECT [Theme].[Name], [ThemeType].[Type] FROM [Theme] Left Outer Join [ThemeType] ON [Theme].[ThemeTypeId] = [ThemeType].[PK_ThemeType] WHERE [Theme].[ThemeTypeId] IS NULL ORDER BY [Theme].[Name] ASC";
    }
}

您必须从Sqldatasource中完全删除selectcommand =“”才能使其正常工作。