关于SQL存储过程的怪问题,请大家帮忙!

sendy_cy 2004-09-07 03:15:58
错误提示:必须声明变量 '@tbstr'
目的:通过传递参数选择不同表,再进行处理
CREATE PROCEDURE test
@tbstr varchar(30)=''
AS
DECLARE test_cursor CURSOR FOR
select * from @tbstr
open test_cursor
--'''''
CLOSE test_cursor
DEALLOCATE test_cursor
GO
...全文
220 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenhaohf 2004-09-07
  • 打赏
  • 举报
回复
表名用变量必须写成 exec ('select * from @table')
jlp2004 2004-09-07
  • 打赏
  • 举报
回复
使用临时表变量啊,
CREATE PROCEDURE test
@tbstr varchar(30)=''
AS
create table #t table(c1 int,c2 varchar(50))
declare @sql varchar(1000)
set @sql='select * from '+ @tbstr
into into #t exec @sql
DECLARE test_cursor CURSOR FOR
select * from #t
open test_cursor
--'''''
CLOSE test_cursor
DEALLOCATE test_cursor
GO
不过临时表不推荐用,自己拿主义
qimini 2004-09-07
  • 打赏
  • 举报
回复
CREATE PROCEDURE test
@tbstr varchar(30)=''
AS
declare @SQL nvarchar(3000)
declare @ParameterDefinition varchar(30)

set @SQL = N'DECLARE test_cursor CURSOR FOR select * from '+ @tbstr + N'
open test_cursor

--....

CLOSE test_cursor
DEALLOCATE test_cursor'

exec sp_executesql @SQL

if @@Error <>''
return 1
else
return 0
GO
dofly 2004-09-07
  • 打赏
  • 举报
回复
CREATE PROCEDURE test
@tbstr varchar(30)=''
AS
Declare @sql VARCHAR(200)
SQL = 'select * from '+ @tbstr
exec( SQL ) --这样子会有返回结果,但是不能做数据源

游标的资料来源不能象你那么写,
建议你换个方式去做。
为什么你不把这个写到代码里面,
如果写到代码里面,
倒是可以根据不同的表去做的
sendy_cy 2004-09-07
  • 打赏
  • 举报
回复
问题:在关键字 'exec' 附近有语法错误
CREATE PROCEDURE test
@tbstr varchar(30)=''
AS
DECLARE test_cursor CURSOR FOR
exec('select * from '+ @tbstr)
open test_cursor
--'''''
CLOSE test_cursor
DEALLOCATE test_cursor
GO
Eddie005 2004-09-07
  • 打赏
  • 举报
回复
目的:通过传递参数选择不同表,再进行处理
mission imposable ~~~ to me ~~
what about others?
VCILOVE 2004-09-07
  • 打赏
  • 举报
回复
对,就是直接执行语句“EXEC 你的存储过程名 你的参数1,你的参数2”就可以了
dofly 2004-09-07
  • 打赏
  • 举报
回复
要实现你的功能,表名做参数传递进去
须把传递进去的参数当做字符串处理
然后 exec(字符串)才可行,
就算传进去了,做游标的话,也很麻烦
没有试过,不知是否可行
llthero 2004-09-07
  • 打赏
  • 举报
回复
CREATE PROCEDURE test
@tbstr varchar(30)=''
AS
DECLARE test_cursor CURSOR FOR
exec('select * from '+ @tbstr)
open test_cursor
--'''''
CLOSE test_cursor
DEALLOCATE test_cursor
GO
llthero 2004-09-07
  • 打赏
  • 举报
回复

select * from @tbstr
这条语句不能这样写,在SQL中不能直接执行

Exec('select * from'+@tbstr)

16,556

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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