写了个存储过程,但是遇到变量与SQL语句连接的问题,导致不能执行,恳求大家帮忙!!!!!

windspeed 2006-04-11 10:17:29
CREATE PROCEDURE dbo.InvSum_Prc

(
@invChkMainFid numeric(18,0) ,
@fdept numeric(18,0),
@fcustomer numeric(18,0),
@fmarket numeric(18,0),
@finvTime DateTime, --盘点日期
@fpcode varchar(20), --产品大类编码
@qSql varchar(50), --查询条件
@fzt numeric(9,0), --帐套
@find numeric(9,0), --产业
@xed numeric(9,0)

)

AS



begin

SELECT FPRODUCT,PRODUCTNAME,SUM(FSUM) AS FACVAL, 0 as FADJVAL,'' as FADJWHYS FROM (

select b.fproduct,b.productname,sum(b.fnum) as fsum from outorder_list_static a left join
outorder_de_static b on a.fid=b.fid inner join zt@fztsysinfo.DBO.PRODUCT_LIST c on c.fid=b.fproduct
and c.xed=1 and c.fend=1 and c.fcode like @fpcode where a.xed=1 and a.findustry=@find and a.fdept=@fdept
and a.fcustomer=@fcustomer and a.etime between @finvTime and getdate() group by b.fproduct,b.productname

union all

select a.fproduct,a.fproductname,sum(-a.fqty) as fsum from tblsales a inner join
zt@fztsysinfo.DBO.PRODUCT_LIST c on c.fid=a.fproduct and c.xed=1 and c.fend=1
and c.fcode like @fpcode where a.xed=1 and a.fbrand='普多' and a.fdept=@fdept and a.fmarket=@fmarket
and a.fdate between @fInvTime and getdate() group by a.fproduct,a.fproductname

union all

SELECT a.fproduct,a.fproductname,a.finvsum as fsum FROM InvCheck_Static a
inner join zt@fztsysinfo.DBO.PRODUCT_LIST c on c.fid=a.fproduct and c.xed=1 and c.fend=1 and c.fcode like @fpcode
WHERE a.XED=1 AND a.FMARKET=@fmarket AND fopmon=(datepart(month,getdate())-1)

union all

select b.fproduct,b.fproductname, sum(b.fadjval) as fsum from invcheck_main
a inner join invcheck_detail b on a.fid=b.forderid and a.xed=1 and a.fmarket=@fmarket
and a.finvtime between @finvTime and datepart(year,getdate())+'-'+datepart(month,getdate())+'-01'
inner join zt@fztsysinfo.DBO.PRODUCT_LIST c on c.fid=b.fproduct and c.xed=1
and c.fend=1 and c.fcode like @fpcode where a.fid=@invChkMainFid and a.xed=@xed
group by b.fproduct,b.fproductname

) aaa group by fproduct,productname


end

RETURN


GO




请大家注意

zt@fztsysinfo.DBO.PRODUCT_LIST


我的目的是根据 @fzt 来 组成 库的名称

比如 zt1sysinfo.dbo.product_list

但调试通不过,使用 zt+''+@fzt+''+sysinfo.dbo.product_list 也不行


...全文
105 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovcal 2006-04-12
  • 打赏
  • 举报
回复
也就是你的zt+@fzt+sysinfo.dbo.product_list 先赋值到一个sysname变量
然后使用exec()来执行动态T-SQL语句
lovcal 2006-04-12
  • 打赏
  • 举报
回复
在使用表名(库名)时,不能这样使用
如下:
declare @tbname sysname
set @tbname='sysobjects'
select * from @tbname
---------------------------------这样是不行的

必须使用动态T-SQL语句
declare @tbname sysname
set @tbname='sysobjects'
exec('select * from '+@tbname)

你再改改看看
windspeed 2006-04-11
  • 打赏
  • 举报
回复
在线求助~

27,579

社区成员

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

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