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
Related
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'
)
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;
Good morning. Trying to get the following query to CSV load data through a statement and failing miserably any assistance would be greatly appreciated.
Here's my data:
The statement that I'm using is:
create table #tempTablea
(
Employee_Id nvarchar(50),
First_Name nvarchar(50),
Last_Name nvarchar(50),
Japanese_Staff nvarchar(50),
Worker_Type nvarchar(50),
Hourly___Salaried nvarchar(50),
[Date] nvarchar(50),
[Start] nvarchar(50),
[End] nvarchar(50),
[Hour] nvarchar(50),
Timesheet_Start nvarchar(50),
Timesheet_End nvarchar(50),
Note nvarchar(50),
Is_Time_Off nvarchar(50),
Department_Full_Path nvarchar(50),
Called_In_Full_Path nvarchar(50),
Time_Off_Name nvarchar(50))
BULK INSERT #tempTablea FROM 'c:\data\555.csv' WITH
(
FIRSTROW = 8,
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n', --Use to shift the control to next row
ERRORFILE = 'C:\Errors\Error.CSV',TABLOCK)
INSERT INTO Ignition.dbo.timelogtest
(Employee_Id,
First_Name,
Last_Name,
Japanese_Staff,
Worker_Type,
[Date],
[Start],
[End],
[Hourly___Salaried],
[Hour],
Timesheet_Start,
Timesheet_End,
Note,
Is_Time_Off,
Department_Full_Path,
Called_In_Full_Path,
Time_Off_Name)
select t.Employee_Id,t.First_Name,t.Last_Name,t.Japanese_Staff,t.Worker_Type,t.Hourly___Salaried,(Cast(t.[Date] as date)),(Cast(t.[Start] as datetime)), (Cast(t.[End] as datetime)),t.[Hour],(Cast(t.Timesheet_Start as date)), (CAST(t.Timesheet_End as date)), t.Note,t.Is_Time_Off,t.Department_Full_Path,t.Called_In_Full_Path,Time_Off_Name
from #tempTablea t
The error that I get is:
(815 rows affected) Msg 241, Level 16, State 1, Line 30 Conversion
failed when converting date and/or time from character string.
So I know the data is loading into the temp table but when I load that data into the actual table I want it in it's failing, I'm assuming it's on the cast from the dates but not sure how I'm doing it wrong. I would do flat file except I'll receive this CSV every day and I want to just set it to eventually be automated where it just picks up the CSV and runs it daily with no one interacting with it. If you could help me with getting it from the temp table into the actual table I would be much obliged thanks!
In your INSERT..SELECT, replace all of your CAST and CONVERT statements with TRY_CAST and TRY_CONVERT.
I think it may be tacky but I figured it out due to being convert instead of cast. Posting answer in case anyone else has issues further along - Thank you all for input I appreciate.
if object_id('tempdb..#tempTablea') is not null
drop table #tempTablea
create table #tempTablea
(
Employee_Id nvarchar(50),
First_Name nvarchar(50),
Last_Name nvarchar(50),
Worker_Type nvarchar(50),
[Date] nvarchar(50),
[Start] nvarchar(50),
[End] nvarchar(50),
[Hour] nvarchar(50),
Is_Time_Off nvarchar(50),
Department_Full_Path nvarchar(50),
Time_Off_Name varchar(75))
BULK INSERT #tempTablea FROM 'c:Data\555.csv' WITH
(
FIRSTROW = 9,
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n', --Use to shift the control to next row
ERRORFILE = 'C:\Errors\Error.CSV',TABLOCK)
INSERT INTO Ignition.dbo.timelogtest
(Employee_Id,
First_Name,
Last_Name,
Worker_Type,
[Date],
[Start],
[End],
[Hour],
Is_Time_Off,
Department_Full_Path,
Time_Off_Name)
select t.Employee_Id,t.First_Name,t.Last_Name,t.Worker_Type,
convert(date,t.[date]),convert(datetime2,t.[Start]),Convert(datetime2,t.[End]),t.[Hour], t.Is_Time_Off,
t.Department_Full_Path,t.Time_Off_Name
from #tempTablea t
where t.[date] not in (select [date] from Ignition.dbo.timelogtest)
This question already has answers here:
Insert results of a stored procedure into a temporary table
(33 answers)
Closed 7 years ago.
I have a table variable with two columns. The first column is a value that I am trying to populate while the second value is being populated by the execution of a stored procedure.
CREATE PROCEDURE [dbo].userProfiles
(#userID INT) AS
BEGIN
DECLARE #sampleTable TABLE (moduleName VARCHAR(20),
userProfile int)
INSERT INTO #sampleTable('userprofile', exec getUserProfile(#userID))
SELECT *
FROM #sampleTable
END
However, I keep getting this error whenever I try executing the stored procedure:
Level 15, State 1, Procedure userProfiles, Line 9
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'userprofile'.
Any help will be well appreciated .
Thanks in advance
Probably your SP do have a select statement,
see: SQL Server - SELECT FROM stored procedure
CREATE PROCEDURE [dbo].userProfiles(
#userID INT
) AS
BEGIN
DECLARE #sampleTable TABLE (
moduleName VARCHAR(20),
userProfile int
)
DECLARE #stored_proc_table TABLE (
clmn datatype --similar as result set from getUserProfile
)
insert into #stored_proc_table exec getUserProfile(#userID)
INSERT INTO #sampleTable
select 'userprofile', clmn from #stored_proc_table;
SELECT * FROM #sampleTable
END
My guess is you need end your statement with semicolon ;
DECLARE #local_variable (Transact-SQL)
DECLARE #MyTableVar table(
EmpID int NOT NULL,
OldVacationHours int,
NewVacationHours int,
ModifiedDate datetime);
Missing values. Also, consider using out parameter to get value from procedure.
CREATE PROCEDURE [dbo].userProfiles(
#userID INT) AS
BEGIN
DECLARE #sampleTable TABLE(
moduleName VARCHAR(20),
userProfile int)
Create table #valueholder(resultvalue int)
insert into #valueholder exec getUserProfile(#userID)
INSERT
INTO #sampleTable
select 'userprofile',resultvalue from #valueholder
SELECT *
FROM #sampleTable
END
Also, you cannot make an inline call to procedure. use out parameter.
here is an example about out param.
https://technet.microsoft.com/en-us/library/ms187004(v=sql.105).aspx
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.