1
votes

Query SQL server

SELECT TOP (1) * FROM [TbaleA] 
WHERE (num IS NOT NULL) AND CAST(('0'+[num]) AS BIGINT) = 0123456 
AND(phone= '975421545' OR phone IN((SELECT ISNULL(phone, '') as phone FROM [TbaleA]
WHERE id IN((SELECT id FROM [TbaleA]
WHERE roll = (SELECT  roll FROM [TbaleA]
WHERE num='{0}' OR CAST(('0'+[num]) AS BIGINT) = 0123456 ))))))

The execution time is long, how can it be improved?

That query isn't valid. phone= {1} isn't valid T-SQL syntax. Neither is = {0} - Larnu
I think you need to take a step back here and explain what the problem you are trying to solve here is. All those nested subqueries smells like a problem unto itself. You also have a TOP (1) but no ORDER BY, meaning that SQL Server is free to return any arbitrary row; a likely flaw in your query unless you are happy with inconsistent results. - Larnu
@larnu I fixed it - Amen