Could you please tell me how to insert excel values in related sql table using sql query-.urgent

Answered Could you please tell me how to insert excel values in related sql table using sql query-.urgent

  • Tuesday, March 06, 2012 10:22 PM
     
     

    Hi Friends,

    I have imported that excel sheet  in sql database table.But Excel  have contained Table name, key , information of the text

    ex:

    Table _Name

    PKey

    Information text

    ABC

    1

    KING

    ABC

    2

    TEST

    ABC

    3

    JOHN

    ABC

    4

    MORE

    DEF

    1

    ASD

    DEF

    2

    ALLEN

    DEF

    3

    JOHHNE

    DEF

    4

    BANK

    GHK

    1

    ENG

    GHK

    2

    RING

    In sql Db I have same table names  ABC,DEF,GHK with PKEY values

    Table _Name

    Key

    ABC

    1

    ABC

    2

    ABC

    3

    ABC

    4

    DEF

    1

    DEF

    2

    DEF

    3

    DEF

    4

    GHK

    1

    GHK

    2

    Could you please send  me the sql querry  for insert the excel values in Related tables. i am beginner in sql 

    Thanks

    sql kid

All Replies

  • Tuesday, March 06, 2012 10:38 PM
    Moderator
     
     Answered

    Use the INSERT SELECT statement, specify the column names:

    http://www.sqlusa.com/bestpractices/insert-select/

    To upload data into the database, use the SSIS Import/Export Wizard:

    http://www.sqlusa.com/bestpractices2008/administration/ssiswizardicon/


    Kalman Toth, SQL Server & Business Intelligence Training; SQL SERVER BEST PRACTICES


  • Tuesday, March 06, 2012 10:41 PM
     
     

    You can do a simple Copy & Paste (in case the fields are in the same order)

    OR use the Import/Export wizard in the Management Studio (are you using MSSQL?)

    Database>Task>Import Data



    Shoshi -------------- Solution Architect www.TaoTechs.com

  • Tuesday, March 06, 2012 10:43 PM
    Moderator
     
     

    Can you please clarify what exactly do you want? Do you have several tables? If yes, what is the structure of these tables? Does your staging table has table name, PKey and a value? 


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

  • Tuesday, March 06, 2012 11:24 PM
     
      Has Code

    This might not be what you want, but what i understand is that you dumped values that belong to various tables, into a single table in your database and you would like to distribute them according to the table name contained into that unified table. This could be  a start maybe or maybe not!

    DECLARE @tablename varchar(10),
    		@SQL varchar(1000);
    
    DECLARE xl_cur CURSOR FOR SELECT table_name FROM xl_table GROUP BY table_name
    
    OPEN xl_cur;
    
    FETCH NEXT FROM xl_cur INTO @tablename;
    
    WHILE @@FETCH_STATUS = 0 BEGIN 
    	SET @SQL = 'INSERT INTO ' + @tablename + ' SELECT Information_text FROM xl_table WHERE table_name=' + @tablename;
    	EXECUTE( @SQL );
    	
        FETCH NEXT FROM xl_cur INTO @tablename;
    END
    
    CLOSE xl_cur;
    DEALLOCATE xl_cur;