Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Variable Table

Author  Topic 

raghuveer125
Constraint Violating Yak Guru

285 Posts

Posted - 2011-04-19 : 06:59:22
How to insert data in variable table without declaring columns and datatype

for example
I have
select 'Hi','Think',Hello,1
----------
I can insert like this
select * into #t from (
SElect 'Hi' as h,'Think' as hh,'Hello' as hhhh,1 as hhhhh) as h

---But i cant

select * into #t from (
SElect 'Hi','Think' ,'Hello',1) as h

-- and even I cant how to solve this

select * into @t from
(
SElect 'Hi' as h,'Think' as hh,'Hello' as hhhh,1 as hhhhh) as h

---and how to solve


select * into @t from (
SElect 'Hi','Think' ,'Hello',1) as h


Raghu' S

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-04-19 : 07:18:09
You can't declare a table variable by a select statement.
You have to give a temp table column names - otherwise you won't be able to reference the data - you don't have to give datatypes, they can be derived from the data.

Your derived table has to be a table - which means it needs column names.

Think you should probably look at why you want to do this.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

raghuveer125
Constraint Violating Yak Guru

285 Posts

Posted - 2011-04-19 : 07:47:55
Hey just doing some R & D on MS-SQL

Raghu' S
Go to Top of Page

sravz
Starting Member

42 Posts

Posted - 2011-04-19 : 09:55:34
Hey ,

what nigelrivett said is true.When u want to add the values to Temp tables u need to select the columns u want in the temp table.I tried like the same way u tried but could not get it.for a last try I tried with like this, it worked out:


SELECT EmpNo,EmpName,EmpAddress
INTO #tmpEmp
FROM Employee
WHERE EmpName LIKE '%M%'


The above thing is just example.
Go to Top of Page
   

- Advertisement -