34,838
社区成员




--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
create table [test]([id] int,[value] int)
insert [test]
select 1,10 union all
select 11,20 union all
select 21,30 union all
select 31,40 union all
select 41,50
declare @str varchar(1000)
set @str='13-35'
select @str=REPLACE(@str,'-',' and ')
exec('select * from test where [value] between '+@str+' or id between '+@str)
/*
id value
11 20
21 30
31 40
*/
--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
create table [test]([id] int,[value] int)
insert [test]
select 1,10 union all
select 11,20 union all
select 21,30 union all
select 31,40 union all
select 41,50
declare @str varchar(1000)
set @str='13-35'
select @str=REPLACE(@str,'-',' and ')
exec('select * from test where [value] between '+@str)
/*
id value
----------------
11 20
21 30
*/
不知道你的结果是怎么出来的
--> 测试数据:[tb]
IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
GO
CREATE TABLE [tb]([num1] INT,[num2] INT)
INSERT [tb]
SELECT 1,10 UNION ALL
SELECT 11,20 UNION ALL
SELECT 21,30 UNION ALL
SELECT 31,40 UNION ALL
SELECT 41,50 UNION ALL
SELECT 20,10 --增加一个[num2]>[num1]的
--------------开始查询--------------------------
--13-35
--应该是下面三行才合理吧
SELECT * FROM [tb] WHERE [num2]>13 AND [num2]>[num1] AND [num1]<35
----------------结果----------------------------
/*
num1 num2
----------- -----------
11 20
21 30
31 40
(3 行受影响)
*/