2 Replies - 2773 Views - Last Post: 10 August 2012 - 10:09 PM

#1 rony001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-July 12

Copy from one table to another based on condition

Posted 06 July 2012 - 02:26 AM

I am new to sql .please help me with this .
the following sql statement works when i use like this
Dim CmdStr As String = "insert into table1(id,name,ref,amount) select id,name,ref,amount from table where name = A"


How do i use the statement multiple times to insert into multiple tables like this

 Dim CmdStr As String
 CmdStr= "insert into table2 (id,name,ref,amount) select id,name,ref,amount from table where name = B"
 CmdStr="insert into  table3 (id,name,ref,amount) select id,name,ref,amount from table where name = C"
 CmdStr="insert into  table4 (id,name,ref,amount) select id,name,ref,amount from table where name = d"

This post has been edited by Atli: 06 July 2012 - 02:48 AM
Reason for edit:: Please use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: Copy from one table to another based on condition

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

Re: Copy from one table to another based on condition

Posted 06 July 2012 - 03:05 AM

Repeatedly overwriting the CmdStr like that won't work. It'll just end up being whatever you assigned it last. If you want to add to that variable you'd have to use the concat operator.
CmdStr  = "Insert into ... WHERE A;"
CmdStr &= "Insert into ... WHERE B;"
CmdStr &= "Insert into ... WHERE C;"


Whether or not executing multiple statements like that is actually allowed is another matter. You may well have to execute them one at a time.

This also seems like an odd thing to do. I'm guessing you are planing to use this in conjunction with what you are asking in your other thread; creating new tables based on data in an existing table. Which leads me to the same question: Why do you want to do this? Generally you'd want to keep this data in a single table, rather than scattering it over multiple smaller tables.
Was This Post Helpful? 0
  • +
  • -

#3 rony001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-July 12

Re: Copy from one table to another based on condition

Posted 10 August 2012 - 10:09 PM

thank you so much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1