我有一个带有列客户端代码的表。
代码范围为 99990000 和 99999999。
我想要这个范围内的所有数字( 99990000 -99999999 )
未分配给客户代码。
请帮我解决这个问题。
我有一个带有列客户端代码的表。
代码范围为 99990000 和 99999999。
我想要这个范围内的所有数字( 99990000 -99999999 )
未分配给客户代码。
请帮我解决这个问题。
您需要生成可能帐户的完整列表,然后排除正在使用的帐户。
/*DECLARE VARIABLES FOR START AND END RANGE*/
DECLARE @StartAccount AS INT
DECLARE @EndAccount AS INT
SET @StartAccount = 99990000
SET @EndAccount = 99999999
;
--Generate a list of numbers to check against:..Thanks to Anthony Horne for this
WITH Nbrs_4( n ) AS ( SELECT 1 UNION SELECT 0 ),
Nbrs_3( n ) AS ( SELECT 1 FROM Nbrs_4 n1 CROSS JOIN Nbrs_4 n2 ),
Nbrs_2( n ) AS ( SELECT 1 FROM Nbrs_3 n1 CROSS JOIN Nbrs_3 n2 ),
Nbrs_1( n ) AS ( SELECT 1 FROM Nbrs_2 n1 CROSS JOIN Nbrs_2 n2 ),
Nbrs_0( n ) AS ( SELECT 1 FROM Nbrs_1 n1 CROSS JOIN Nbrs_1 n2 ),
Nbrs ( n ) AS ( SELECT 1 FROM Nbrs_0 n1 CROSS JOIN Nbrs_0 n2 )
SELECT n
FROM ( SELECT ROW_NUMBER() OVER (ORDER BY n)
FROM Nbrs ) D ( n )
WHERE n >= @StartAccount
AND n <= @EndAccount
AND n NOT IN
(SELECT ClientCode FROM CustomerTable) --Exclude Accounts that are in use
如果您只想选择之间的范围99990000 AND 99999999
SELECT * FROM
myTable
WHERE ClientCode BETWEEN (99990000 AND 99999999)
如果要选择排除范围99990000 AND 99999999
SELECT * FROM
myTable
WHERE ClientCode NOT BETWEEN (99990000 AND 99999999)
据我了解这个问题,这将是查询:
SELECT * FROM Table WHERE ClientCode BETWEEN 99990000 AND -99999999