将表名作为参数的问题
在下面的程序中,我想将table1,table2作为参数传进去,
也就是@tableName1,@tableName2,不知如何实现,请指点!
create procedure sp_Test
@tableName1 varchar(100),
@tableName2 varchar(100)
as
begin
if exists(select name from sysobjects where name='table1' and type='U')
drop table table1 --question1
declare cur_Test cursor for
select * from table2 --question2
.....
end
go
问题点数:20、回复次数:4Top
1 楼yjdn(人形机器)回复于 2005-06-02 18:33:59 得分 10
以表名作为参数就得用exceTop
2 楼yjdn(人形机器)回复于 2005-06-02 18:35:41 得分 10
给你写个例子,自己改改吧
declare @tn varchar(20)
declare @sql varchar(8000)
set @tn='tb'
exec('select * from '+@tn)Top
3 楼coolnick(林息)回复于 2005-06-02 21:52:41 得分 0
晕,楼上两位说的我早就试过,不行的,
我用的两个地方比较特殊Top
4 楼coolnick(林息)回复于 2005-06-02 22:31:37 得分 0
我将程序改成如下,执行时没有语法错误,也能打印出
'The source table aaaa does not exists.' 但return却没有起作用,
程序还是继续执行,请指点!
select @strSql='if not exists(select name from sysobjects where name=''' + @srcTableName + ''' and type=''U'')'
select @strSql=@strSql + char(13) + ' begin ' +char(13)+ ' print ''The source table ' + @srcTableName + ' does not exist.'''+char(13)+' return '+char(13)+' end'
print @strSql
exec(@strSql)Top




