Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad SQL #20

Closed
genusP opened this issue Jun 3, 2013 · 8 comments
Closed

Bad SQL #20

genusP opened this issue Jun 3, 2013 · 8 comments

Comments

@genusP
Copy link
Contributor

genusP commented Jun 3, 2013

For query:

this.GetTable<Departament>().Where(it => IIF((IIF((it.Name == null), null, Convert(it.Name.StartsWith("КИ"))) == null), False, IIF((it.Name == null), null, Convert(it.Name.StartsWith("КИ"))).Value))

result SQL:

SELECT
    [it].[Name],
    [it].[Type] as [Type1],
    [it].[Chief_Id],
    [it].[Parent_Id],
    [it].[Id]
FROM
    [Departaments] [it]
WHERE
    CASE
        WHEN CASE
            WHEN [it].[Name] IS NULL THEN NULL
            ELSE [it].[Name] LIKE N'КИ%'
        END IS NULL
            THEN 0
        WHEN [it].[Name] IS NULL THEN NULL
        ELSE [it].[Name] LIKE N'КИ%'
    END = 1

This query build WCF Data Services for uri: http://localhost:50089/ricService.svc/Departaments?$filter=startswith(Name,'%D0%9A%D0%98')

@igor-tkachev
Copy link
Member

What is wrong with this SQL?
And I am not sure what is IIF and Convert methods.

@genusP
Copy link
Contributor Author

genusP commented Jun 4, 2013

Sql Server says:

Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'LIKE'.

В этом запросе ему не нравится вложенное условие like

@igor-tkachev
Copy link
Member

What SQL do you expect?

@genusP
Copy link
Contributor Author

genusP commented Jun 7, 2013

SELECT
    [it].[Name],
    [it].[Type] as [Type1],
    [it].[Chief_Id],
    [it].[Parent_Id],
    [it].[Id]
FROM
    [Departaments] [it]
WHERE
    CASE
        WHEN CASE
            WHEN [it].[Name] IS NULL THEN NULL
            when [it].[Name] LIKE N'КИ%' then 1
        END IS NULL
            THEN 0
        WHEN [it].[Name] IS NULL THEN NULL
        when [it].[Name] LIKE N'КИ%' then 1
    END = 1

In SQL Server 2012 exists IIF function http://msdn.microsoft.com/en-us/library/hh213574.aspx

@igor-tkachev
Copy link
Member

Who implemented conversion of the IIF function to SQL?

@genusP
Copy link
Contributor Author

genusP commented Jun 7, 2013

IIF in linq query ConditionalExpression.
Conversion implemented in ExpressionBuilder.SqlBuilder line 710.

@igor-tkachev
Copy link
Member

From your example .Where(it => IIF((IIF((it.Name....
IIF looks like a custom function. What is that?

@genusP
Copy link
Contributor Author

genusP commented Jun 8, 2013

This LINQ query restored from Expression. IIF not function, is text view ConditionalExpression.
Sample:

var par = Expression.Parameter(typeof(Organization), "it");
var testExp = Expression.Equal(Expression.Property(par, "Name"), Expression.Constant(null)); //(it.Name==null)
var calExp=Expression.Convert(Expression.Call(Expression.Property(par, "Name"), typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }), Expression.Constant("КИ")), typeof(int?)); //Convert(it.Name.StartsWith("КИ"))
Expression conditionExpr = Expression.Condition( //II(
             testExp,
             Expression.Constant(null, typeof(int?)), //,null
             calExp
             );

Данный запрос формируется кодом WCF Data Services, поэтому мне пришлось его востанавливать из выражения. Думал что IIF это встроенная функция и не стал ее подменять. Ваши вопросы потребовали более глубокого изучения вопроса. Оказалось что IIF это текстовое представление выражения типа ConditionalExpression, что доказывает код выше.
Если посмотреть в отладчике на conditionExpr то будет написано выражение IIF.
PS. Может имеет смысл такое выражение представлять как функцию IIF, а не CASE как сейчас. А провайдер уже разберется и преобразует в нужный SQL.

igor-tkachev added a commit that referenced this issue Jun 10, 2013
igor-tkachev added a commit that referenced this issue Jun 10, 2013
otemnov pushed a commit to otemnov/bltoolkit that referenced this issue Feb 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants