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'
)
Related
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
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)
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;
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.
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 8 years ago.
Improve this question
I have a database table in which I am storing data in this format say May 2014. I want to fetch data from my table according to month i.e May 2014, Apr 2014, Mar 2014. . .Jan 2014
Please guide me what should be my select query? My column data type is varchar(450)
How should I use order by clause in my select query? Should I use Group By clause?
This may help you.
I think you can create a separate Look Up table (including all Months and Years) and join with your source table
Example:
LookUp table
DECLARE #LUDTTABLE TABLE
(ID INT IDENTITY(1,1), [YEAR] VARCHAR(20),[MONTH] VARCHAR(20))
INSERT INTO #LUDTTABLE VALUES
('2014','JAN'),
('2014','FEB'),
('2014','MAR'),
('2014','APR'),
('2014','MAY'),
('2014','JUN'),
('2014','JUL')
Source table
DECLARE #TABLE TABLE (ID INT IDENTITY(900,1), DT VARCHAR(20))
INSERT INTO #TABLE VALUES
('JAN 2014'),
('JUL 2014'),
('FEB 2014'),
('FEB 2014'),
('APR 2014'),
('JAN 2014'),
('JAN 2014')
Result:
SELECT *
FROM #TABLE A
JOIN #LUDTTABLE B
ON LEFT(A.DT,3) = B.MONTH
AND RIGHT(A.DT,4) = B.YEAR
ORDER BY B.ID
If you are using sql server 2008 cast your column to date format and order by month()
use the following snippet
SELECT * FROM your_table_name ORDER BY MONTH(CAST (your_column_name AS DATE))
For previous version use convert function to convert varchar to datetime
SELECT * FROM your_table_name ORDER BY MONTH(CONVERT(datetime, your_column_name))