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
 table variable alternate

Author  Topic 

pnpsql
Posting Yak Master

246 Posts

Posted - 2012-05-09 : 00:27:24
hi team , i have a table abc1 that have 6 cols id , a1 , a2 , a3, a4, a5
i need insert values of these columns in a table vaiable and the
traverse the values. can it be done by any other way like cte.

ex:

DECLARE @var1 TABLE (
a1 VARCHAR(100) identity,
a2 VARCHAR(100));

then

insert into @var1 (a1)
select a1 from abc1 where ID = 1

insert into @var1 (a2)
select a1 from abc1 where ID = 1

insert into @var1 (a3)
select a1 from abc1 where ID = 1

insert into @var1 (a4)
select a1 from abc1 where ID = 1

insert into @var1 (a5)
select a1 from abc1 where ID = 1

please suggest the better way

challenge everything

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-09 : 06:47:25
what are you trying to do not clear. In your code you have specified the IDentity on varchar datatype that does not allow .. indentiy can be set on numeric data.Is your code working? I dont think so it would be working ..For inserting the data see the below example.




DECLARE @var1 TABLE (
a1 int identity,
a2 VARCHAR(100));


insert into @var1 (a2)values('2');

select * from @var1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-09 : 16:07:14
also you dont need seperate inserts for inserting column values. you can simply do them in a single statement


insert into @var1 (a1,a2,...a6)
select a1,a2,...,a6 from abc1 where ID = 1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -