[Cialug] SQL Question: Advanced INSERT INTO

Claus cniesen at gmx.net
Fri Sep 3 16:56:40 CDT 2010


Yes, this should work, however
- specify select only once unless you actually nest select statements
- don't use order by

So it's something like this should work (mysql syntax):

INSERT INTO table (column_1, column_2, column_3)
SELECT column_a, 100, 'hi there' FROM another_table

It helps if you could tell us what database you are using.  You also can 
check the manuals (on the net).  Google is your friend. ;)

   Claus

On 9/3/2010 2:34 PM, Todd Walton wrote:
> Dear Techie Friends and other SQL Knowledgeable,
>
> I'm wondering, if I need to do an INSERT INTO a table, and the table
> has a counter for a primary key, how do I handle setting the counter?
> I don't know beforehand what it should be, and it could change second
> by second.  Would something like this work?:
>
> insert into directory values
> (select (select top 1 directory_id from directory
> order by directory_id desc) + 1,'other columns')
>
> --
> Todd
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug
>



More information about the Cialug mailing list