Insert query in oracle showing error Missing Expression - database

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;

Related

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.
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]

SQL Server Maximum rows that can be inserted in a single insert statment

I want to do a batch insert, similar to this question
How to do a batch insert in MySQL
What is the limitation is SQL Server on how many rows can be inserted in a single insert statement ?
What happens when for example the first value is inserted but the second one causes a primary key violation. Are the all INSERT statements rolled back?
INSERT INTO tbl_name (a,b)
VALUES (1, 2), (1, 3));
The Maximum number of rows you can insert in one statement is 1000 when using INSERT INTO ... VALUES... i.e.
INSERT INTO TableName( Colum1)
VALUES (1),
(2),
(3),...... upto 1000 rows.
But if your are using a SELECT statement to insert rows in a table, there is no limit for that, something like...
INSERT INTO TableName (ColName)
Select Col FROM AnotherTable
Now coming to your second question. What happens when an error occurs during an insert.
Well if you are inserting rows using multi-value construct
INSERT INTO TableName( Colum1)
VALUES (1),
(2),
(3)
In the above scenario if any row insert causes an error the whole statement will be rolled back and none of the rows will be inserted.
But if you were inserting rows with a separate statement for each row i.e. ...
INSERT INTO TableName( Colum1) VALUES (1)
INSERT INTO TableName( Colum1) VALUES (2)
INSERT INTO TableName( Colum1) VALUES (3)
In the above case each row insert is a separate statement and if any row insert caused an error only that specific insert statement will be rolled back the rest will be successfully inserted.
You can actually pass in an unlimited number of records using a subquery.
;WITH NewData AS (SELECT * FROM ( VALUES (1, 'A'),(2,'B'),(3,'C')) x (Id, SomeName))
INSERT INTO TableName (Column1, Column2) SELECT Id, SomeName FROM NewData
Although the max is 1000, it's been demonstrated that performance begins to diminish at much smaller numbers. Eugene Philipov wrote a great article exploring this very topic:
https://www.red-gate.com/simple-talk/sql/performance/comparing-multiple-rows-insert-vs-single-row-insert-with-three-data-load-methods/
To summarize, the author did some very well-designed experimenting and found a sweet spot at around 25. YMMV.
Dutchman's solution is cool, but it can be simplified. It turns out that you don't need the CTE. I tried it, and it works without the CTE, like this:
INSERT INTO TableName (Column1, Column2) SELECT Id, SomeName
FROM ( VALUES (1, 'A'),(2,'B'),(3,'C')) x (Id, SomeName)
you can try this
with tempDataTable AS (SELECT *From (VALUES
(18001,79626,'1992-12-11','1993-12-11') -- this is data u want to insert
)x(empNO,sal,frmDate,toDate)) -- tempDataColoumns
INSERT INTO salaries(emp_no,salary,from_date,to_date) SELECT empNO,sal,frmDate,toDate from newData
Remove '--' at the time of query
There's short workaround to avoid rows limit and still treat it like one statement (all goes in or if there's one error, everything is rolled back)
INSERT INTO tbl_name (a,b)
SELECT 1,2 UNION ALL
SELECT 1,3 UNION ALL
SELECT 1,4 .......
This is how I add more than 1000 values to the temp table.
CREATE TABLE #TempTable(ID int)
INSERT INTO #TempTable (ID)
SELECT * from (VALUES (45764),(45763),(45762),(45761),(45760),(45759),(45758),(45757),(45756)....)AS temp (column1)

How to avoid bulk data duplicate values when insert in to a table in SQL Server 2012

The problem im trying to solve is about avoiding duplicate data getting into my table. I'm using xml to send bulk data to a stored procedure. The procedure I wrote works with 100, 200 records. But when it comes to 20000 of them there is a time out exception.
This is the stored procedure:
DECLARE #TEMP TABLE (Page_No varchar(MAX))
DECLARE #TEMP2 TABLE (Page_No varchar(MAX))
INSERT INTO #TEMP(Page_No)
SELECT
CAST(CC.query('data(PageId)') AS NVARCHAR(MAX)) AS Page_No
FROM
#XML.nodes('DocumentElement/CusipsFile') AS tt(CC)
INSERT INTO #TEMP2(Page_No)
SELECT Page_No
FROM tbl_Cusips_Pages
INSERT INTO tbl_Cusips_Pages(Page_No, Download_Status)
SELECT Page_No, 'False'
FROM #TEMP
WHERE Page_No NOT IN (SELECT Page_No FROM #TEMP
INTERSECT
SELECT Page_No FROM #TEMP2)
How can I solve this? Is there a better way to write this procedure?
As was already suggested, NVARCHAR(MAX) column/variable is very slow and has limited options. If you can change it, it would help a lot.
MERGE tbl_Cusips_Pages
USING (
SELECT
CAST(CC.query('data(PageId)') AS NVARCHAR(4000))
FROM
#XML.nodes('DocumentElement/CusipsFile') AS tt(CC)
) AS source (Page_No)
ON tbl_Cusips_Pages.Page_No = source.Page_No
WHEN NOT MATCHED BY TARGET
THEN INSERT (Page_No, Download_Status)
VALUES (source.Page_No, 'false')
Anyway, your query is not that bad either, just put the queries directly into the third one (TEMP2 one for sure) instead of inserting the data into the table variables. Table variables are quite slow in comparison.
Replace last INSERT Statement with following Script, I have replace IN Clause With NOT EXISTS that may help you for better performance.
DECLARE #CommanPageNo TABLE (Page_No varchar(MAX))
INSERT INTO #CommanPageNo SELECT Page_No FROM #TEMP
INTERSECT
SELECT Page_No FROM #TEMP2
INSERT INTO tbl_Cusips_Pages(Page_No, Download_Status)
SELECT Page_No, 'False'
FROM #TEMP
WHERE NOT EXISTS (SELECT 1 FROM #CommanPageNo WHERE Page_No=#CommanPageNo.Page_No)

Output insert and table values when doing insert into

I'm inserting into a table in MS SQL Server 2008 (it's rather a copy of values from the same table) and want to get the output values for the insert. I want to get the id value of the select statement (t.id in the example below), the INSERTED.id works just fine
create table tmp.tbl_inserted (fromId int, toId int)
INSERT INTO mytable (name)
OUTPUT t.id, INSERTED.id INTO tmp.tbl_inserted
SELECT t.name FROM mytable t
Thanks in advance
You can't do it directly from an INSERT:
from_table_name
Is a column prefix that specifies a table included in the FROM clause of a DELETE, UPDATE, or MERGE statement that is used to specify the rows to update or delete.
Note that INSERT isn't mentioned.
What you have to do instead is cheat and use a MERGE:
MERGE INTO mytable m
USING (name,id FROM mytable) t ON 1=0
WHEN NOT MATCHED THEN INSERT (name) VALUES (t.name)
OUTPUT t.id, INSERTED.id INTO tmp.SizeCurveGroup_inserted
;

SQL Insert into Select failure due to one of the set of records

I am trying to insert a set of records into a table using
Insert into tbl1 Select * from tbl
One of the records failed due to check constrain in tbl1. But i want to insert the other records which have passed the check constraint and others i want to catch them as exception. Could someone please help.
In that case, you need to be more selective about your SELECT - exclude those rows that are trouble from your selection:
INSERT INTO dbo.tbl1(Col1, Col2, ...., ColN)
SELECT Col1, Col2, ....., ColN
FROM dbo.tbl
WHERE (some condition here to exclude those rows that don't match the `CHECK` constraint.....)

Resources