Jump to content

[MSSQL] Populate a row with data from another table


bluesoul

Recommended Posts

I think I'm not really in the right state of mind right now to do this, I seem to remember a very neat shortcut to populate an identical set of columns in a table with data from another table.

 

A bit of background, I have a table called domains, with fields called NS1, NS2, NS3...NS13.  I am writing a page to turn the domains off when they're expired, and I want to hold this data in a new table called expired_domains for safekeeping when the time comes to restore it.  It has identical fields, NS1, NS2, NS3...NS13.

 

It would be something like an INSERT SELECT right?

INSERT INTO tbl_name (`col`, `col2`) SELECT `col`, `col2` FROM tabl_name WHERE x=x;

 

Should work. If the tbl_name columns matches the ones in tabl_name you can omit the (`col`..) portion and change the select to * instead of defining each column.

$q_copyns = "INSERT INTO brat_exp_ns ('domain_name','NS1','NS2','NS3','NS4','NS5','NS6','NS7','NS8','NS9','NS10','NS11','NS12','NS13') SELECT 'domain_name','NS1','NS2','NS3','NS4','NS5','NS6','NS7','NS8','NS9','NS10','NS11','NS12','NS13' FROM Domain WHERE domain_name='$turnoff[$i]'";

 

Throws an SQL error regarding an invalid object named brat_exp_ns.  Removing the quotes from the first set of columns inserts static data with those values (domain_name gets domain_name, etc).  Backticks as you have displayed don't work with MSSQL.

 

EDIT:  Got it, you actually need to have no quotes at all.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.