DECLARE sales_rep CURSOR FOR SELECT StuName from StudentTable where Sex = '男'
DECLARE @Ref_Value nvarchar(50)
DECLARE @ACT_Value nvarchar(50)
set @Ref_Value=''
set @ACT_Value=''
OPEN sales_rep
/*得到第一条记录,如果失败@@FETCH_STATUS返回-1,如果没有记录@@FETCH_STATUS则返回-2*/ FETCH NEXT FROM sales_rep INTO @Ref_Value WHILE (@@FETCH_STATUS <> -1) BEGIN IF (@@FETCH_STATUS <> -2) Begin --print '开始'
set @ACT_Value=@ACT_Value+@Ref_Value+','
End /*读取下一条*/ FETCH NEXT FROM sales_rep INTO @Ref_Value END /*关闭游标*/ CLOSE sales_rep /*释放游标*/ DEALLOCATE sales_rep select @ACT_Value
------解决方案--------------------
------解决方案-------------------- DataTable dt = new DataTable(); string str = string.Join(",", (from temp in dt.AsEnumerable() select new { StuName = temp.Field<string>("StuName ") }).ToArray());
------解决方案--------------------
SQL code
select LEFT(Name,LEN(Name)-1) As Name from (select (select StuName+',' from StudentTable where Sex ='男' FOR XML PATH('')) As Name) B