简体   繁体   中英

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

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

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

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 ):

You cannot reference a synonym in a DDL statement

And any statement that creates a table, including SELECT INTO counts as a DDL statement. (See here: http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1085a84c-faac-4d26-8f35-c64d8b901034/insert-into-is-sentence-dml-or-ddl )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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