简体   繁体   中英

SQL Server 2008R2 - data in perm table but not in temp table

I am not sure why this is happening... the actual table has data yet when trying to pull data from the actual table into the temp table, there is no data...

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

Thanks :)

Firstly, you are not comparing apples with apples. Your test select should include the SAME filter as the INSERT..SELECT query

Secondly, it's best to minimize the time spent on SELECT.. INTO . Better to create the temporary table first.

Thirdly, don't use tempdb.dbo.#Toys . # is implicitly in tempdb. You will get this warning anyway

Database name 'tempdb' ignored, referencing object in tempdb.

The changed portions:

-- 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*';

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