1

我不确定为什么会发生这种情况......实际表有数据但是当试图从实际表中提取数据到临时表时,没有数据......

IF object_id('DI_Temp.di.Holly') IS NOT NULL 
   DROP TABLE DI_Temp.di.Holly

CREATE TABLE DI_Temp.di.Holly
(
Toys varchar(20)
,Meals varchar(100)
,Breaks varchar(20)
)

Insert DI_Temp.DI.Holly 
   Select 
      Toys, Meals, Breaks
   from toysarefun.dbo.crazytoys (nolock)
   where status = 'active'

--Select * from di_temp.di.holly

IF OBJECT_ID('tempdb.dbo.#Toys') IS NOT NULL 
   DROP TABLE tempdb.dbo.#Toys

SELECT * 
INTO #Toys
FROM DI_Temp.di.Holly (NOLOCK) 
WHERE Toys like 'H*'

Select *
from tempdb.dbo.#Toys

谢谢 :)

4

1 回答 1

4

首先,您没有将苹果与苹果进行比较。您的测试选择应包含与 INSERT..SELECT 查询相同的过滤器

其次,最好尽量减少花在SELECT.. INTO. 最好先创建临时表。

第三,不要使用tempdb.dbo.#Toys. # 隐含在 tempdb 中。无论如何,您都会收到此警告

忽略数据库名称“tempdb”,引用 tempdb 中的对象。

更改的部分:

-- uncomment first to check if there are any matches
--Select * from di_temp.di.holly WHERE Toys like 'H*'

IF OBJECT_ID('tempdb.dbo.#Toys') IS NOT NULL 
DROP TABLE #Toys;

SELECT *
INTO #Toys
FROM DI_Temp.di.Holly (NOLOCK) 
WHERE 1=0;

INSERT INTO #Toys
SELECT * 
FROM DI_Temp.di.Holly (NOLOCK) 
WHERE Toys like 'H*';
于 2013-05-16T22:59:30.773 回答