sql server 2005 没有 split 函数 所以对于一串有分隔符的字段 不能直接拆开用 in 来查询,必须用循环 取出 组合出字符串 或临时表,因为我做这个是直接code 换 name ,写成函数 更加通用,代码如下:
Code
-- =============================================
-- Author:
-- Create date: <2009-3-3 13:32>
-- Description: <根据TeamCode 和 PincipalCode 查询 TeamName>
-- =============================================
ALTER FUNCTION [dbo].[Fun_GetTeamName]
(
@TeamCodes varchar(50),
@PincipalCode varchar(50)
)
RETURNS varchar(50)
AS
BEGIN
Declare @TeamName varchar(50);
declare @TeamCode varchar(10);
Declare @locality int;
Declare @TeamNames varchar(200);
Declare @Last int;
set @TeamNames=''
set @locality=1
set @Last=len(@TeamCodes) --获取总数
if charindex(',',@TeamCodes)>0 --判断参数是否包含,
begin
while(@locality<@Last) -- 当前字符位数少于总数就循环
begin
set @locality=charindex(',',@TeamCodes) --获取其初始位置
if @locality=0
begin
set @locality=len(@TeamCodes)+1 --最后一次时,用+1来抵消-1
set @Last=0 --置0跳出循环
end
--获取最前面的TeamCode
set @TeamCode=substring(@TeamCodes,1,@locality-1)
--截断已被截取的TeamCode,下一次循环将获取下一个值
set @TeamCodes=substring(@TeamCodes,@locality+1,len(@TeamCodes))
-- 查询获取TeamName
select @TeamName=TeamName from TM_TEAMS where TeamCode like (@TeamCode) and PrincipalCode like @PincipalCode
--如果查不到TeamName,将会重复加值,如003,查不到,将出现002,002
if(charindex(@TeamName,@TeamNames)<1)
set @TeamNames=@TeamNames+','+@TeamName
end
if charindex(',',@TeamNames)=1 --去掉最前面的,
set @TeamNames= substring(@TeamNames,2,len(@TeamNames))
end
else
begin
select @TeamNames=TeamName from TM_TEAMS where TeamCode like (@TeamCodes) and PrincipalCode like @PincipalCode
end
RETURN @TeamNames
END
-- =============================================
-- Author:
-- Create date: <2009-3-3 13:32>
-- Description: <根据TeamCode 和 PincipalCode 查询 TeamName>
-- =============================================
ALTER FUNCTION [dbo].[Fun_GetTeamName]
(
@TeamCodes varchar(50),
@PincipalCode varchar(50)
)
RETURNS varchar(50)
AS
BEGIN
Declare @TeamName varchar(50);
declare @TeamCode varchar(10);
Declare @locality int;
Declare @TeamNames varchar(200);
Declare @Last int;
set @TeamNames=''
set @locality=1
set @Last=len(@TeamCodes) --获取总数
if charindex(',',@TeamCodes)>0 --判断参数是否包含,
begin
while(@locality<@Last) -- 当前字符位数少于总数就循环
begin
set @locality=charindex(',',@TeamCodes) --获取其初始位置
if @locality=0
begin
set @locality=len(@TeamCodes)+1 --最后一次时,用+1来抵消-1
set @Last=0 --置0跳出循环
end
--获取最前面的TeamCode
set @TeamCode=substring(@TeamCodes,1,@locality-1)
--截断已被截取的TeamCode,下一次循环将获取下一个值
set @TeamCodes=substring(@TeamCodes,@locality+1,len(@TeamCodes))
-- 查询获取TeamName
select @TeamName=TeamName from TM_TEAMS where TeamCode like (@TeamCode) and PrincipalCode like @PincipalCode
--如果查不到TeamName,将会重复加值,如003,查不到,将出现002,002
if(charindex(@TeamName,@TeamNames)<1)
set @TeamNames=@TeamNames+','+@TeamName
end
if charindex(',',@TeamNames)=1 --去掉最前面的,
set @TeamNames= substring(@TeamNames,2,len(@TeamNames))
end
else
begin
select @TeamNames=TeamName from TM_TEAMS where TeamCode like (@TeamCodes) and PrincipalCode like @PincipalCode
end
RETURN @TeamNames
END
TM_TEAMS 表结构:
实际应用表字段结构:
由于teamCode字段 存在多个teamCode值,想直接在展示视图看teamName,用in语句显然 拼接 不起来,所以需要用函数来中转展示。
测试语句如下:
select
dbo.Fun_GetTeamName(
'
001,002
'
,
'
035
'
)
附上一个查询: 查出同一个 orderno 是否有多个 itemno 的情况:
Code
select b.OrderNo,count(0) as co from (SELECT OrderNo, COUNT(itemno) AS co
FROM TM_ORDERS_ITEMS
GROUP BY OrderNo,itemno
HAVING (COUNT(Principal) > 1)) b group by b.OrderNo having count(b.OrderNo)>1
select b.OrderNo,count(0) as co from (SELECT OrderNo, COUNT(itemno) AS co
FROM TM_ORDERS_ITEMS
GROUP BY OrderNo,itemno
HAVING (COUNT(Principal) > 1)) b group by b.OrderNo having count(b.OrderNo)>1