I have the following SQL query
SELECT MAX(CP2.CounterpartyKey)
FROM GFRM_STAR2.SDS.Counterparty CP2
WHERE CP1.Id = CP2.Id
AND ISNULL(CP2.EffectiveFromDate, '1900-01-01') <= CONVERT(DATE, '20170620', 112)
This is a sub query inside another SQL query, but the rest isn't changing so I have omitted it for this question
What I'm trying to do is change this is move the data I get from CP2.CounterpartyKey into a temp table then do an inner join instead of what I'm doing now.
To begin with I've amended the query to be
SELECT MAX(CP2.CounterpartyKey)
INTO #Temp
FROM GFRM_STAR2.SDS.Counterparty CP2
WHERE CP1.Id = CP2.Id
AND ISNULL(CP2.EffectiveFromDate, '1900-01-01') <= CONVERT(DATE, '20170620', 112)
But when I run, I get the following error:
Incorrect syntax near the keyword 'INTO'
I'm new to SQL and I'm unsure why this is failing as looking around, my syntax looks to correct, but clearly I'm doing something wrong.
Use this instead:
INSERT INTO #Temp
SELECT MAX(CP2.CounterpartyKey)
FROM GFRM_STAR2.SDS.Counterparty CP2
WHERE CP1.Id = CP2.Id
...
Alias Column name , because temp table needs a column name where you will be inserting into
SELECT MAX(CP2.CounterpartyKey) as CounterPartKey
into #Temp
FROM GFRM_STAR2.SDS.Counterparty CP2
WHERE CP1.Id = CP2.Id
AND ISNULL(CP2.EffectiveFromDate, '1900-01-01') <= CONVERT(DATE, '20170620', 112)
Before this Query you should have a command to
create table #Temp (CounterPartyMax int)
and after you are done be sure to delete with
drop table #Temp
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.