将表名作为参数的问题

coolnick 2005-06-02 06:30:18
在下面的程序中,我想将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


...全文
57 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolnick 2005-06-02
  • 打赏
  • 举报
回复
我将程序改成如下,执行时没有语法错误,也能打印出
'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)
coolnick 2005-06-02
  • 打赏
  • 举报
回复
晕,楼上两位说的我早就试过,不行的,
我用的两个地方比较特殊
yjdn 2005-06-02
  • 打赏
  • 举报
回复
给你写个例子,自己改改吧
declare @tn varchar(20)
declare @sql varchar(8000)
set @tn='tb'
exec('select * from '+@tn)
yjdn 2005-06-02
  • 打赏
  • 举报
回复
以表名作为参数就得用exce

27,580

社区成员

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

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