存储过程运行报在应使用条件的上下文(在 ')' 附近)中指定了非布尔类型的表达式的错误
alter PROCEDURE [dbo].[Sourcefenxi](@dDate datetime,@BuilldNo nvarchar(20))
AS
begin
declare @Sql nvarchar(max)
declare @tableName nvarchar(100)
declare @tableName2 nvarchar(100)
--选择最适用的数据表
set @tableName = 'dbo.TMETER' + CAST(datepart(YYYY,@dDate) as varchar(4))
set @tableName2 = 'dbo.TELECTRI' + CAST(datepart(YYYY,@dDate) as varchar(4))
--拼接查询语句
set @Sql = N'
select c.AREAGUID,c.BUILDNO,c.BUILDNAME,累计耗电量,当日耗电,电单耗,累计耗水量,当日耗水量,水单耗,累计耗热,计划供热,单日供热量,热量单耗,抄表时间 from TBUILD as c,(
select f.AREAGUID ,f.BUILDNO,累计耗电量,当日耗电,电单耗,累计耗水量,当日耗水量,水单耗,f.METERNLRL as 累计耗热 ,f.METERJHGR as 计划供热,f.METERDAN as 单日供热量,f.METERRLDH as 热量单耗, d.抄表时间 from '+@tableName+' as f,
(select a.AREAGUID,a.buildno,a. METERNLLJ as 累计耗电量,a.METERDAY as 当日耗电 ,a.METERDAN as 电单耗,b.METERNLLJ as 累计耗水量,b.METERDAY as 当日耗水量,b.METERDAN as 水单耗,b.DDATE as 抄表时间
from '+@tableName2+' as a inner join (select e.BUILDNO,e.AREAGUID,e.DDATE,e.DEVICETYPE, e.METERNLLJ,e.METERDAY,e.METERDAN from '+@tableName2+' as e where e.DEVICETYPE=11)as b
on a.DDate=b.DDate and a.AREAGUID=b.AREAGUID and a.BUILDNO=b.BUILDNO and a.DEVICETYPE=10
and convert(varchar(20),a.DDate,23))='''+ convert(varchar(20),@dDate,23) +''' and a.BUILDNO='+@BuilldNo+'
)d
where f.AREAGUID=d.AREAGUID and f.BUILDNO=d.BUILDNO and f.DDATE=d.抄表时间 and CONVERT(varchar(20),f.DDate,112)='''+convert(varchar(20),@dDate,23)+''' and f.BUILDNO='+@BuilldNo+')as g
where c.AREAGUID=g.AREAGUID and c.BUILDNO=g.BUILDNO
'
exec(@Sql)
end;
GO
报 在应使用条件的上下文(在 ')' 附近)中指定了非布尔类型的表达式。的错误
------解决方案--------------------declare @dDate datetime,@BuilldNo nvarchar(20)
set @dDate='2013-12-12'
set @BuilldNo='A0001'
declare @Sql nvarchar(max)
declare @tableName nvarchar(100)
declare @tableName2 nvarchar(100)
&