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

tsql temp table issue 1

Status
Not open for further replies.

bouwob

Programmer
Apr 12, 2006
171
US
So I have this sql

declare @tempstr varchar(8000)
declare @freds varchar(3000)

set @freds = '8347151, 4, 7608501'



set @tempstr = 'create table temp as select fredid, bubbid, count(dotid) from freds where fredid in (' + @freds + ')'

exec (@tempstr)

but am recieving this error
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'as'.

it hovers over "declare @tempstr varchar(8000)" when I click on the error message.

Can I build a temp table in tsql and if so How do I do it???
 
You must put the symbol "#" infront of all temporary tables.

Try this:
Code:
declare @tempstr varchar(8000)
declare @freds varchar(3000)

set @freds = '8347151, 4, 7608501'

set @tempstr =  'select fredid, bubbid, count(dotid) into #temp from freds where fredid in (' + @freds + ')'

exec (@tempstr)

or

Code:
declare @tempstr varchar(8000)
declare @freds varchar(3000)

Create table #temp
(
fredid int,
bubbid int,
numRecs int
)

set @freds = '8347151, 4, 7608501'

set @tempstr =  'Insert into #temp(fredid, bubbid, numRecs)select fredid, bubbid, count(dotid) from freds where fredid in (' + @freds + ')'

exec (@tempstr)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top