简体   繁体   English

在SQL Server中使用同义词创建新表

[英]Creating a new table using a synonym in SQL Server

I have a synonym created for this table ParaPITD.dbo.BM_00_PolicyMaster that points to ParaPITDYYYYMM.dbo.BM_00_PolicyMaster 我为此表ParaPITD.dbo.BM_00_PolicyMaster创建了一个同义词,它指向ParaPITDYYYYMM.dbo.BM_00_PolicyMaster

There is no table yet - so it acts as a place holder. 还没有桌子-因此它充当占位符。

I have a stored procedure that creates a temp table #BM_00_PolicyMaster and now I want to insert or select into the place holder where the synonym points to and create the table in ParaPITDYYYYMM.dbo.BM_00_PolicyMaster 我有一个创建临时表#BM_00_PolicyMaster的存储过程,现在我想在同义词指向的占位符中插入或选择并在ParaPITDYYYYMM.dbo.BM_00_PolicyMaster创建表

If I run this: 如果我运行此命令:

select * 
into ParaPITD.dbo.BM_00_PolicyMaster
from #BM_00_PolicyMaster

it creates the table in ParaPITD.dbo.BM_00_PolicyMaster and ignores the synonym 它在ParaPITD.dbo.BM_00_PolicyMaster创建表,并忽略同义词

If I run this: 如果我运行此命令:

INSERT INTO ParaPITD.dbo.BM_00_PolicyMaster
   SELECT * 
   FROM #BM_00_PolicyMaster 

it gives me an invalid object error acting like the table must exist before it will insert. 它给我一个无效的对象错误,就像在插入表之前必须存在该表一样。

Appreciate any help - thanks 感谢任何帮助-谢谢

From the doc ( http://technet.microsoft.com/en-us/library/ms187552.aspx ): 从文档( http://technet.microsoft.com/en-us/library/ms187552.aspx ):

You cannot reference a synonym in a DDL statement 您不能在DDL语句中引用同义词

And any statement that creates a table, including SELECT INTO counts as a DDL statement. 创建表的任何语句(包括SELECT INTO计为DDL语句。 (See here: http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1085a84c-faac-4d26-8f35-c64d8b901034/insert-into-is-sentence-dml-or-ddl ) (请参见此处: http : //social.msdn.microsoft.com/Forums/sqlserver/en-US/1085a84c-faac-4d26-8f35-c64d8b901034/insert-into-is-sentence-dml-or-ddl

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

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