A SQL TIP

xiaoxiao2021-03-06  55

For SQL statements as follows: select * from t1 where c1 = @ c1 and c2 = @ C2 If only the @ c1 parameter does not enter C2 parameters, the query critical query condition is usually required: SELECT * from T1 Where C1 = @ C1 This causes the SQL statement to be dynamically generated, and the processing is very cumbersome. Later, through the high-person point, change the SQL statement to the following form: SELECT * from T1 WHERE @ C1 in (C1, ') and @ c2 in (C2,' ') so when @ C2 is empty, the statement is equivalent to : Select * from t1 where @ c1 in (c1, '') and @ c2 <> '' Oh, it will never have to worry about dynamically generated query conditions.

PS: The expression in parentheses should vary depending on the type of query field.

转载请注明原文地址:https://www.9cbs.com/read-116419.html

New Post(0)