Creating variable myhospitalstaff [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 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;

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'
)

Incorrect syntax near 'NVARCHAR' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
CREATE TABLE #temptable
(
Assessment_Component_Identifier NVARCHAR(50),
Predecessor_Assessment_Component_Identifier NVARCHAR(50),
Assessment_Period_Identifier NVARCHAR(50),
[Level] TINYINT,
Assessment_Period_dataination_Identifier NVARCHAR(50)
);
INSERT INTO #temptable
(
Assessment_Component_Identifier NVARCHAR(50),
Predecessor_Assessment_Component_Identifier NVARCHAR(50),
Assessment_Period_Identifier NVARCHAR(50),
[Level] TINYINT,
Assessment_Period_dataination_Identifier NVARCHAR(50)
);
I'm getting the following error
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near 'NVARCHAR'
If you want to insert into #temp table then you have to use only column names in the list as follows:
INSERT INTO #temptable
(
Assessment_Component_Identifier,
Predecessor_Assessment_Component_Identifier,
Assessment_Period_Identifier,
[Level],
Assessment_Period_dataination_Identifier
) VALUES (.....);
CREATE TABLE #temptable
(
S_No INT Identity(1, 1),
Assessment_Component_Identifier NVARCHAR(50),
Predecessor_Assessment_Component_Identifier NVARCHAR(50),
Assessment_Period_Identifier NVARCHAR(50),
[Level] TINYINT,
Assessment_Period_dataination_Identifier NVARCHAR(50)
);
--it's better to use primary key or indexing if you are working on bulk data
insert into #temptable values('x','x','x',1,'x')
select * from #temptable
IF OBJECT_ID(N'tempdb..#temptable') IS NOT NULL
BEGIN
DROP TABLE #temptable
END

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)

SQL Server : Procedure, setting value for the variables [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
Intending to update Table6 with the following procedure. I have set variables and they are correctly addressed but how do I define value for the 2 types of variables. there are search variables and update variables
Procedure with condition if the cell has NULL value than add whatever the Variable has but if it already has value then leave its original value/don't overwrite it.
CREATE PROCEDURE dbo.[Learning]
#StartDate DATE NULL,
#EndDate DATE NULL,
#Data VARCHAR(30) NULL,
#Cond_CID INT NOT NULL,
#Cond_PID INT NOT NULL,
#Cond_SSC VARCHAR(3) NOT NULL
AS
BEGIN
BEGIN
UPDATE temp.dbo.Table6
SET StartDate = ISNULL(StartDate,#StartDate)
,EndDate = ISNULL(EndDate,#EndDate)
,Data = ISNULL(Data,#Data)
WHERE Table6.CID = #Cond_CID AND TABLE6.PID = #Cond_PID AND TABLE6.SSC = #Cond_SSc
The table and data insert
CREATE TABLE temp.dbo.Table6
(
CID INT NOT NULL,
PID INT NOT NULL,
SSC VARCHAR(3) NULL,
StartDate DATE NULL,
EndDate DATE NULL,
Data VARCHAR(30) NULL
)
INSERT INTO temp.dbo.Table6
VALUES
(1001, 1333,'OP', GETDATE(), GETDATE(), 'OP2001156519952012'),
(1002, 1245,'OR', GETDATE(), NULL, 'OR20121005'),
(1003, 1116,'OP', GETDATE(), NULL, 'OP20131215'),
(1004, 1234,'OP', GETDATE(), GETDATE(), 'OP2001156519952012')
SELECT * FROM temp.dbo.Table6
sample data which has null values and this procedure will be used to populate data into the table.
It seems like you want to prevent the (3) search parameters to be NULLs, but when you set those parameters as NOT NULL SQL Server will complain about it, and throw
Msg 11555 Level 15 State 1 Line 7
The parameter 'ParameterName' has been declared as NOT NULL. NOT NULL parameters are only supported with natively compiled modules, except for inline table-valued functions.
Also, I think your first 3 parameters shouldn't be NULL too, cause if you pass NULLs then your UPDATE won't do anything, just set your columns to NULL again.
You can check for NULLs in the body of your SP, and if one of them IS NULL then raise an error as
CREATE PROCEDURE dbo.[Learning]
(
#StartDate DATE,
#EndDate DATE,
#Data VARCHAR(30),
#Cond_CID INT,
#Cond_PID INT,
#Cond_SSC VARCHAR(3)
)
AS
BEGIN
IF (#Cond_CID IS NULL OR #Cond_PID IS NULL OR #Cond_SSC IS NULL
OR #StartDate IS NULL OR #EndDate IS NULL OR #Data IS NULL
)
BEGIN
RAISERROR('Null values not allowed for the 3 last parameters!', 16, 1)
END
ELSE
BEGIN
UPDATE temp.dbo.Table6
SET StartDate = ISNULL(StartDate,#StartDate)
,EndDate = ISNULL(EndDate,#EndDate)
,Data = ISNULL(Data,#Data)
WHERE Table6.CID = #Cond_CID
AND
TABLE6.PID = #Cond_PID
AND
TABLE6.SSC = #Cond_SSc
END
END
or by Creating Natively Compiled Stored Procedures.

Update with select not working as expected [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 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