2

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

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 动态获取不同的输入。

基本上,我有一个包含 3 个选项的下拉列表,所有这些都在上面正确显示,但我需要一个选择参数来更改输入的值以产生 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>
4

3 回答 3

1

更新 where 子句以检查参数的值或 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)
于 2013-07-25T20:16:36.620 回答
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]

您将有一个 ThemeTypeId,它是一个整数或一个由“null”组成的字符串,您可以与LIKE例如:'null'、、'%'OR进行比较'%[0-9]%'

于 2013-07-26T15:51:42.540 回答
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="" 才能正常工作。

于 2013-07-25T20:13:46.033 回答