SQLOakland
SQLOakland

Reputation: 29

SQL Server - Insert record count into table

I need to execute this script monthly to load a record count into at table:

select count([BG_BUG_ID]) 
from [uc_maint_maintenance_db].[td].[BUG]

I created a table with one column to contain the numeric output:

CREATE TABLE [dbo].[BG_BUG_ID]
(
    [BG_BUD_ID_COUNT] [numeric](18, 0) NULL
) 

I receive an error on the select statement when I execute the script below:

INSERT INTO [AdminDB].[dbo].[BG_BUG_ID](count)
VALUES (SELECT COUNT([BG_BUG_ID]) 
        FROM [uc_maint_maintenance_db].[td].[BUG])

What am I doing wrong? The select runs fine on its own. Any ideas are greatly appreciated!

I need to make this insert into a stored procedure.

Upvotes: 1

Views: 1818

Answers (3)

Joe C
Joe C

Reputation: 3993

INSERT INTO [AdminDB].[dbo].[BG_BUG_ID]
SELECT COUNT([BG_BUG_ID]) 
        FROM [uc_maint_maintenance_db].[td].[BUG]

Upvotes: 0

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

  • You have mentioned wrong column name count instead of BG_BUD_ID_COUNT.
  • Remove keyword values.

Try like below

INSERT INTO [AdminDB].[dbo].[BG_BUG_ID](BG_BUD_ID_COUNT)
SELECT COUNT([BG_BUG_ID])
FROM [uc_maint_maintenance_db].[td].[BUG]

Upvotes: 0

Chris Pickford
Chris Pickford

Reputation: 8991

Remove values:

Insert into [AdminDB].[dbo].[BG_BUG_ID](BG_BUD_ID_COUNT)
select count([BG_BUG_ID])
from [uc_maint_maintenance_db].[td].[BUG]

Upvotes: 1

Related Questions