当前位置: 代码迷 >> ASP.NET >> 存储过程中如何使用字符串参数
  详细解决方案

存储过程中如何使用字符串参数

热度:6589   发布时间:2013-02-25 00:00:00.0
存储过程中怎么使用字符串参数?
我想用字符串传递表名:

create   proc   [dbo].[DeleteMulti](@forumID   int,   @tableName   char(50))
AS
...

if   (Exists(Select   ID   from   @tableName   where   ParentID=@temp   and   ForumID=@forumID))
begin
select   @delID=ID   from   @tableName   Where   ParentID=@delID     and   ForumID=@forumID
set   @temp   =   @delID
end
...

为什么老说我没有声明@tableName变量呢?
@tableName不是传入的参数吗?


------解决方案--------------------------------------------------------
不能这样做的.应该用动态sql
------解决方案--------------------------------------------------------
不能这样做的.

应该用动态sql
对的 表明不能传 必须拼!
------解决方案--------------------------------------------------------
Select ID from @tableName where ParentID=@temp and ForumID=@forumID

===============

FROM 字句中 表名 不支持使用参数
------解决方案--------------------------------------------------------
可以传递字符串的,用string传递
把你的代码全部贴上吧
------解决方案--------------------------------------------------------
CREATE PROCEDURE PROC_TEST (@TableName varchar(50)) AS exec( ' select Name from '+ @TableName + 'where ParentID <> 0 ') GO
------解决方案--------------------------------------------------------
只能帮顶了!!
  相关解决方案