表变量的使用:
DECLARE @T_ChartsIdeaItem1 Table(XData char(20) null,YData char(20) null)
INSERT INTO @T_ChartsIdeaItem1
(XData,YData)
VALUES ('BU9876', 'Creating')
select * from @T_ChartsIdeaItem1
查询列转行显示:
begin
declare @sql1 varchar(8000),
@sql2 varchar(8000),
@sql3 varchar(8000)
set @sql1='select '
Set @sql2='select '
set @sql3='select '
select @sql1=@sql1+ '''' +convert(varchar(50), 字段1)+ ''' as ['+ convert(varchar(50), 字段1) +'],',
@sql2=@sql2+ '''' +convert(varchar(50),×字段2)+''' as['+ convert(varchar(50), 字段1) +'],',
@sql3=@sql3+ '''' +convert(varchar(50), 字段3)+''' as ['+ convert(varchar(50), 字段1) +'],'
from table
set @sql1=left(@sql1,len(@sql1)-1)
set @sql2=left(@sql2,len(@sql2)-1)
set @sql3=left(@sql3,len(@sql3)-1)
exec(@sql1 + ' union ' + @sql2 + ' union ' + @sql3)
end
日期:
Convert(varchar(10),getdate(),120)
这样的方法来实现,青年人网提示其中varchar(10)定义的是你要的字段的长度,当然长度的不同返回的也会不的,如果我们只要日期部分,设成10正好为日期长度,如果设成19则正好可以读到时间部分。
现在介绍最后面代的参数,分别以代的参数及实例效果说明
参数结果
参数 |
结果 |
100 |
05 8 2006 9:27PM |
101 |
05/08/2006 |
102 |
2006.05.08 |
103 |
08/05/2006 |
104 |
08.05.2006 |
105 |
08-05-2006 |
106 |
08 05 2006 |
107 |
05 08, 2006 |
108 |
21:30:51 |
109 |
05 8 2006 9:31:11 |
110 |
05-08-2006 |
111 |
2006/05/08 |
112 |
20060508 |
113 |
08 05 2006 21:31:59 |
114 |
21:33:06:503 |
120 |
2006-05-08 21:33:38 |
另类子查询:
语句:
select *,
(select top 1 grades from T_Assessment where companyid=c.companyid order by AssessmentID desc) as pgrades,
(select (sum(Score) + 60) from T_Honesty where AuditDate>=(select top 1 AssessmentDate from T_Assessment where companyid=c.companyid order by AssessmentID desc) and AuditDate<=getdate() and companyid=c.companyid and Status=2) as Score
from t_company c inner join T_USER u on c.User_ID = u.User_ID where u.UserType=0
责任编辑:小草