运行存储过程报这个错误,为什么?
ALTER PROCEDURE [dbo].[Sourcefenxi1](@dDate datetime,@BuilldNo nvarchar(20))
AS
begin
declare @Sql nvarchar(max)
declare @tableName nvarchar(100)
declare @tableName2 nvarchar(100)
declare @sqlwhere 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 @sqlwhere=convert(varchar(20), @dDate)
--拼接查询语句
set @Sql = N'
select c.AREAGUID,c.BUILDNO,c.BUILDNAME,累计耗电量,当日耗电,电单耗,累计耗水量,当日耗水量,水单耗,累计耗热,计划供热,单日供热量,热量单耗,抄表时间 from TBUILD c,(
select f.AREAGUID ,f.BUILDNO,累计耗电量,当日耗电,电单耗,累计耗水量,当日耗水量,水单耗,f.METERNLRL as 累计耗热 ,f.METERJHGR as 计划供热,f.METERDAN as 单日供热量,f.METERRLDH as 热量单耗, d.抄表时间 from '+@tableName+' 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+' a inner join (select e.BUILDNO,e.AREAGUID,e.DDATE,e.DEVICETYPE, e.METERNLLJ,e.METERDAY,e.METERDAN from '+@tableName2+' e where e.DEVICETYPE=11) b
on a.DDate=b.DDate and a.AREAGUID=b.AREAGUID and a.BUILDNO=b.BUILDNO and a.DEVICETYPE!=b.DEVICETYPE
and convert(varchar(20),a.DDate,23)='+@sqlwhere+' 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)='+@sqlwhere+' and f.BUILDNO='+@BuilldNo+') g
where c.AREAGUID=g.AREAGUID and c.BUILDNO=g.BUILDNO
'
exec(@Sql)
end;
存储过程输入2个参数@dDate=2013-10-16 ,@BuilldNo=132
报错误
消息 102,级别 15,状态 1,第 7 行
'16' 附近有语法错误。
[解决办法]
改成这样试试:
alter PROCEDURE [dbo].[Sourcefenxi1](@dDate datetime,@BuilldNo nvarchar(20))
AS
begin
declare @Sql nvarchar(max)
declare @tableName nvarchar(100)
declare @tableName2 nvarchar(100)
declare @sqlwhere 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 @sqlwhere=convert(varchar(10), @dDate,120)
--拼接查询语句
set @Sql = N'
select c.AREAGUID,c.BUILDNO,c.BUILDNAME,累计耗电量,当日耗电,电单耗,累计耗水量,当日耗水量,水单耗,累计耗热,计划供热,单日供热量,热量单耗,抄表时间 from TBUILD c,(
select f.AREAGUID ,f.BUILDNO,累计耗电量,当日耗电,电单耗,累计耗水量,当日耗水量,水单耗,f.METERNLRL as 累计耗热 ,f.METERJHGR as 计划供热,f.METERDAN as 单日供热量,f.METERRLDH as 热量单耗, d.抄表时间 from '+@tableName+' 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+' a inner join (select e.BUILDNO,e.AREAGUID,e.DDATE,e.DEVICETYPE, e.METERNLLJ,e.METERDAY,e.METERDAN from '+@tableName2+' e where e.DEVICETYPE=11) b
on a.DDate=b.DDate and a.AREAGUID=b.AREAGUID and a.BUILDNO=b.BUILDNO and a.DEVICETYPE!=b.DEVICETYPE
and convert(varchar(20),a.DDate,23)='''+@sqlwhere+''' 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)='''+@sqlwhere+''' and f.BUILDNO='+@BuilldNo+') g
where c.AREAGUID=g.AREAGUID and c.BUILDNO=g.BUILDNO
'
--exec(@Sql)
print @Sql
end;
exec [Sourcefenxi1] @dDate='2013-10-16' ,@BuilldNo=132
ALTER PROCEDURE [dbo].[Sourcefenxi1]
(@dDate datetime,@BuilldNo nvarchar(20))
AS
begin
declare @Sql nvarchar(max)
declare @tableName nvarchar(100)
declare @tableName2 nvarchar(100)
declare @sqlwhere 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 @sqlwhere=convert(varchar(20),@dDate,23)
--拼接查询语句
set @Sql = N'
select c.AREAGUID,c.BUILDNO,c.BUILDNAME,累计耗电量,当日耗电,电单耗,累计耗水量,当日耗水量,水单耗,累计耗热,计划供热,单日供热量,热量单耗,抄表时间 from TBUILD c,(
select f.AREAGUID ,f.BUILDNO,累计耗电量,当日耗电,电单耗,累计耗水量,当日耗水量,水单耗,f.METERNLRL as 累计耗热 ,f.METERJHGR as 计划供热,f.METERDAN as 单日供热量,f.METERRLDH as 热量单耗, d.抄表时间 from '+@tableName+' 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+' a inner join (select e.BUILDNO,e.AREAGUID,e.DDATE,e.DEVICETYPE, e.METERNLLJ,e.METERDAY,e.METERDAN from '+@tableName2+' e where e.DEVICETYPE=11) b
on a.DDate=b.DDate and a.AREAGUID=b.AREAGUID and a.BUILDNO=b.BUILDNO and a.DEVICETYPE!=b.DEVICETYPE
and convert(varchar(20),a.DDate,23)='''+@sqlwhere+''' 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)='+@sqlwhere+' and f.BUILDNO='+@BuilldNo+') g
where c.AREAGUID=g.AREAGUID and c.BUILDNO=g.BUILDNO
'
exec(@Sql)
end;
-- 执行
exec [dbo].[Sourcefenxi1] @dDate='2013-10-16',@BuilldNo=N'132'
ALTER PROCEDURE [dbo].[Sourcefenxi1]
(@dDate varchar(8),@BuilldNo nvarchar(20))
AS
set nocount on
declare @Sql nvarchar(max)
declare @tableName nvarchar(100)
declare @tableName2 nvarchar(100)
declare @sqlwhere 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 @sqlwhere=@dDate
--拼接查询语句
set @Sql = N'
select c.AREAGUID,c.BUILDNO,c.BUILDNAME,累计耗电量,当日耗电,电单耗,累计耗水量,当日耗水量,水单耗,累计耗热,计划供热,单日供热量,热量单耗,抄表时间 from TBUILD c,(
select f.AREAGUID ,f.BUILDNO,累计耗电量,当日耗电,电单耗,累计耗水量,当日耗水量,水单耗,f.METERNLRL as 累计耗热 ,f.METERJHGR as 计划供热,f.METERDAN as 单日供热量,f.METERRLDH as 热量单耗, d.抄表时间 from '+@tableName+' 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+' a inner join (select e.BUILDNO,e.AREAGUID,e.DDATE,e.DEVICETYPE, e.METERNLLJ,e.METERDAY,e.METERDAN from '+@tableName2+' e where e.DEVICETYPE=11) b
on a.DDate=b.DDate and a.AREAGUID=b.AREAGUID and a.BUILDNO=b.BUILDNO and a.DEVICETYPE!=b.DEVICETYPE
and a.DDate='''+@sqlwhere+''' and a.BUILDNO='''+@BuilldNo+'''
)d
where f.AREAGUID=d.AREAGUID and f.BUILDNO=d.BUILDNO and f.DDATE=d.抄表时间 and f.DDate='+@sqlwhere+' and f.BUILDNO='+@BuilldNo+') g
where c.AREAGUID=g.AREAGUID and c.BUILDNO=g.BUILDNO
'
exec(@Sql)
-- 执行
exec [dbo].[Sourcefenxi1] @dDate='20131016',@BuilldNo=N'132'