日期:2014-05-17 浏览次数:20584 次
--> 测试数据:[odd] IF OBJECT_ID('[odd]') IS NOT NULL DROP TABLE [odd] GO CREATE TABLE [odd]([Name] VARCHAR(1),[fNum] INT,[LNum] INT) INSERT [odd] SELECT 'A',1,5 UNION ALL SELECT 'B',6,10 UNION ALL SELECT 'C',11,15 UNION ALL SELECT 'D',16,20 --> 测试数据:[bill] IF OBJECT_ID('[bill]') IS NOT NULL DROP TABLE [bill] GO CREATE TABLE [bill]([Num] INT) INSERT [bill] SELECT 3 UNION ALL SELECT 7 UNION ALL SELECT 13 UNION ALL SELECT 18 --------------开始查询-------------------------- IF OBJECT_ID('[vv]') IS NOT NULL DROP VIEW [vv] GO CREATE VIEW vv AS SELECT a.[Name],b.[Num] FROM [odd] a, [bill] b WHERE b.[Num] BETWEEN a.[fNum] AND a.[LNum] GO SELECT * FROM vv ----------------结果---------------------------- /* Name Num A 3 B 7 C 13 D 18 */
------解决方案--------------------
create view V_odd_bill as select b.Num,a.Name from odd a, bill b where a.id=b.id and b.Num>a.fNum and b.Num<a.LNum go
------解决方案--------------------
use tempdb if OBJECT_ID('tb1')=NULL drop table ta1 go if OBJECT_ID('tb2')=NULL drop table ta2 create table tb1 (name nvarchar(10) ,fNum int , LNum int) insert into tb1 select 'A',1,5 union all select 'B',6,10 union all select 'C',11,15 union all select 'D',16,20 CREATE TABLE TB2 (NUM INT) INSERT TB2 SELECT 3 UNION ALL SELECT 7 UNION ALL SELECT 13 UNION ALL SELECT 18 SELECT * , (SELECT name FROM tb1 WHERE NUM BETWEEN fNum AND LNum) FROM TB2