Use and search by indexes [closed] - sql

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)

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

Delete/Drop columns with nulls values [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 1 year ago.
Improve this question
I want to drop all columns from my table where all records are null.
My table looks as follows:
Locations [1] [2] [3] [4] [5]
[a] 10.00 Null Null 20.00 Null
[b] Null 30.00 Null Null Null
Basically, I want to delete columns 3 and 5 from the above table. What would be the best way to do this?
Edit: Instead of manually going through the table to drop each column individually, is there a way where I can drop all columns with null records together?
Thank you.
To check if the values of a column for all rows (not records) are null you can use an aggregate such as
select max([1]), max([2])... from table
to remove a column you drop it
alter table <tablename> drop column <column name>
You could create a dynamic version of a script something like this
drop table if exists #Locations;
go
CREATE TABLE #Locations (
[Location] varchar(10) not null,
[1] decimal(12,2),
[2] decimal(12,2),
[3] decimal(12,2),
[4] decimal(12,2),
[5] decimal(12,2));
INSERT INTO #Locations([Location], [1], [2], [3], [4], [5]) values
('[a]', 10.00, Null, Null, 20.00, Null),
('[b]', Null, 30.00, Null, Null, Null);
if not exists (select 1 from #Locations where [3] is not null)
alter table #Locations
drop column [3];
select * from #Locations;
Location 1 2 4 5
[a] 10.00 NULL 20.00 NULL
[b] NULL 30.00 NULL NULL
As I understood ,to check the data that contain of values Null use this query like that:
SELECT 1 -- with using a literal
FROM (table)
HAVING COUNT(a) = 0
AND COUNT(*) > 0;
After that remove the columns with using this query :
Alter table <Table Name> drop column <Column Name>

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

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;

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.