Insert Data from to a table

Posted by Lee_McIntosh on Stack Overflow See other posts from Stack Overflow or by Lee_McIntosh
Published on 2010-05-10T15:20:46Z Indexed on 2010/05/10 15:24 UTC
Read the original article Hit count: 310

Filed under:
|
|
|
|

I have a table that lists number of comments from a particular site like the following:

Date                        Site         Comments         Total
---------------------------------------------------------------
2010-04-01 00:00:00.000      1              5               5
2010-04-01 00:00:00.000      2              8               13
2010-04-01 00:00:00.000      4              2               7
2010-04-01 00:00:00.000      7              13              13
2010-04-01 00:00:00.000      9              1               2

I have another table that lists ALL sites for example from 1 to 10

Site
-----
1
2
...
9
10

Using the following code i can find out which sites are missing entries for the previous month:

SELECT s.site
from tbl_Sites s
EXCEPT
SELECT c.site
from tbl_Comments c
WHERE c.[Date] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0)

Producing:

site
-----
3
5
6
8
10

I would like to be able to insert the missing sites that is listed from my query into the comments table with some default values, i.e '0's

Date                        Site         Comments         Total
---------------------------------------------------------------
2010-04-01 00:00:00.000      3              0               0
2010-04-01 00:00:00.000      5              0               0
2010-04-01 00:00:00.000      6              0               0
2010-04-01 00:00:00.000      8              0               0
2010-04-01 00:00:00.000      10             0               0

the question is, how did i update/insert the table/values?

cheers,

Lee

© Stack Overflow or respective owner

Related posts about sql

Related posts about update