Update with select not working as expected [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have two tables
First Table
create table ApprovedLeave(
Username varchar(100),
FromDate date,
ToDate date,
type varchar(100),
address varchar(1000),
contactNo varchar(20),
NoofWorkingDays int,
);
Second Table
create table EMPLOYEE(
FName varchar(100),
LName varchar(200),
Username varchar(100),
NoOfDaysRemaining int,
constraint emp1 primary key(Username));
Once the leave is approved, I want to update with this logic:
NoOfDaysRemaining = NoOfDaysRemaining- NoofWorkingDays
So far what I have doesn't perform the above operation.
create Procedure UpdateNoOfDays2
AS
BEGIN
update le
set NoOfDaysRemaining = al.sum_NoofWorkingDays
from EMPLOYEE le join
(select UserName, sum(NoofWorkingDays) as sum_NoofWorkingDays
from ApprovedLeave
group by UserName
) al
on le.UserName = al.UserName
End
How can I add in this logic?

Your question is missing a question, but based on this:
NoOfDaysRemaining = NoOfDaysRemaining- NoofWorkingDays
Are you wanting this?
SET NoOfDaysRemaining = NoOfDaysRemaining - al.sum_NoofWorkingDays
UPDATE le
SET NoOfDaysRemaining = NoOfDaysRemaining - al.sum_NoofWorkingDays
FROM EMPLOYEE le
INNER JOIN
(SELECT UserName, sum(NoofWorkingDays) as sum_NoofWorkingDays
FROM ApprovedLeave
GROUP BY UserName
) al ON le.UserName = al.UserName

Related

Inserting into temp table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I am just getting data inserting to temp table.
DECLARE #XmlStringNPDBudget NVARCHAR(max) = '{"BudgetList":[{"BudgetId":4,"Month":1,"Year":2022,"Budget":750000},{"BudgetId":5,"Month":2,"Year":2022,"Budget":950000},{"BudgetId":0,"Month":3,"Year":0,"Budget":0},{"BudgetId":0,"Month":4,"Year":0,"Budget":0},{"BudgetId":0,"Month":5,"Year":0,"Budget":0},{"BudgetId":0,"Month":6,"Year":0,"Budget":0},{"BudgetId":0,"Month":7,"Year":0,"Budget":0},{"BudgetId":0,"Month":8,"Year":0,"Budget":0},{"BudgetId":0,"Month":9,"Year":"2022","Budget":"11111"},{"BudgetId":1,"Month":10,"Year":2022,"Budget":1000},{"BudgetId":2,"Month":11,"Year":2022,"Budget":350000},{"BudgetId":3,"Month":12,"Year":2022,"Budget":550000}]}'
DROP TABLE IF EXISTS #ParticularYearBudgetInsertUpdate
CREATE TABLE #ParticularYearBudgetInsertUpdate
(
[BudgetId] INT,
[Month] INT,
[Year] INT,
[Budget] DECIMAL
)
INSERT INTO #ParticularYearBudgetInsertUpdate([BudgetId], [Month], [Year], [Budget])
SELECT
[BudgetId], [Month], [Year], [Budget]
FROM
OPENJSON (#XmlStringNPDBudget)
WITH (BudgetId INT, [Month] INT, [Year] INT, [Budget] DECIMAL)
SELECT * FROM #ParticularYearBudgetInsertUpdate
SELECT * FROM NPDBudget
It's displaying column name and one row of NULLs. please help me out
Your JSON data is contained in a BudgetList array - so you need to take that into account - try this code for the SELECT:
SELECT
[BudgetId], [Month], [Year], [Budget]
FROM
OPENJSON (#XmlStringNPDBudget, N'$.BudgetList')
WITH
(
BudgetId INT '$.BudgetId',
[Month] INT '$.Month', [Year] INT '$.Year',
[Budget] DECIMAL '$.Budget'
)

Use and search by indexes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Suppose I have this table with the index given below:
CREATE TABLE [dbo].[Jobs]
(
[Id] BIGINT NOT NULL PRIMARY KEY IDENTITY(1,1),
[Type] SMALLINT NOT NULL,
[Path] NVARCHAR(MAX),
[Name] VARCHAR(256),
)
GO
CREATE INDEX [IX_Jobs_Name_Type] ON [dbo].[Jobs] ([Name], [Type])
Which query will have better performance:
1.
UPDATE TOP((#JobCount + 3) / 4 )
Jobs WITH (ROWLOCK, READPAST)
SET [Name] = #NName
WHERE [Name] IS NULL AND (Type = 1 OR Type = 4)
UPDATE TOP((#JobCount + 3) / 8 )
Jobs WITH (ROWLOCK, READPAST)
SET [Name] = #NName
WHERE [Name] IS NULL AND Type = 1
UPDATE TOP((#JobCount + 3) / 8 )
Jobs WITH (ROWLOCK, READPAST)
SET [Name] = #NName
WHERE [Name] IS NULL AND Type = 4
Ignore in this case the correctness of the amount of rows updated,
Can doing a single search with 'or' for the Type be less effective than 2 separate queries because of the index?
You should of course try on your database, but the database should be able to apply the index in the OR case. It would be better written as:
WHERE [Name] IS NULL AND Type IN (1, 4)

Creating variable myhospitalstaff [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
DECLARE #Myhospitalstaff TABLE(EmpID INT NOT NULL, Name VARCHAR(50) , Job VARCHAR(50) , HireDate Datetime , City VARCHAR(50), State VARCHAR(50) )
SET #Myhospitalstaff = (SELECT
EMPID,
SUBSTRING(NameJob,1,CHARINDEX('_',NameJob)-1) AS Name,
SUBSTRING(NameJob,CHARINDEX('_',NameJob),LEN(NameJob)) AS Job,
HireDate,
SUBSTRING(Location,1,CHARINDEX('-',Location)-1) AS City,
SUBSTRING(Location, CHARINDEX('-',Location),LEN(Location)) AS State
FROM HospitalStaff)
If you are looking to assign the results of the query to the table variable, then you need the insert syntax rather than set - and there is no need to alias the columns of the resultset (the table has its own column names already):
DECLARE #Myhospitalstaff TABLE(
EmpID INT NOT NULL,
Name VARCHAR(50),
Job VARCHAR(50),
HireDate Datetime,
City VARCHAR(50),
State VARCHAR(50)
);
INSERT INTO #Myhospitalstaff
SELECT
EMPID,
SUBSTRING(NameJob,1,CHARINDEX('_',NameJob)-1),
SUBSTRING(NameJob,CHARINDEX('_',NameJob),LEN(NameJob)),
HireDate,
SUBSTRING(Location,1,CHARINDEX('-',Location)-1),
SUBSTRING(Location, CHARINDEX('-',Location),LEN(Location))
FROM HospitalStaff;

What are the deprecated features in SQL Server 2016? Is MERGE statement available in SQL Server 2016? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
create table #Temp1 (id int identity(1,1), name varchar(50))
insert into #Temp1 values('Gokul')
insert into #Temp1 values('Dhivakar')
create table #Temp2 (id int, name varchar(50))
insert into #Temp2 values(1, 'Srikanth')
insert into #Temp2 values(3, 'Yogish')
select * from #Temp1
select * from #Temp2
MERGE #Temp1 AS target
USING #Temp2 AS source
ON (target.id = source.id)
WHEN MATCHED THEN
UPDATE SET Name = source.Name
WHEN NOT MATCHED THEN
INSERT (Name)
VALUES (source.Name) ;
-- OUTPUT deleted.*, $action, inserted.* INTO #MyTempTable;
Yes, Merge Statement supported in SQL Server 2016. It is supported 2008 onwards.

any way autometically add no is table [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
these are tables and coding.
create table student
(
s_id varchar(10) unique,
s_name varchar(50),
s_report varchar(50),
)
create table student_contact
(
stid int identity primary key,
stcellphone varchar(50) unique,
s_id varchar(10),
foreign key(s_id) references student(s_id)
)
create table test
(
t_id int identity primary key,
t_marks int,
)
create table st_test
(
stt_id int identity primary key,
s_id varchar(10),
t_id int,
foreign key(s_id) references student(s_id),
foreign key(t_id) references test(t_id)
)
alter view [join]
as
select s1.s_name,s2.stcellphone,s4.t_marks,s1.s_report
from
student as s1
inner join
student_contact as s2
ON s1.s_id=s2.s_id
inner join
st_test s3
ON s1.s_id=s3.s_id
inner join
test as s4
ON s4.t_id=s3.t_id
alter procedure marks
#st_id varchar(10)='',
#name varchar(50)='',
#mobile varchar(50)='',
#marks int=''
as
begin
if(#marks < 3)
begin
insert into student values(#st_id,#name,'You are enrolled for 6 months')
insert into test values(#marks)
insert into student_contact values(#mobile,#st_id)
select * from [join] where s_name =#name
end
end
this the coding which i have created i have created a procedure to insert data now i want that in "st_test" table there is 2 column names are (s_id,t_id) which are references to student and test table now the problem is i have created a view in which these table are join and this view is already used in procedure the problem is in procedure is that i want that when i fill all parameter i will see the inserted table according to condition.
You need to look into using SCOPE_IDENTITY():
declare #id int
insert into test values(#marks)
SET #id = SCOPE_IDENTITY()
insert into st_test values(#st_id,#id)
insert into student_contact values(#mobile,#st_id)
select * from [join] where s_name =#name
I'd also recommend using #id in your last select statement instead of the s_name as you know it should be unique.