繁体   English   中英

SQL Server 2012中的alter命令

[英]alter command in sql server 2012

我试图通过向需要创建临时表的现有视图添加新列来更改视图。

现在我无法创建临时表,因为它说

 'ALTER VIEW' must be the first statement in a query batch.

我的代码段看起来像

 declare @temp_table table (x int,y xml,z int)
   insert into @temp_table (x,y,z)
      select(t.loc.value('(text()[1])','nvarchar(max)') from table1 
      Cross Apply ExtensionData.nodes('/xml/xml')  AS  t(Loc)
      where t.loc.value(@id,smallint)=1)

 Alter dbo.myview
  as 
  select
   existingcolumns,
   newcolumn from 
   existing_table et left join 
   temp_table tt on et.x=tt.x

请告知最佳方法。 我有删除视图并重新创建视图的想法,但是理想情况下,这不是最好的方法。

我认为您不能在视图中使用临时表,因为视图的寿命比临时表长。

文档中 ,它说:“视图定义中的SELECT子句不能包含以下内容:(...)对临时表或表变量的引用。”

您需要一个GO

declare @temp_table table (x int,y xml,z int)
   insert into @temp_table (x,y,z)
      select(t.loc.value('(text()[1])','nvarchar(max)') from table1 
      Cross Apply ExtensionData.nodes('/xml/xml')  AS  t(Loc)
      where t.loc.value(@id,smallint)=1)

GO

 Alter dbo.myview
  as 
  select
   existingcolumns,
   newcolumn from 
   existing_table et left join 
   temp_table tt on et.x=tt.x 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM