SQL Server - Insert record count into table - sql-server

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.

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]

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]

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

Related

Insert query in oracle showing error Missing Expression

insert into ASSET_MAIN_CATEGORIES values(select max(sno) from ASSET_MAIN_CATEGORIES,
'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1);
I wanted to insert max of SNO as column value. But It is showing "Missing Expression Error"
How can I achieve this.
Any Help would be appreciated.
There are at least ways of doing it:
INSERT INTO ... VALUES with embedded select in parentheses so that database will evaluate it:
insert into ASSET_MAIN_CATEGORIES values(
(select max(sno) from ASSET_MAIN_CATEGORIES),
'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1
);
INSERT INTO ... SELECT since all other data is static:
insert into ASSET_MAIN_CATEGORIES
select
max(sno),
'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1
from ASSET_MAIN_CATEGORIES
;
Note that if you do not specify which columns you are populating in ASSET_MAIN_CATEGORIES database assumes that you're feeding values for all of them in the order that they were created in this table.
You need to surround select max with brackets, because it's a SQL statement:
insert into ASSET_MAIN_CATEGORIES values((select max(sno) from ASSET_MAIN_CATEGORIES), 'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1);
If you have an error, prefer to add column names in insert statement
this will work :-
i have checked with my tables its working ,sample code to understand:
insert into b(col1,col2) select 7,(select max(col2) from b)
from dual;
what code you require:-
insert into ASSET_MAIN_CATEGORIES(col1,col2,....,col10)
select (select max(col2) from ASSET_MAIN_CATEGORIES), 'PROD','AC
HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1)
from dual;

SQL - Cannot print values from 'inserted' table

I have heard that the 'inserted' table automatically stores the last inserted record of any table temporarily.I was able to get the values from this inserted table when using triggers but not manually as
insert into samantha (id , name) values (11,'has')
select *from inserted
I am getting the error message as
Invalid object name 'inserted'.
Can you please give the reason.Thanks in advance
There is inserted table, but it can be only used with output clause, with something like this:
insert into samantha (id , name)
output inserted.*
values (11,'has')
If you have an identity field and you want the latest value from it, you can use SCOPE_IDENTITY() -function too:
insert into samantha (id , name)
values (11,'has')
select SCOPE_IDENTITY()
try use SELECT LAST_INSERT_ID() from samantha

SQL Server query - how to insert selected values into another table

Here's what I am trying to do.
This is the select statement
select Id
from tblUsersPokemons
where UserId = 1 and PokemonPlace = 'bag'
Now I want to insert those returned Ids into another table like this:
foreach all returned Ids
insert into tblNpcBattleUsersPokemons values (Id, 1)
How can I do that ?
Like this:
insert into tblNpcBattleUsersPokemons
select Id, 1 from tblUsersPokemons where UserId=1 and PokemonPlace='bag'
This can be done in a single sql call
insert into tblNpcBattleUsersPokemons (id, [whatever the name of the other column is])
select Id, 1 from tblUsersPokemons where UserId=1 and PokemonPlace='bag'
I've used the longhand here because it does not make any assumption about the ordering of columns in the destination table, which can change over time and invalidate your insert statement.
You can insert the set retrieved by a SELECT into another existing table using the INSERT...SELECT syntax.
For example:
INSERT INTO tblNpcBattleUsersPokemons (Id, Val) -- not sure what the second column name was
SELECT Id FROM tblUsersPokemons WHERE UserID = 1 and PokemonPlace='bag';

SQL Server list of insert identities

I have a table with an autoincrement id that I am doing a
INSERT INTO ( ... ) SELECT ... FROM ...
Is there a way for me to get the list of id's that have been inserted?
I was thinking I could get the max id before the insert then after and assuming everything in between is new, but then if a row gets inserted from somewhere else I could run into problems. Is there a proper way to do this?
I am using SQL Server 2005
Use the output clause.
DECLARE #InsertedIDs table(ID int);
INSERT INTO YourTable
OUTPUT INSERTED.ID
INTO #InsertedIDs
SELECT ...
Create a table variable and then use the OUTPUT clause into the table variable.
OUTPUT inserted.NameOfYourColumnId INTO tableVariable
Then you can SELECT from your table variable.

How to copy & insert records in table by stored procedure?

I have a table with one field: lngStatusID with value 2 for all the records.
Now I need to insert all the records again in the same table but with lngStatusID=1
So for that I think stored procedure will help me somehow.
AS per my logic it should be something like:
1) I need to read each record with loop
2) copy all fields in temporary variables
3) And than execute insert query to insert the record with lngStatusID=1
I am new to stored procedure.
So can any one guide me how to do that?
Or is there any easy way to do so?
You don't need a stored procedure for this, a simple INSERT statement will do:
insert into mytable
(field1, field2, lngStatusID)
select field1, field2, 1
from mytable
INSERT INTO <TABLENAME> (Col1,Col2,Col3)
SELECT Col1, Col2, 2
FROM <TABLENAME>
No need for sp or cursors
Why do you need to insert them again? Maybe simple UPDATE will be enough?
UPDATE table SET IngStatusID = 1
Please provide more details, because for me it is pointless to copy all the records to temporary table to insert them again.
UPDATE <tablename>
SET IngStatusID = 1
That being said, any table with only one field probably shouldn't be a table unless its some kind of lookup.

Resources