27,580
社区成员
发帖
与我相关
我的任务
分享
declare @date datetime,@kgcode uniqueidentifier
declare @print varchar(max)
select @print='今日食谱:'
set @date = '2009-05-12'
set @kgcode = 'b997cb81-e5fe-40ef-98dc-61173bc86b5c'
if object_id('tempdb..#foodnames') is not null
drop table #foodnames;with [foodname]
as
(select TOP 100 b.typename,a.cookname,a.fooddate,a.displayorder
from kd_FoodWgMenu a join dic_FoodType b on a.guidfoodtype=b.guidfoodtype
where a.guidfood='00000000-0000-0000-0000-000000000000' and a.fooddate=@date and a.kindercode=@kgcode order by charindex(TypeName,'早餐,午餐,晚餐,早点,间点,晚点'),DisplayOrder)
select distinct fooddate,
foodnames=typename+':'+stuff((select ','+cookname from [foodname] b where a.fooddate=b.fooddate and a.typename=b.typename order by DisplayOrder for xml path('')),1,1,'') into #foodnames from [foodname] a order by charindex(TYPENAME,'早餐,午餐,晚餐,早点,间点,晚点'),DisplayOrder;select @print=@print+foodnames from #foodnames;
select @print
select distinct fooddate,TYPENAME,DisplayOrder,
foodnames=typename+':'+stuff((select ','+cookname from [foodname] b where a.fooddate=b.fooddate and a.typename=b.typename for xml path('')),1,1,'') into #foodnames from [foodname] a order by charindex(TYPENAME,'早餐,午餐,晚餐,早点,间点,晚点'),DisplayOrder;
select distinct fooddate,TYPENAME,DisplayOrder,
foodnames=typename+':'+stuff((select ','+cookname from [foodname] b where a.fooddate=b.fooddate and a.typename=b.typename order by DisplayOrder for xml path('')),1,1,'') into #foodnames from [foodname] a order by charindex(TYPENAME,'早餐,午餐,晚餐,早点,间点,晚点'),DisplayOrder;
if object_id('tempdb..#foodnames') is not null
drop table #foodnames;with [foodname]
as
(select TOP 100 b.typename,a.cookname,a.fooddate,a.displayorder
from kd_FoodWgMenu a join dic_FoodType b on a.guidfoodtype=b.guidfoodtype
where a.guidfood='00000000-0000-0000-0000-000000000000' and a.fooddate=@date and a.kindercode=@kgcode order by charindex(TypeName,'早餐,午餐,晚餐,早点,间点,晚点'),DisplayOrder)
select distinct fooddate,TYPENAME,
foodnames=typename+':'+stuff((select ','+cookname from [foodname] b where a.fooddate=b.fooddate and a.typename=b.typename order by DisplayOrder for xml path('')),1,1,'') into #foodnames from [foodname] a ;
--既然distinct 语句不允许我写order by 那么我这句就不写,重新定义一个表重新进行排序,哈哈,耍了点小聪明
if object_id('tempdb..#foodData') is not null
drop table #foodData;
select foodnames,typename into #foodData from #foodnames order by charindex(TYPENAME,'早餐,早点,午餐,间点,晚餐,晚点');
select @print=@print+' '+foodnames from #foodData
--try ?
declare @date datetime,@kgcode uniqueidentifier
declare @print varchar(max)
select @print='今日食谱:'
set @date = '2009-05-12'
set @kgcode = 'b997cb81-e5fe-40ef-98dc-61173bc86b5c'
if object_id('tempdb..#foodnames') is not null
drop table #foodnames
;with [foodname]
as
(select TOP 100 b.typename,a.cookname,a.fooddate,a.displayorder
from kd_FoodWgMenu a
join dic_FoodType b on a.guidfoodtype=b.guidfoodtype
where a.guidfood='00000000-0000-0000-0000-000000000000' and
a.fooddate=@date and
a.kindercode=@kgcode
order by charindex(TypeName,'早餐,午餐,晚餐,早点,间点,晚点'),DisplayOrder)
select distinct fooddate
,foodnames=typename+':'+stuff((select ','+cookname
from [foodname] b
where a.fooddate=b.fooddate and a.typename=b.typename for xml path('')),1,1,''
order by DisplayOrder)
into #foodnames
from [foodname] a
--建议用order by 1 或 order by 2 进行排序。
select @print=@print+foodnames
from #foodnames
select @print
declare @date datetime,@kgcode uniqueidentifier
declare @print varchar(max)
select @print='今日食谱:'
set @date = '2009-05-12'
set @kgcode = 'b997cb81-e5fe-40ef-98dc-61173bc86b5c'
if object_id('tempdb..#foodnames') is not null
drop table #foodnames
;with [foodname]
as
(select TOP 100 b.typename,a.cookname,a.fooddate,a.displayorder
from kd_FoodWgMenu a
join dic_FoodType b on a.guidfoodtype=b.guidfoodtype
where a.guidfood='00000000-0000-0000-0000-000000000000' and
a.fooddate=@date and
a.kindercode=@kgcode
order by charindex(TypeName,'早餐,午餐,晚餐,早点,间点,晚点'),DisplayOrder)
select distinct fooddate
,foodnames=typename+':'+stuff((select ','+cookname
from [foodname] b
where a.fooddate=b.fooddate and a.typename=b.typename
order by DisplayOrder for xml path('')),1,1,'')
into #foodnames
from [foodname] a
-- order by charindex(TYPENAME,'早餐,午餐,晚餐,早点,间点,晚点'),DisplayOrder
--此结果集已经在with [foodname] 中进行了排序,所以这里排序是无效且错误的。
--可以用order by 1 或 order by 2 进行排序。
select @print=@print+foodnames
from #foodnames
select @print