SQL Server - Query Short-Circuiting?

后端 未结 7 1450
北荒
北荒 2020-12-01 20:00

Do T-SQL queries in SQL Server support short-circuiting?

For instance, I have a situation where I have two database and I\'m comparing data between the two tables to

相关标签:
7条回答
  • 2020-12-01 20:50

    fix the database to be consistent

    select * from table1 where table1.ID LIKE '%1234'
    

    will match '1234', '01234', '00000000001234', but also '999991234'. Using LIKE pretty much guarantees an index scan (assuming table1.ID is indexed!). Cleaning up the data will improve performance significantly.

    if cleaning up the data is not possible, write a user-defined function (UDF) to strip off leading zeros, e.g.

    select * from table1 where dbo.udfStripLeadingZeros(table1.ID) = '1234'
    

    this may not improve performance (since the function will have to run for each row) but it will eliminate false matches and make the intent of the query more obvious

    EDIT: Tom H's suggestion to CAST to an integer would be best, if that is possible.

    0 讨论(0)
提交回复
热议问题