ALTER TRIGGER tr_ItemAccessInsert
ON [dbo].[tbl_InventoryGroup]
AFTER INSERT
AS
BEGIN
DECLARE #ID decimal(18,3), #InventoryID decimal(18,3)
SELECT #ID = [ItemID] FROM [dbo].[tbl_ItemMain]
SELECT #InventoryID = [GroupID] FROM inserted
INSERT INTO tbl_ItemInventoryAccess ([ItemID], [InventoryID], [Value])
VALUES (#ID, #InventoryID, '0.11')
END
this is my query for trigger
i have 10 item in tbl_itemmain so i want to insert all the 10 itemsid in the tbl_inventoryacess please help me with that
You would just use insert . . . select:
insert into tbl_ItemInventoryAccess ([ItemID], [InventoryID], [Value])
select i.??, i.GroupID, '0.11'
from inserted i;
It is unclear how ItemId is determined. I imagine that a table called tbl_ItemMain has more than one row. Perhaps you can just use i.ItemId.
if i am using like
Alter trigger tr_ItemAccessInsert
on [dbo].[tbl_InventoryGroup]
After insert
as begin
Declare #ID decimal(18,3),#InventoryID decimal(18,3)
select #InventoryID=[GroupID] from inserted
insert into tbl_ItemInventoryAccess([ItemID],[InventoryID],[Value]) values((select itemid from tbl_ItemMain) ,#inventoryid,'0')
end
getting an error subquery returned more than one value
declare #temp table
(
Id int,
Qty int
)
insert into #temp values(1,null)
update #temp set Qty+=2
select * from #temp
I am trying to update the Qty column however the Qty column is null by default. So because the production database column might be null when I try to update I am getting a null value I need Qty to be 2.
Use coalesce (or isnull) to handle the null case.
update #temp set Qty = coalesce(Qty,0) + 2;
If you want to increment the value, then the column should not allow NULLs.
I would suggest that you default the value to 0. Then don't insert a NULL value explicitly:
declare #temp table (
Id int,
Qty int not null default 0
);
insert into #temp (id) values (1);
update #temp set Qty += 2;
select * from #temp;
You can also express the insert as:
insert into #temp (id, value) values (1, default);
I have a stored procedure that I inherited. The goal is for the data in the temp table: #multi_nominees_uf to be joined with the data in #temp_reviewers_UF and assign #temp_reviewers_UF.uf_rev_id to #multi_nominees_uf.application_number where the corresponding uf_rev_id's short_plan does not match the major associated with the appNumber's major .
It is not as simple as doing a JOIN.
The following has to be in place:
short_plan can not match the major(associated with the uf_rev_id)
count of each uf_rev_id can only be in the table a certain number of times (#RevsPerRevieweruf). Also, the uf_rev_id will be in the #temp_reviewers_uf table more than once with a different short_plan, it should only be looking at DISTINCT uf_rev_id when calculating the #RevPerRevieweruf.
The way it is written now, the counts are not consistent. One uf_rev_ID may have 122 records and another may have 50 - each distinct uf_rev_id should have the same count (or very close). I have researched and tried NTILE but I couldn't figure it out.
Any ideas of the best way to accomplish this?? Any input is appreciated.
-----Sample Data -----
CREATE TABLE #mult_nominees_uf(
appnum VARCHAR(8)
,major VARCHAR(8)
,compid INT
);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('00012345','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002343','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002777','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10000023','DED',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('23457829','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('78954321','RRE',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('90002342','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('11156726','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('88855593','RRE',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10000001','DED',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('20000393','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('11119999','DED',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('78927626','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('67589393','RRE',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('12453647','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('00012345','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002343','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002777','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10000023','DED`',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('23457829','AAR',2);
--with this sample data the #RevsPerReviewerUF count would be 4 since A5 is listed twice and we only want distinct values used
CREATE TABLE #Temp_Reviewers_uf(
uf_rev_id VARCHAR(8)
,short_plan VARCHAR(8)
,fac_emplid INTEGER
);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A1','ACT',00000012);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A2','BBC',00000145);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A3','DED',10002934);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A5','RRE',90001223);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A5','ACT',90001223);
----Stored procedure -
DECLARE #Index INT
DECLARE #Num_nomineesUF INT
DECLARE #Num_reviewersUF INT
DECLARE #Num_reviewersUFDISTINCT INT
DECLARE #Num_reviews INT
DECLARE #Rev_ID nvarchar(25), #Nom_ID varchar(8), #Short_Plan varchar(8), #Major varchar(8)
DECLARE #RevsPerReviewerUF INT
SET #Num_reviews = 4
DECLARE #actualCompID int
DECLARE #UF_Flag INT
DECLARE #InsertNum int
SET #InsertNum = 1
create table #mult_nominees_UF (appNumber varchar(8), Major varchar(8), comp_id INT)
create table #TempNomineeTable (uf_rev_id varchar(8), fac_emplid varchar(9), appNumber varchar(8), Major varchar(8), short_plan varchar(8), comp_id int)
create table #Temp_Reviewers_UF (uf_rev_id varchar(8), short_plan varchar(8), fac_emplid varchar(9)) -- temp table used to hold Nom_IDs already assigned to Rev_IDs
set #actualCompID = 21
-- * * SELECT APPLICATION NUMBER & MAJOR FROM FS_RESULTS TABLE * * * * * --
select appNumber, LEFT(Major, CHARINDEX('-', Major)-1) as Major, comp_id into #Delete_nomineesUF
from FS_Results
where UF='Y'
and comp_id = #actualCompID
and nominated=1;
SET #Num_nomineesUF = ##rowcount; --GET RECORD COUNT
IF (#Num_nomineesUF > 0)
BEGIN
SET #UF_Flag = 1;
END
SET #Index = 1 ; -- reinit variable
WHILE #Index <= 4 BEGIN
if (#UF_Flag > 0)
BEGIN
INSERT into #mult_nominees_uf
select * from #Delete_nomineesUF
END
-- Create temp table for UF Reviewers
select uf_rev_id, short_plan, fac_emplid into #temp_reviewers_UF
from ReviewersID_ShortPlan
where uf_rev_id like 'UF%'
and competition_id = #actualCompID
SET #Num_reviewersUF = ##rowcount
SELECT DISTINCT UF_REV_ID FROM ReviewersID_ShortPlan WHERE UF_REV_ID like 'UF%' AND competition_id = #actualCompID
SET #Num_reviewersUFDistinct = ##rowcount
SET #RevsPerReviewerUF = (#Num_nomineesUF * #Num_reviews) / nullif(#Num_reviewersUFDistinct,0)
WITH Match_NomineesWithReviewers AS(
SELECT DISTINCT
appNumber,
RTRIM(Major) AS Major,
COUNT(1) as rowcnt,
comp_id
FROM #mult_nominees_uf
GROUP BY appNumber,
RTRIM(Major),
comp_id
)
, rownum_matches AS (
SELECT m.appNumber,
m.Major,
t.short_plan,
t.uf_rev_id,
t.fac_emplid,
m.rowcnt,
m.comp_id,
ROW_NUMBER() OVER (PARTITION BY m.appNumber order by newid()) AS rownum
FROM Match_NomineesWithReviewers m
JOIN #temp_reviewers_UF t ON t.short_plan != m.major
GROUP BY m.appNumber, m.Major, t.short_plan,
t.uf_rev_id, t.fac_emplid, m.rowcnt, m.comp_id
HAVING COUNT(t.uf_rev_id) <= #RevsPerRevieweruf
)
INSERT INTO #TempNomineeTable
SELECT uf_rev_id, fac_emplid, appNumber, Major, short_plan, null, 0, null, comp_id FROM rownum_matches rm
WHERE rownum <= rowcnt
group by uf_rev_id, fac_emplid, appNumber, Major, short_plan, comp_id
HAVING COUNT(uf_rev_id) <= #RevsPerRevieweruf
For table1 Inserted 3 records
It should get those three identities and it should insert 3 records in table3 (but it’s not happening- it inserts 3 records with same identity ie.last scope identity)
create table table1(ID INT identity(1,1),Name varchar(50))
insert into table1 values('Ram'),('Sitha'),('Laxman')
create table table1(ID INT identity(1,1),Name varchar(50))
create table table3(ID INT ,Name varchar(50))
insert into table2(Name)
select Name from table1
declare #id int;
set #id= (select scope_Identity())
begin
insert into table3(ID,Name)
select #id,Name from table2
end
select * from table2
select * from table3
How can get all identities to insert do I need to write a loop (or) do I need to Create a trigger.
Please give me a solution I am strugguling from past 4 hours.
Thanks in anvance
Use the OUTPUT clause to handle multi-row inserts:
INSERT INTO dbo.table2(Name)
OUTPUT inserted.ID, inserted.Name INTO table3
SELECT Name FROM dbo.table1;
You can use the OUTPUT clause to get the identity from any number of inserts.
create table table1(ID INT identity(1,1),Name varchar(50))
DECLARE #T1 Table (ID int, name varchar(50))
insert into table1
OUTPUT inserted.ID, Inserted.Name INTO #T1
values('Ram'),('Sitha'),('Laxman')
DECLARE #IdentityId INT,#Count INT=1
DECLARE #temp AS TABLE (Id INT IDENTITY ,Name NVARCHAR(100))
INSERT INTO #temp(Name)
SELECT Name FROM table1
WHILE #Count <=(SELECT COUNT(SId) FROM #temp)
BEGIN
INSERT INTO table2(Name)
SELECT Name FROM #temp
WHERE Id=#Count
SET #IdentityId = SCOPE_IDENTITY()
INSERT INTO tabel3(#IdentityId,Name)
SELECT 3, #IdentityId,1,GETDATE()
SET #Count=#Count+1
END
question for sql server query.
I try to create a query for searching every word on a sendence. words are split by a space.
The result contain lines that the value contain every word not at least 1.
sample:
DECLARE #DATATABLE TABLE (ID INT,COL1 VARCHAR(MAX))
INSERT INTO #DATATABLE (ID,COL1) VALUES (1,'ALPHA')
INSERT INTO #DATATABLE (ID,COL1) VALUES (2,'ALPHA BETA')
INSERT INTO #DATATABLE (ID,COL1) VALUES (3,'ALPHA BETA CHARLIE')
INSERT INTO #DATATABLE (ID,COL1) VALUES (4,'ALPHA BETA CHARLIE DELTA')
DECLARE #TESTTABLE AS TABLE (ID INT,COL1 VARCHAR(MAX))
INSERT INTO #TESTTABLE (ID,COL1) VALUES (1,'ALPHA') -- RETURN 1,2,3,4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (2,'ALPHA BETA') -- RETURN 2,3,4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (3,'BETA CHARLIE ALPHA') -- RETURN 3,4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (4,'BETA DELTA') -- RETURN 4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (5,'CHARLIE') -- RETURN 3,4
SELECT * FROM #DATATABLE
DECLARE #I AS INT
SET #I = 1
DECLARE #COUNTLINE AS INT
SET #COUNTLINE = (SELECT MAX(ID) FROM #TESTTABLE )
DECLARE #CURRENTVALUE AS VARCHAR(MAX)
WHILE(#I <= #COUNTLINE)
BEGIN
SET #CURRENTVALUE = (SELECT COL1 FROM #DATATABLE WHERE ID = #I)
-- ** TODO **
-- QUERY SHOULD RETURN EVERY LINE CONTAINING EACH WORD OF THE CURRENT TEST VALUE
SELECT * FROM #DATATABLE WHERE COL1 LIKE #CURRENTVALUE
SET #I = #I + 1
END
I write "TODO" where I put the first test (saw what that shouldn't do).
I cant create SP or Function.
I look on my side for "with" and "in" or something else.
tank for any tips
and good week end