-6
create procedure <procedure>
(
    @tbl as varchar(50)='product',
    @col as varchar(50)='proid'
)
as 
begin
    select @col from @tbl 
end
4

1 回答 1

2

因为默认情况下您不能使用参数化的表名或列名。你可以做到这一点的唯一方法是在字符串中创建动态 SQL,然后执行该字符串。例如,

create procedure <procedure>
(
    @tbl as varchar(50)='product',
    @col as varchar(50)='proid'
)
as 
begin

   Declare @SQL VarChar(1000)

   SET @SQL = 'select ' + @col + ' from ' + @tbl 
   Exec ( @SQL)
end
于 2013-01-11T14:27:57.550 回答