t-sql query to populate some data into a new table

  • i have a table T1 in which i have 10 columns for example col1 , col2 , col3,......col 10 .

    now the records for col1 has to be populated from colX which is in tableX similarly for col2 from coly and table y .... similarly for col3 .... till col5

    col6 is hard coded , for example i need the records in col6 as 'source' and also for col 7 and col 8 (col6 through col8 are hard coded) and i need col 9 and col 10 as nulls

    how do i go ahead and write the t-sql ( whats the best approach ?)

    thanks

  • Sorry, but from reading your text description, I have no idea what you are trying to accomplish. Could you please show some examples?

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • table1

    col1 col2 col3 col4 col5 col6 col7 col8 col9 col10

    tableA tableB tableC tableD tableE

    colA colb colc colD colE

    s p d f g

    q w r t u

    i want to populate table1

    the data for col1 has to come form colA

    similarly col2 --- colb , col3----- colc, col -- 4 colD , col5 ---colE

    col 6 , col 7 and col 8 are hard coded (col 6 is hard coded as "X" col 7 as "Y" col8 "z" col9 has to be null . col 10 has to be null

    the result should the data inserted into table1

    table1

    col1 col2 col3 col4 col5 col6 col7 col8 col9 col10

    s p d f g x y z null null

    q w r t u x y z null null

  • tableA ,TableB, tableC , tableD , tableE,

    colA. colb ,colc , colD , colE,

    s, p, d, f, g,

    q, w, r, t , u,

    table 1

    col1,col2,col3,col4,col5,col6,col7,col8,col9,col10

    s,p,d,f,g,X,Y,Z,null,null,

    q,w,r,t,u,X,Y,Z,null,null,

    x , y and z are hard coded ,

    initially table1 has no records

    i need the t-sql to insert records in table 1 and the data inserted should look like the records above (table1) so the records what we are seeing in table one are coming form columns in different tables

    like i have mentioned above

  • On both copies of your question people are asking for clarification. Remember we can't see your screen and have no knowledge of your project. The details you provided are simply not clear enough to begin to come up with a solution.

    As a total guess...you are trying to pull 5 columns of data from one table into another table? In addition to those 5 columns you want 4 specified values?

    Total shot in the dark...

    insert sometable

    select col1, col2, col3, col4, col5, 'X', 'Y', 'Z', null, null

    from SomeOtherTable

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • i am trying 5 columns of data from 5 different tables into one table

  • koolme_85 (11/28/2011)


    i am trying 5 columns of data from 5 different tables into one table

    So use the example I posted above.

    select a.col1, b.col2, c.col3, d.cold4, e.col5, 'x', 'y', null, null

    from table1 a

    join table2 b on b.id = a.id

    join table3 c on c.id = a.id

    etc...

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • works well thanks guys

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply