Insert multiple column with where clause - sql-server

While inserting multiple columns of data with where condition, I get an error
insert into s_grpeffect(grpeffect_cess, grpeffect_rcm, grpeffect_fulllife)
values ('12.0000', '12.0000', '2019')
where grpeffect_grpkid = 124;

You cannot add where clause for insert statement.
insert into s_grpeffect(grpeffect_cess,grpeffect_rcm,grpeffect_fulllife) values('12.0000','12.0000','2019')
Or If you are selecting value from table then below is the syntax,
INSERT INTO table2
SELECT * FROM table1
WHERE condition;

Related

How do I add/update a table variable row based on a DB row in MS SQL loop

I need to create a table variable and then select rows from an MS SQL DB table and then do the following:
loop thru each row
add or update the table variable row, depending on the looped row column values
Here's how the code more or less be structured
select * from table1 t1
define #table variable --this should have corresponding columns with the same types in t1
--loop thru each t1 row
--check if the combination of row values in t1 is present
--if present update #table column
--else, insert the t1 row into #table
loop each t1 row
select * from #table where #table.c1=t1.c1 and #table.c2=t1.c2 and #table.c3=t1.c3
if #table #numrows >= 1
update #table.c4=t1.c4 where #table.c1=t1.c1 and #table.c2=t1.c2 and #table.c3=t1.c3
else
insert into #table(c1, c2, c3, c4) values (t1.c1, t1.c2, t2.c3, t1.c4)

Insert into with select statement where value is not null

I want to perform an insert operation based on the select statement from another table in SQL Server.
I have built this:
INSERT INTO Table1
SELECT table2.var1, table2.var2, 1, GETDATE(), 1, GETDATE(),0
FROM table2
The values in Table1 are all NOT NULL, and there is a couple of record of table2.var2 where there is a null value, I want to skip the Null and continue with the operation.
You can filter the rows where table2.var2 is null in a WHERE clause.
INSERT INTO table1
(<list of target columns>)
SELECT table2.var1,
table2.var2,
1,
getdate(),
1,
getdate(),
0
FROM table2
WHERE table2.var2 IS NOT NULL;
You should also explicitly list the targeted columns in any INSERT so that statements don't break if the number or order of columns change in the target table.

How to access inserted and deleted logical tables with a stored procedure

Is there a way to access inserted and deleted tables using a stored procedure or a function WITHOUT using or passing values from a trigger.
Ex:
INSERT INTO dbo.Table (ID)
VALUES (1)
SELECT * FROM inserted -> This raises an error
Is there a way to do something like this in order to capture the inserted value?
I was able to do this to a query which uses the MERGE command as below.
MERGE Products AS TARGET
USING UpdatedProducts AS SOURCE ON (TARGET.ProductID = SOURCE.ProductID)
WHEN MATCHED AND TARGET.ProductName <> SOURCE.ProductName
OR TARGET.Rate <> SOURCE.Rate
THEN
UPDATE SET TARGET.ProductName = SOURCE.ProductName,
TARGET.Rate = SOURCE.Rate
WHEN NOT MATCHED BY TARGET
THEN
INSERT (ProductID, ProductName, Rate)
VALUES (SOURCE.ProductID, SOURCE.ProductName, SOURCE.Rate)
WHEN NOT MATCHED BY SOURCE
THEN
DELETE
OUTPUT
$action,
DELETED.ProductID AS TargetProductID,
DELETED.ProductName AS TargetProductName,
DELETED.Rate AS TargetRate,
INSERTED.ProductID AS SourceProductID,
INSERTED.ProductName AS SourceProductName,
INSERTED.Rate AS SourceRate;
Is there a way to access inserted and deleted tables for simple SQL queries?
You can use the inserted and deleted tables only in triggers or in the output clause of DML statements.
In an insert statement, the output clause can reference the inserted table:
DECLARE #Ids AS TABLE (id int);
INSERT INTO dbo.Table (ID)
OUTPUT Inserted.ID INTO #Ids(id)
VALUES (1), (2), (3);
In an update statement, you can reference both the inserted table and the deleted table:
DECLARE #Ids AS TABLE (oldId int, newId int);
UPDATE dbo.Table
SET ID = 1
OUTPUT Deleted.ID, Inserted.ID INTO #Ids(oldId, newId);
And in a delete statement, you can reference the deleted table:
DECLARE #Ids AS TABLE (id int);
DELETE dbo.Table
OUTPUT Inserted.ID INTO #Ids(id)
FROM dbo.Table
WHERE ID IN (1, 2, 3);
A Merge statement is unique since you have access to both the source table and the inserted/deleted tables in it's output clause, as demonstrated on this post.

How to get multiple scope_identity from table then insert to other table?

brothers, can you help me?
Sample Query:
DECLARE #id_scope TABLE (ID_TBA_PK int)
Insert into TABLE_A
Select ID_TBA_PK,ID_LET_FK,NAME, ADDRESS FROM TABLE_A WHERE
ID_LET_FK=#ID_LET_FK
set #id_scope = scope_identity() --but must get multiple identity
-- because above insert multiple,ID_TBA_PK is autoincement.
then insert to table others:
insert into TABLE_B
select ID_TBA_FK=#id_scope , NAME, ADDRESS FROM TABLE_B
WHERE ID_TBA_FK=#ID_TBA_FK
--(MULTIPLE INSERT TO TABLE_B)
Add the OUTPUT clause to your INSERT. Something like this:
INSERT TABLE_A(NAME, ADDRESS, etc.)
OUTPUT Inserted.ID_TBA_PK into #id_scope
Select ID_TBA_PK,ID_LET_FK,NAME, ADDRESS FROM TABLE_A WHERE
ID_LET_FK=#ID_LET_FK
This line I am assuming ID_TBA_PK is your new identity
OUTPUT Inserted.ID_TBA_PK into #id_scope
so the general case is
INSERT [table] (columns)
OUTPUT INSERTED.[column] into [#other table]
SELECT columns from .....

sqlserver assigning last inserted Id to another column in one query

when this query is executed
DECLARE #Temp TABLE (ID INT IDENTITY,Name VARCHAR(50),ID2 INT NULL)
INSERT INTO #Temp ([Name]) VALUES ('Ali')
UPDATE #Temp SET ID2= (SELECT SCOPE_IDENTITY()) WHERE [ID]=(SELECT SCOPE_IDENTITY())
INSERT INTO #Temp ([Name]) VALUES ('Veli')
UPDATE #Temp SET ID2= (SELECT SCOPE_IDENTITY()) WHERE [ID]=(SELECT SCOPE_IDENTITY())
SELECT * FROM #Temp
We can get this table
ID-NAME-ID2
1 - Ali - 1
2 - Veli - 2
is there a way to do this in one insert query ( Assigning inserted id to another column without using idendity property in that column) ?
thanks a lot.
You want to make it a computed column like below
create TABLE Temp(ID INT IDENTITY,Name
VARCHAR(50),
ID2 AS ID PERSISTED);
Then insert rows ... your ID values will be persisted in ID2 column
INSERT INTO Temp ([Name])
VALUES ('Ali');
INSERT INTO Temp ([Name])
VALUES ('Veli');
INSERT INTO Temp ([Name])
VALUES ('Neli');
Which will results in
See a demo fiddle here http://sqlfiddle.com/#!3/59fca/1
EDIT:
If you can't change your table structure then the only way insert ID value to ID2 column is the way you are currently doing it cause in same insert statement the identity value is still not available and so it will be null.

Resources