从变量到文本

时间:2012-03-27 17:01:42

标签: sql variables views procedure declare

我有这个

@mutation
@name

我要做的是让@mutation查找产品名称(name.products),然后@name@mutation中产品的名称作为文本。所以我要做的是在产品表中获取产品的名称,并在以后使用它来命名视图。

ALTER PROCEDURE [dbo].[exec_view]
AS
BEGIN

DECLARE @mutation NVARCHAR(255);
DECLARE @name NVARCHAR(255) ;


SET @mutation = N'SELECT 
name 
FROM INSERTED

' ;


SET @test = N'@mutatation varchar' ;

EXECUTE sp_executesql @name = @mutation ;



EXEC (
'
CREATE VIEW @name
AS SELECT * 
FROM view_for_future_mutation
');

END

1 个答案:

答案 0 :(得分:0)

declare @name sysname;

set @name = (select quotename(name) from inserted);

exec (N'create view ' + @name + N' as select * from view_for_future_mutation');