请高手看一下SQLServer2000帮助中这段话是什么意思?

cqjiang 2005-11-11 09:09:08
@parameter

过程中的参数。在 CREATE PROCEDURE 语句中可以声明一个或多个参数。用户必须在执行过程时提供每个所声明参数的值(除非定义了该参数的默认值)。存储过程最多可以有 2.100 个参数。

使用 @ 符号作为第一个字符来指定参数名称。参数名称必须符合标识符的规则。每个过程的参数仅用于该过程本身;相同的参数名称可以用在其它过程中。默认情况下,参数只能代替常量,而不能用于代替表名、列名或其它数据库对象的名称。

问题:
"默认情况下,参数只能代替常量..."
在哪种情况下参数可以用作表名呢?

因为我要在存储过程中创建物理表,所以要用到下面的存储过程,但下面的存储过程中有几处是关于这个错误的。


if exists (select 1
from sysobjects
where name = 'NcNetCreateTab'
and type = 'P')
drop procedure NcNetCreateTab
go



create procedure NcNetCreateTab
@sname varchar(32)
WITH RECOMPILE
as
declare @fkname varchar(40),@pkname varchar(40)
begin

select @pkname='pk_'+@sname
select @fkname='fk_'+@sname

create table @sname (
srvid int not null,
groupid int not null,
dataid int not null,
dtime datetime not null,
lval int null,
dblval double precision null,
sval varchar(16) null,
constraint @pkname primary key (srvid, groupid, dataid, dtime)
)
go

alter table @fkname
add constraint @fkname foreign key (srvid, dataid, groupid)
references datadef (srvid, id, groupid)
go

end
go



...全文
85 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqjiang 2005-11-11
  • 打赏
  • 举报
回复
谢谢aw511(点点星灯)马上结贴给分
点点星灯 2005-11-11
  • 打赏
  • 举报
回复
declare @tabname varchar(20)
set @tabname='BOOKS'
exec('select * from '+@tabname)
点点星灯 2005-11-11
  • 打赏
  • 举报
回复
if exists (select 1
from sysobjects
where name = 'NcNetCreateTab'
and type = 'P')
drop procedure NcNetCreateTab
go

create procedure NcNetCreateTab
@sname varchar(32)
WITH RECOMPILE
as
declare @fkname varchar(40),@pkname varchar(40)
begin

select @pkname='pk_'+@sname
select @fkname='fk_'+@sname

exec('create table '+@sname+' (
srvid int not null,
groupid int not null,
dataid int not null,
dtime datetime not null,
lval int null,
dblval double precision null,
sval varchar(16) null,
constraint @pkname primary key (srvid, groupid, dataid, dtime)
)')
go

exec ('alter table '+@fkname+'
add constraint @fkname foreign key (srvid, dataid, groupid)
references datadef (srvid, id, groupid)')
go

end
go

27,579

社区成员

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

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