Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting Data from a 'WITH' statement

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Jan 26, 2006
392
US
Hello. I have the following query:

Code:
with A as (
select T.pr,T.sa,T.block,T.tlot, r=T.tlot-rank()over(partition by T.pr,T.sa,T.block order by T.tlot)
from (Select pr,CONVERT(INT,lot) tlot,block,sa from lotblock WHERE isnumeric(lot + 'e0') = 1) T
)
select pr,sa,block,MIN(tlot),MAX(tlot)
from A
group by pr,sa,block order by pr,sa,block


The query runs fine, but I am trying to insert the results into another table:

Code:
insert into #temp
with A as (
select T.pr,T.sa,T.block,T.tlot, r=T.tlot-rank()over(partition by T.pr,T.sa,T.block order by T.tlot)
from (Select pr,CONVERT(INT,lot) tlot,block,sa from lotblock WHERE isnumeric(lot + 'e0') = 1) T
)
select pr,sa,block,MIN(tlot),MAX(tlot)
from A
group by pr,sa,block order by pr,sa,block

I keep getting the error: Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'A'.


How do I need to modify this to insert it into a table?

Thanks!

Brian
 
Brian,
It's merely an issue of ordering your statements; the Update or Insert needs to follow the common table expresssion.

Code:
;with A as (SELECT ....)

INSERT into #temp
SELECT * from A

soi là, soi carré
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top