Re: create and/or insert into a table, specifying columns once

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 08/26/04


Date: Thu, 26 Aug 2004 10:57:52 +0200

On Wed, 25 Aug 2004 17:07:09 -0700, David W. Rogers wrote:

>
>Moving the "SELECT INTO" out of the loop doesn't gain me anything - I still
>have to create a query that comes up with all of the columns in two places:
>first to create a new table and then to populate an existing table.
>
>The problem is that the syntax:
> SELECT select_list [ INTO new_table ]
>
>is the only way to use a select list to populate a table that doesn't
>already exist. And INSERT INTO requires a pre-existing table. What I would
>need would be:
>
> INSERT [INTO] new_table derived_table
>
>Thanks,
>David

Hi David,

I'm sorry, but there is no SQL command that does something like this. If
you really need to build your result set row by row, then your options
are:

1. Use a permanent table. Create it once; delete everything from it before
(or after) use. The quickest way to empty a table is to use TRUNCATE.

2. Use CREATE TABLE for a temporary table in your procedure. This will
create the table before the loop; you can now use INSERT INTO .. SELECT
for all iterations.

3. Use a dummy SELECT .. INTO (with dummy, I mean that you add WHERE 1=2
to ensure that you get only the structure, not the data) before the loop;
this duplicates a lot of your statement but not necessarily all.

4. Leave your code as it is now.

If the result set MUST be built row by row, I'd prefer to use the first of
these options. If that is not possible (e.g. because multiple users can
run the same procedure at the same time), choose the second option
instead. I see no reason to prefer the third or fourth option over this.

But as I said: that's only if you must do it row by row. In the example
code you posted, there is no reason at all to use iteration. Instead, you
could use a numbers table (creating it is a one-time operation and you'll
benefit from it very often) - see http://www.aspfaq.com/show.asp?id=2516.

Once you have your numbers table, you can replace the complete code you
posted in the message that started this discussion with this single query:

SELECT
        numbers.n AS Number1,
        GETDATE() AS DateTime,
        t1.*,
        t2.ATextColumn SecondText,
        t2.AnotherTextColumn ThirdText
FROM #Table1 t1
JOIN #Table2 t2
ON t1.ID = t2.ID
JOIN numbers
ON t1.ID = numbers.n
WHERE numbers.n BETWEEN 0 AND 10

Best, Hugo

-- 
(Remove _NO_ and _SPAM_ to get my e-mail address)


Relevant Pages

  • Re: Formatting and sorting cells with VBA
    ... I'm using a query to populate a series ... For some reason, the cell in the worksheets contains ...
    (microsoft.public.excel.programming)
  • RE: Query Problem, Please Help!!
    ... > this table to be laid out the way it is. ... The reason that experts recommend using a query as the Record Source for a ... The query ... will allow the designer to display the data in just about any way needed. ...
    (microsoft.public.access.queries)
  • Re: Issue with order placement...
    ... David's suggestion to use Me.Dirty = False is a good one. ... variable again to re-execute the same query, but I can also tell you this -- ... executing the identical update query to set a field to zero twice in a row ... There is good reason to set ...
    (comp.databases.ms-access)
  • Re: Converting bad table design to good design
    ... There isn't really a whole lot to learn about append queries. ... query) they are queries like any other. ... >> newsgroup, but if you have a good reason to send me e-mail, you'll find ... >>> Category for review ...
    (microsoft.public.access.externaldata)
  • Passing 2 parameters to stored query?
    ... query from a form. ... populate the variables then open the query it ... Set rst = New ADODB.Recordset ... Do Until CalcDate = Me.txtDateTo ...
    (microsoft.public.data.ado)