日期:2014-05-18  浏览次数:20411 次

sql 2000存储过程中表名改作变量
我有1存储过程,能正常调用

CREATE PROCEDURE cxgl_lsb 
@qrq smalldatetime,
@zrq smalldatetime
AS
if object_id('[fgsgl_sb]') is not null drop table [fgsgl_sb]
create table [fgsgl_sb]([XRQ] smalldatetime,[RGL] numeric(18, 0),[JCMC] varchar(20))
insert fgsgl_sb (xrq,rgl,jcmc) 
select Convert(Varchar(10),xrq,120) as xrq,sum(rgl) as Rgl,cx from jqld2012 where xrq>=@qrq and xrq<=@zrq group by xrq,cx
GO

但要把表名“jqld2012”换做变量名@biao
如何改动

------解决方案--------------------
try
SQL code
CREATE PROCEDURE cxgl_lsb  
@qrq smalldatetime,
@zrq smalldatetime,
@biao varchar(50)
AS
declare @sql varchar(8000)
if object_id('[fgsgl_sb]') is not null drop table [fgsgl_sb]
create table [fgsgl_sb]([XRQ] smalldatetime,[RGL] numeric(18, 0),[JCMC] varchar(20))

set @sql='insert fgsgl_sb (xrq,rgl,jcmc)  
select Convert(Varchar(10),xrq,120) as xrq,sum(rgl) as Rgl,cx 
from '+@biao
+' where xrq>='''+convert(varchar(10),@qrq,120)+''' and xrq<='''+convert(varchar(10),@zrq,120)+''' 
group by xrq,cx'
exec (@sql)
GO

------解决方案--------------------
探讨
try

SQL code
CREATE PROCEDURE cxgl_lsb
@qrq smalldatetime,
@zrq smalldatetime,
@biao varchar(50)
AS
declare @sql varchar(8000)
if object_id('[fgsgl_sb]') is not null drop table [fgsgl_sb]
……