创建存储过程到 drop table 或引用 table 参数
Create stored procedure to a drop table or refer to table with the parameter
我的问题是关于如何在存储过程中引用对象。我想知道这样的事情是否可行:
create proc drop_table @tablename ??
as
drop table @tablename
这样的事情怎么样?
create or alter proc select_col @col_name nvarchar(50), @table_nbame nvarchar(50)
as
select @col_name
from @table_nbame
我可以创建一个变量并在 SP 中引用 table 吗?
传递参数时不能删除 table 而不会出错。但是,如果您可以将错误重定向到再次检查 table 是否存在,您可以获得所需的结果:
create procedure drop_table_smartly
@tablename varchar(20)
as
begin
declare @cmd varchar(50) = (select 'drop table '+ @tablename)
exec (@cmd)
end
所以现在通过使用下面的执行,你可以删除任何 table
exec drop_table_smartly 'droper'
请注意,无论其他人就这些警告您是对的。你应该只在适当的测试后在生产中使用(不要在生产中使用)
我的问题是关于如何在存储过程中引用对象。我想知道这样的事情是否可行:
create proc drop_table @tablename ??
as
drop table @tablename
这样的事情怎么样?
create or alter proc select_col @col_name nvarchar(50), @table_nbame nvarchar(50)
as
select @col_name
from @table_nbame
我可以创建一个变量并在 SP 中引用 table 吗?
传递参数时不能删除 table 而不会出错。但是,如果您可以将错误重定向到再次检查 table 是否存在,您可以获得所需的结果:
create procedure drop_table_smartly
@tablename varchar(20)
as
begin
declare @cmd varchar(50) = (select 'drop table '+ @tablename)
exec (@cmd)
end
所以现在通过使用下面的执行,你可以删除任何 table
exec drop_table_smartly 'droper'
请注意,无论其他人就这些警告您是对的。你应该只在适当的测试后在生产中使用(不要在生产中使用)