Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
XML
<b>I have a situation ,there are two tables

table1 :
            col1 col2 col3
            a    b    c
table2 :
            id ,col
            1  a
            2  c
            3  b and so on...


table 1 col1,col2,col3 data will be inserted into table 2 and generate id by identity.
Now I want ids of col1,col2 and col3  like
  1,2,3 (a,b,c..id values) in a single select query.
Because already my job is taking more time for remaining scripts I dont want to burden more
Can any one help me out .


</b>
Posted
Comments
$*Developer - Vaibhav*$ 3-Apr-14 7:18am    
table1 :
col1 col2 col3
a b c
table2 :
id ,col
1 a
2 c
3 b
4 e
5 k

In above case your table1 column increase?
pratap420 3-Apr-14 8:35am    
No for sample I gave like that but actually my table1 has fixed 8 cols like this,data will be different for each row and table2 data will be increased based on table1 col data .Table2 always have distinct rows .

Try this:
SQL
DECLARE @src TABLE(col1 VARCHAR(30), col2 VARCHAR(30), col3 VARCHAR(30))

INSERT INTO @src (col1, col2, col3)
VALUES('a', 'b', 'c')

DECLARE @dst TABLE(id INT IDENTITY(1,1), col VARCHAR(30))

INSERT INTO @dst (col)
SELECT colValue AS col
FROM (
    SELECT colValue, colDescription
    FROM (
        SELECT col1, col2, col3
        FROM @src
    ) AS pvt
    UNPIVOT(colValue FOR colDescription IN ([col1], [col2], [col3])) AS unpvt
) AS src

SELECT *
FROM @dst



Result:
1	a
2	b
3	c


The magic does UNPIVOT[^] statement ;)


[EDIT]
Below solution is based on OP's comments.

As per i understand what OP wants to do, He/She want to insert into table3 id's instead values stored in col1, col2, col3 which are equal to sValue.

SQL
DECLARE @table1 TABLE (col1 VARCHAR(30), col2 VARCHAR(30), col3 VARCHAR(30))

INSERT INTO @table1 (col1, col2, col3)
VALUES('file_abc ', 'Dfile_xyz', 'Mfile_mnp'),
('file_afc', 'Dfile_xyz', 'Mfile_mnp'),
('file_afc', 'Dfile_acd', 'Mfile_oop')

DECLARE @table2 TABLE(id INT IDENTITY(1,1), sValue VARCHAR(30))

INSERT INTO @table2 (sValue)
VALUES ('file_afc'), ('Dfile_acd'),
('Mfile_mnp'), ('file_abc'),
('Dfile_xyz'), ('Mfile_oop')


DECLARE @table3 TABLE(tab3id INT IDENTITY(1,1), col1 INT, col2 INT, col3 INT)

--SELECT *
--FROM @table1

--SELECT *
--FROM @table2


INSERT INTO @table3 (col1, col2, col3)
SELECT (SELECT id FROM @table2 WHERE sValue = t1.col1) AS col1,
        (SELECT id FROM @table2 WHERE sValue = t1.col2) AS col2,
        (SELECT id FROM @table2 WHERE sValue = t1.col3) AS col3
FROM @table1 AS t1


SELECT *
FROM @table3
ORDER BY tab3id


Note: There are few ways to achieve that. I showed you a way with subqueries ;)
[/EDIT]
 
Share this answer
 
v2
Comments
pratap420 4-Apr-14 2:35am    
Sorry for confusion.I already have table1,table2.By using these two tables I need to insert into other table (final_table),like
col1 col2 col3
1 2 3
10 12 9
Here 1,2,3,10,12,9 all these values are from table2 ids corresponding to table1 col values .
Maciej Los 4-Apr-14 2:40am    
What's the problem. Have a look at example and change it to your needs. @src - means source table, @dst - destination table. Replace it with table1 and table2 depend on your needs. It must works!
When you get stuck, come back here and i'll try to explain it in details.
pratap420 4-Apr-14 6:14am    
In your example you have one table for unpivot but in my case I need to use two tables.But how?
Maciej Los 4-Apr-14 10:39am    
OK. As per i understand, you want to join these 2 tables and insert its result set to table3. Am i right? So, what do i expect? Sample data for table1 and table2 and expected output. Then i'll improve my answer.
pratap420 5-Apr-14 1:49am    
yeah correct . sample data :

table1 : col1 col2 col3
file_abc Dfile_xyz Mfile_mnp
file_afc Dfile_xyz Mfile_mnp
file_afc Dfile_acd Mfile_oop
table 2: id value
1 file_afc
2 Dfile_acd
3 Mfile_mnp
4 file_abc
5 Dfile_xyz
6 Mfile_oop
7 .......
8 .........

whatever values in table1 col1,col2,col3 we are going to insert into table2 .
1.Everytime based on table1 , data will be inserted into table2 with out duplicating data .
2.By using tabl1,table2 we will insert result to table3 .

Now I want table 3 would be
table3_id col1_id col2_id col3_id
1 4 5 3
2 1 5 3
3 1 2 6
step 1 I have done using unpivot concept.I am working for step2.
Note : all these three tables are perminent tables
SQL
 insert into table3(col1,col2,col3)
(Select c1.id,c2.id,c3.id
from table1 t1
left join table2 c1 on c1.col=t1.col1
left join table2 c2 on c2.col=t1.col2
left join table2 c3 on c3.col=t1.col3
)


I tried with this .plz suggest me is it fulfill my requirement?
 
Share this answer
 
v3
Comments
Maciej Los 4-Apr-14 5:54am    
Not an answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900