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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select into temp table

Status
Not open for further replies.

dand11

Programmer
Jun 24, 2008
63
US
Is it possible to create a temp table on the fly as with the following SQL Server TSQL Script?

Code:
Select 'Some value' as Col1
into #temp
from myTable 
where primkey > 0

ALTER TABLE #temp ADD RowNum IDENTITY(1,1)
 
I have found some info and I'm trying the following but getting an error. any help is deeply appreciated:


Code:
BEGIN 
							Create temporary table tmp_Veh as 
							SELECT  'OFFENSE'  as TblSource
								, v.Primary as UNIQUEKEY
								, v.VIN as VIN
								, v.YEAR as VYEAR
								, v.MAKE as MAKE
								, v.Model as VModelText
								, v.License as TAG
								, v.Lic_Stat as	STATE
								, v.Top_clr + ' ' + v.Bot_clr as COLORSOLID
							 	, ' ' as ORIGINATINGORG
								, 1 as TotalCount
							
							 FROM Vehicle v inner join incident i on v.CN = i.CN
                 					 INNER JOIN charges c on v.CN = c.CN 
						 WHERE UPPER(v.Lic_Stat) = ?  
							UNION
							SELECT  'TRAFFIC'  as TblSource
								, v.Primary	as UNIQUEKEY
								, v.VIN as	VIN
								, v.YEAR as 	VYEAR
								, v.MAKE as	MAKE
								, v.Model as	VModelText
								, v.License as	TAG
								, v.Lic_Stat as	STATE
								, v.Top_clr + ' ' + v.Bot_clr as COLORSOLID
							 	, ' ' as ORIGINATINGORG
								, 1 as TotalCount
							 
							 FROM Vehicle v inner join incident i on v.CN = i.CN
                 					 INNER JOIN accident c on v.CN = c.ComplaintNumber 
						 WHERE LTRIM(v.Lic_Stat) = ?   
							UNION
							SELECT  'UCM'  as TblSource
								, v.Primary as UNIQUEKEY
								, v.VIN as VIN
								, v.YEAR as VYEAR
								, v.MAKE as MAKE
								, v.Model as VModelText
								, v.License as TAG
								, v.Lic_Stat as	STATE
								, v.Top_clr + ' ' + v.Bot_clr as COLORSOLID
							 	, ' ' as ORIGINATINGORG
								, 1 as TotalCount
							 
							 FROM Vehicle v inner join incident i on v.CN = i.CN
                 					 INNER JOIN ticket t on i.CN = t.CaseNumber 
						 WHERE LTRIM(v.Lic_Stat) = ?    
							ALTER TABLE tmp_Veh ADD Column RowNum AUTO_INCREMENT 
					
							Select * from tmp_Veh where RowNum > 0 and RowNum < 10
					
							COMMIT
 
Thats actually a MYSQL query the error I'm getting is below:


Code:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Create temporary table tmp_Veh as 
							SELECT  'OFFENSE'  as TblSource
				' at line 2
 
The question mark is replaced with: 'FL'
 
judging by where the error occurred, you might wish to remove the BEGIN keyword

don't forget to separate your individual SQL statements with semi-colons

r937.com | rudy.ca
 
Should I have the semi colons eve thought there are "UNION" keywords between them?
 
no, the semi-colon comes at the end of the SELECTs, before the ALTER, and another after the ALTER

and that ALTER doesn't look too healthy, either

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top