Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
gavakie
Posting Yak Master
221 Posts |
Posted - 2010-10-21 : 10:00:59
|
This is my statement and it runs when I dont use the temp tables but why cant I use the same temp table in each statement. It should just skip the query if it doesnt match the criteria. quote: Declare @Date datetimeSet @Date = (SELECT DATEADD(DD,-7,max(dte))MDATE From smb.dbo.vw_p_date_ref Where FISCAL_MNTH = Case When CASE WHEN Day(getdate()) < 23 then MONTH(getdate()-DAY(getdate()))+1 Else Month(getdate())+1 End = 13 then 1 else CASE WHEN Day(getdate()) < 23 then MONTH(getdate()-DAY(getdate()))+1 Else Month(getdate())+1 End End And FISCAL_YR = Case when MONTH(getdate()) = 12 and DAY(getdate()) > 21 then YEAR(getdate())+1 else YEAR(getdate()) End )IF GETDATE()< @Date Begin Select * Into #Dates From smb.dbo.vw_p_date_ref Where FISCAL_MNTH = (Select FISCAL_MNTH From smb.dbo.vw_p_date_ref Where DTE = CASE WHEN DAY(GETDATE()) = 22 then cast(CAST(GETDATE()-1 as varchar(12)) AS DATETIME) else cast(CAST(GETDATE() as varchar(12)) AS DATETIME)End ) And FISCAL_YR = (Select FISCAL_YR From smb.dbo.vw_p_date_ref Where DTE = CASE WHEN DAY(GETDATE()) = 22 then cast(CAST(GETDATE()-1 as varchar(12)) AS DATETIME) else cast(CAST(GETDATE() as varchar(12)) AS DATETIME)End ) End Else Begin Select * Into #Dates From ( Select * From smb.dbo.vw_p_date_ref Where FISCAL_MNTH = (Select FISCAL_MNTH From smb.dbo.vw_p_date_ref Where DTE = CASE WHEN DAY(GETDATE()) = 22 then cast(CAST(GETDATE()-1 as varchar(12)) AS DATETIME) else cast(CAST(GETDATE() as varchar(12)) AS DATETIME)End ) And FISCAL_YR = (Select FISCAL_YR From smb.dbo.vw_p_date_ref Where DTE = CASE WHEN DAY(GETDATE()) = 22 then cast(CAST(GETDATE()-1 as varchar(12)) AS DATETIME) else cast(CAST(GETDATE() as varchar(12)) AS DATETIME)End ) Union All Select top 7 * From smb.dbo.vw_p_date_ref Where FISCAL_MNTH = (Select FISCAL_MNTH From smb.dbo.vw_p_date_ref Where DTE = CASE WHEN DAY(GETDATE()) = 22 then cast(CAST(GETDATE()-1 as varchar(12)) AS DATETIME) else cast(CAST(GETDATE() as varchar(12)) AS DATETIME)End )+1 And FISCAL_YR = (Select FISCAL_YR From smb.dbo.vw_p_date_ref Where DTE = CASE WHEN DAY(GETDATE()) = 22 then cast(CAST(GETDATE()-1 as varchar(12)) AS DATETIME) else cast(CAST(GETDATE() as varchar(12)) AS DATETIME)End ) )ttTemp End
|
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-21 : 10:15:36
|
BecauseSELECT * INTO TABLENAME FROM ...creates a new table.The next statement will then also create a new table but it already exists created by the first statement. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
gavakie
Posting Yak Master
221 Posts |
Posted - 2010-10-21 : 10:16:50
|
| Any suggestions? |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-21 : 10:23:24
|
Yes.Do a CREATE TABLE first.Then use the normal INSERT statement i.e. INSERT TABLENAME(col1,col2,...) SELECT col1,col2,... FROM ... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
gavakie
Posting Yak Master
221 Posts |
Posted - 2010-10-21 : 10:27:11
|
| webfred your the man. Thanks!!! |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-21 : 10:28:33
|
welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|