0

Question: How to Insert Complicated select data Into temp Table in sql 2012

select ROW_NUMBER() OVER(order by ppt.type) as Item_code,
ppt.type type,
...,
...,
...,
'11/02/19 09:51' Created_dt
from product psi
inner join [DB1]..items ios on ios.icode=psi.icode
inner join [DB2]..types ppt on ppt.type=ios.type

I have Tried following solution

select * into #temptable from
(select ROW_NUMBER() OVER(order by ppt.type) as Item_code,
ppt.type type,
...,
...,
...,
'11/02/19 09:51' Created_dt
from product psi
inner join [DB1]..items ios on ios.icode=psi.icode
inner join [DB2]..types ppt on ppt.type=ios.type)

I got following Error

Incorrect syntax near ')'.

When I am Normally running select statement i am getting expected data

Dale K
28.2k15 gold badges59 silver badges85 bronze badges
asked Mar 7, 2019 at 6:26
6
  • Created_dt, (comma should not be here) Commented Mar 7, 2019 at 6:30
  • in real query there is no comma i have edited check again Commented Mar 7, 2019 at 6:31
  • So, this is "fix my code, but I'm not going to show you my code"? Commented Mar 7, 2019 at 6:32
  • @Damien_The_unbeliever why Commented Mar 7, 2019 at 6:33
  • Well, so far someone's found one issue with the code, but it's with the "fake query" in the question, not the "real query" which you have actual problems with. There's an obvious extra ) at the end of your second query that isn't present in the first, but who knows whether that's relevant to your actual problem... Commented Mar 7, 2019 at 6:34

2 Answers 2

2

The following code is correct as far as syntax goes:

SELECT *
INTO #Temptable
FROM
(
 SELECT ROW_NUMBER() OVER(ORDER BY Ppt.Type) AS Item_Code,
 Ppt.Type AS Type,
 '11/02/19 09:51' AS Created_Dt
 FROM Product AS Psi
 INNER JOIN Db1..Items AS Ios ON Ios.Icode = Psi.Icode
 INNER JOIN Db2..Types AS Ppt ON Ppt.Type = Ios.Type );

Normally, you could capture the logic in a CTE and insert the CTE in your temp table.

USE SomeDB;
WITH CTE AS 
(
 SELECT *
 FROM
 (
 SELECT ROW_NUMBER() OVER(ORDER BY Ppt.Type) AS Item_Code,
 Ppt.Type AS Type,
 '11/02/19 09:51' AS Created_Dt
 FROM Product AS Psi
 INNER JOIN Db1..Items AS Ios ON Ios.Icode = Psi.Icode
 INNER JOIN Db2..Types AS Ppt ON Ppt.Type = Ios.Type )
)
INSERT INTO #T
SELECT * FROM CTE
answered Mar 7, 2019 at 6:36
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks its worked for me using common table expression
1

The problem is, you are inserting data from an unnamed data source to a table.

select * into #temptable from
(select ROW_NUMBER() OVER(order by ppt.type) as Item_code,
ppt.type type,
...,
...,
...,
'11/02/19 09:51' Created_dt
from product psi
inner join [DB1]..items ios on ios.icode=psi.icode
inner join [DB2]..types ppt on ppt.type=ios.type) as tbl

Just go with this and your problem will be solved. I have just added a alias for the source you are inserting data.

answered Mar 7, 2019 at 6:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.