运行存储过程报这个错误,为什么?

fanxiaolin84 2013-12-14 07:02:14
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' 附近有语法错误。
...全文
144 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
人鱼传说 2013-12-15
  • 打赏
  • 举报
回复
要确认一下你的抄表时间是不带时间的吧,如果带时间,写法就有一点不同了
人鱼传说 2013-12-15
  • 打赏
  • 举报
回复

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'
唐诗三百首 2013-12-14
  • 打赏
  • 举报
回复
try this,

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 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

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧