简体   繁体   中英

Converting an Oracle Query using LIKE to SQL Server Query

I am converting an Application that is using an Oracle Database. In a part of the code there is a condition (Below). In the ELSE, they are using the LIKE with '{0}%'. The length of id in the database is always 8, so I don't understand why it is checking for a length of 10. So what happens is that if the user enters a valid id (12345678), it will always go to the ELSE. But I don't understand what the LIKE is actually doing. What would the conversion be for TSQL?

        if (id.Length == 10)
        {
            sb.Append("where job_id = :ID ");
        }
        else
        {
            sb.AppendFormat("where job_id like '{0}%' order by job_id desc ", id);
        }

like determines whether a specific character string matches a specified pattern. A pattern can include regular characters and wildcard. so your query basically look at the id length, if it is 10 then it try to find the exact match otherwise it looks for any job_id that starts with 0 ( looks like weird logic)

sb is a StringBuilder I believe? (You should always include such information in a question!) Then the arguments of AppendFormat() (for that given overload) are a format string and then an arbitrary number of parameters. To address the parameters in the format string their index enclosed in {} can be used. {0} is a place holder for the first parameter, {1} would be second and so on. So say if id is 123 for example "where job_id like '123%' order by job_id desc would be appended.

LIKE is an operator that compares two strings. The second operand can use wildcards -- in Oracle and SQL Server % is provided for any or zero arbitrary characters (including no character at all) or _ for exactly one arbitrary character. For example 'ABCDEF' LIKE 'ABC%' or 'ABCDED LIKE '%CD%'` would be true. SQL Server allows some other patterns too but that aren't of concern here. So there's nothing to translate here, the expressions are valid in Oracle and SQL Server.

Now what the C# code does is appending where job_id =:ID to the string if id , which is a string I assume, has a length of 10 characters. Otherwise it appends where job_id like '{0}%' order by job_id desc where {0} is replaced with id 's value.

But the code is flawed. You should never insert values for SQL queries by using string concatenation or interpolation but always use parameters. So

sb.AppendFormat("where job_id like '{0}%' order by job_id desc ", id);

should really be something like

sb.Append("where job_id like concat(:ID, '%') order by job_id desc ");

( concat() concatenates the strings given to it. And of course that may require to change subsequent calls executing the query to provide a value for the parameter either way.)

I cannot judge if it makes sense to order the result only if the id 's length isn't 10 nor if it makes any sense to only search exactly for it if its length is 10 but for any value beginning with it otherwise because I don't know the reasoning behind it. But it certainly seems strange. Maybe the whole if... else... should simply be replaced by

sb.Append("where job_id like concat(:ID, '%') order by job_id desc ");

after all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM