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)
Related
I'm trying to insert Id with the help of output clause but I get this error:
Column name or number of supplied values does not match table definition
CREATE TABLE #TEMP_Master_DimensionValues
(
Id int,
[Name] varchar(max),
[FullName] varchar(max),
ID_DimensionHierarchyType varchar(max),
StartDate varchar(max),
EndDate varchar(max)
)
DECLARE #OutputTbl TABLE ([ID] INT);
INSERT INTO #TEMP_Master_DimensionValues
OUTPUT INSERTED.[ID] INTO #OutputTbl([ID])
SELECT
'April01-17' [Name],
'''Week of ''' + CONVERT(VARCHAR, (SELECT Min('2021-04-01') FROM Master_DimensionValues), 107) [FullName],
'3' [ID_DimensionHierarchyType],
'2021-04-01' [StartDate],
NULL [EndDate];
The select statement above is correct and returns a result, but I couldn't figure out what's going wrong when I am trying into #TEMP_Master_DimensionValues. If anybody could help me it would be appreciated
I always recommend to be explicit about the columns you are inserting into - so change your INSERT statement to be like this:
DECLARE #OutputTbl TABLE ([ID] INT);
INSERT INTO #TEMP_Master_DimensionValues ([Name], [FullName], ID_DimensionHierarchyType, StartDate, EndDate)
OUTPUT INSERTED.[ID] INTO #OutputTbl([ID])
SELECT
'April01-17' [Name],
'''Week of ''' + CONVERT(VARCHAR, (SELECT Min('2021-04-01') FROM Master_DimensionValues), 107) [FullName],
'3' [ID_DimensionHierarchyType],
'2021-04-01' [StartDate],
NULL [EndDate];
Also, I'd highly recommend not just making everything a varchar(max) - use the most appropriate datatype - always - so a StartDate or EndDate should really be DATE (or alternatively DATETIME2(n)) - but most certainly NOT varchar(max)!
I am getting an error from this code:
DECLARE #New_Hospitalstaff TABLE
(
[NAME] VARCHAR(50),
JOB VARCHAR(50),
HIREDATE DATETIME,
CITY VARCHAR(50),
[STATE] VARCHAR(50)
)
INSERT INTO #New_Hospitalstaff (NAME, JOB, HIREDATE, CITY, STATE)
SELECT
(SUBSTRING(Name, 1, (CHARINDEX('_', Name) - 1,))), AS Name
(SUBSTRING(JoB, (CHARINDEX('_'job) + 1, 15)) AS JobHireDate,
(SUBSTRING(Location, 1, (CHARINDEX('_', Location) - 1))) AS City
(SUBSTRING(Location, CHARINDEX('_', Location) + 1, 10)) AS State
FROM
DBO.HospitalStaff
SELECT *
FROM #New_Hospitalstaff
You are getting a lot of syntax errors. I think formatting your code and paying closer attention to parenthesis will help you. Take a look at this and compare to yours.
DECLARE #New_Hospitalstaff TABLE ( [NAME] varchar(50) , [JOB] varchar(50) , [HIREDATE] datetime ,
[CITY] varchar(50) , [STATE] varchar(50) )
INSERT INTO #New_Hospitalstaff(NAME,JOB,HIREDATE,CITY,STATE )
SELECT (SUBSTRING(Name,1, (CHARINDEX('',Name)-1))) AS Name,
-- Missing [JOB] field in query. Needed in order for INSERT to work.
-- You probably want the whole field so: JOB,
(SUBSTRING(JOB, (CHARINDEX('', job)+1),15)) AS JobHireDate,
(SUBSTRING(Location,1, (CHARINDEX('',Location)-1))) As City,
(SUBSTRING(Location, CHARINDEX('',Location)+1,10)) As State
FROM DBO.HospitalStaff
SELECT * FROM #New_Hospitalstaff
I fixed out of place commas, missing parenthesis, and extra parenthesis based on the functions being called.
I am facing TWO MAJOR PROBLEMS!!
PROBLEM 1:
I have two tables and want to show the required data into a specific gridview by using UNION in SQL. Table 1 contains columns {[Date] datetime, [Head] nvarchar(50), [Details] nvarchar(360), [ExpId] int, [Amount] decimal(18,2)}
Table 2 contains columns {[Purchase_Date] datetime, [VendorName] nvarchar(50), [Remarks] nvarchar(50), [Bill_No] nvarchar(50), [AmountPaid] decimal(18,2) }
My stored procedure is;
DECLARE #Ledger TABLE
(
DATE DATETIME,
DESCRIPTION NVARCHAR(350),
REF_NO NVARCHAR (50),
AMOUNT INT
)
INSERT INTO #Ledger
SELECT
[Date], [Head] + ' - ' + [Details], [ExpId], [Amount]
FROM
[dbo].[Table1]
UNION
SELECT
[Purchase_Date], 'PURCHASE' + ' ' + [VendorName] + ' ' + [Remarks], [Bill_No], [AmountPaid]
FROM
[dbo].[Table2]
SELECT * FROM #Ledger
When is execute the query I get an error
Conversion failed when converting the nvarchar value 'ABC113973' to data type int.
I wonder why its throwing this error when I try to execute it without Table1 it's fine. Is is due to the column ExpId with datatype int? If yes then how to deal with it?
PROBLEM 2:
In the above #Ledger table when I change Amount datatype to decimal(18,0) as I want to show the result in decimal figure it throws error
Conversion failed when converting varchar into numeric
sort of error. as the datatype of amount columns of both the actual tables are decimal(18,2).
Can anyone tell me the solution and the reasons of this problem? Thanks
Try this:
DECLARE #Ledger TABLE
(
DATE DATETIME,
DESCRIPTION NVARCHAR(350),
REF_NO NVARCHAR (50),
AMOUNT INT
)
INSERT INTO #Ledger
SELECT [Date], [Head] + ' - ' + [Details], CAST([ExpId] AS NVARCHAR(50)), [Amount] FROM [dbo].[Table1]
UNION
SELECT [Purchase_Date], 'PURCHASE' + ' ' + [VendorName] + ' ' + [Remarks], CAST([Bill_No] AS NVARCHAR(50)), [AmountPaid] FROM [dbo].[Table2]
SELECT * FROM #Ledger
You are getting an error because you are trying to insert bill_no and expid into a varchar column,but the data type of both fields are int. So, you will have to either cast or convert the int values and then insert it into the table.
Edit:
If you want to store the amount in Ledger table as decimal then change the data type of amount column to decimal(18,2) and make sure that columns of both the actual tables are also of the same data type.
DECLARE #Ledger TABLE
(
DATE DATETIME,
DESCRIPTION NVARCHAR(350),
REF_NO NVARCHAR (50),
AMOUNT DECIMAL(18,2)
)
I want to transfer data from source Database table named Patient (which contains many rows) to destination database tables(2) named Person & Patient.
I already have stored procedure named AddPatient in destination database which will add person related fields to Person table and other fields to Patient table, so I would like to execute that procedure and to assign the fields from source database as variables to it. The following are the code of AddPatient sp in destination database.
ALTER PROCEDURE [dbo].[AddPatient]
(
#TenantId BIGINT,
#FirstName NVARCHAR(100),
#LastName NVARCHAR(100),
#PersonNumber NVARCHAR(20),
#MobileNumber NVARCHAR(20),
#EmailId NVARCHAR(100),
#Address NVARCHAR(255),
#City NVARCHAR(50),
#ZipCode NVARCHAR(20),
#ListComments NVARCHAR(1000),
#Comment NVARCHAR(500),
#AlternateEmailId NVARCHAR(100) ,
#HomePhone NVARCHAR(20) ,
#Relative NVARCHAR(255) ,
#HasDiabetes [bit],
#HasBlooPressure [bit],
#AddedBy BIGINT,
#AddedDateTime smalldatetime,
#PersonId BIGINT OUTPUT
)
AS
BEGIN
SET NOCOUNT ON
IF #TenantId IS NULL
RAISERROR('The value for #TenantID should not be null', 15, 1) -- with log
ELSE
BEGIN
DECLARE #new_person_id BIGINT
DECLARE #new_patient_id BIGINT
DECLARE #PatientIdentifier NVARCHAR(50)
EXEC dbo.GetNextPatientIdForTenant #TenantID, #PatientIdentifier OUTPUT
INSERT INTO dbo.Person
(
TenantId,
FirstName,
LastName,
PersonNumber,
MobileNumber,
EmailId,
Address,
City,
ZipCode,
AddedBy,
AddedDateTime
)
VALUES
(
#TenantId,
#FirstName,
#LastName,
#PersonNumber,
#MobileNumber,
#EmailId,
#Address,
#City,
#ZipCode,
#AddedBy,
#AddedDateTime
)
SELECT #new_person_id = SCOPE_IDENTITY()
INSERT INTO dbo.Patient
(
TenantId,
PatientIdentifier,
PersonId,
ListComments,
Comment,
AlternateEmailId,
HomePhone,
Relative,
HasDiabetes,
HasBlooPressure,
AddedBy,
AddedDateTime
)
VALUES
(
#TenantId,
#PatientIdentifier,
#new_person_id,
#ListComments ,
#Comment ,
#AlternateEmailId,
#HomePhone ,
#Relative ,
#HasDiabetes,
#HasBlooPressure,
#AddedBy ,
#AddedDateTime
)
SELECT #new_patient_id = SCOPE_IDENTITY()
SELECT #PersonId = #new_person_id
SELECT #new_patient_id
END
END
There is no TenantId & AddedBy field in source, so I want to assign both as 1 for all rows to be transfered.
I know Execute SQL Task will handles stored procedure and for each row data Foreach Loop Container will take care in SSIS. But I don't know how to assign the variables of sp in destination database to the fields of table from source database.
Anyone help me with this.
Thanks in advance !
So I am using a cursor to loop through a bunch of records that my query returns. I have just updated some details in a table and now I want to pull the details from that table so I have used a temporary table.
So now I want to insert some values into a new table that are unrelated to the last and then the rest of the values would be a direct copy from the table variable...how can I do this?
I'll post below the section in question to help people see what I am trying to do.
The part in question is between the update status comment and the above not finished comment.
OPEN cur
FETCH NEXT FROM cur INTO #MembershipTermID , #EndDate , #MembershipID <VARIABLES>
WHILE ##FETCH_STATUS = 0
BEGIN
--PERFORM ACTION
DECLARE #TodaysDate DATETIME
SET #TodaysDate = getDate()
--CANCEL DETAIL
DECLARE #CancellationDetailID INT
INSERT INTO CancellationDetail(CancellationDetailID,RefundAmount,OldEndDate,EffectiveDate,CancelDate,ReasonCodeProgKey)
VALUES (0, 0.0, #EndDate, #TodaysDate, #TodaysDate, 'CANC_DORMANT')
SELECT #CancellationDetailID = SCOPE_IDENTITY()
INSERT INTO CancellationDetailAudit(StampUser,StampDateTime,StampAction,CancellationDetailID,RefundAmount,OldEndDate,EffectiveDate,CancelDate,ReasonCodeProgKey)
VALUES('SYSTEM', GetDate(), 'I', #CancellationDetailID, 0.0, #EndDate, #TodaysDate, #TodaysDate, 'CANC_DORMANT')
--LINK TO TERM
INSERT INTO MembershipTermCancellationDetail(CancellationDetailID,MembershipTermID)
VALUES(#CancellationDetailID, #MembershipTermID)
INSERT INTO MembershipTermCancellationDetailAudit(StampUser,StampDateTime,StampAction,MembershipTermCancellationDetailID,CancellationDetailID,MembershipTermID)
VALUES('SYSTEM', GetDate(), 'I', 0, #CancellationDetailID, #MembershipTermID)
--UPDATE STATUS
UPDATE MembershipTerm
SET MemberStatusProgKey = 'CANCELLED',
EndDate = #TodaysDate,
UpdateDateTime = #TodaysDate,
AgentID = 224,
NextTermPrePaid = 'False'
WHERE MembershipTermID = #MembershipTermID
DECLARE #MembershipTermTable TABLE
(
MembershipTermID int,
MemberStatusProgKey nvarchar (50),
StartDate datetime,
EndDate datetime,
AdditionalDiscount float,
EntryDateTime datetime,
UpdateDateTime datetime,
MembershipID int,
AgentID smallint,
PlanVersionID int,
ForceThroughReference nvarchar (255),
IsForceThrough bit,
NextTermPrePaid bit,
IsBillingMonthly bit,
LastPaymentDate datetime,
PaidToDate datetime,
IsIndeterminate bit
)
INSERT INTO #MembershipTermTable
SELECT MembershipTermID,
MemberStatusProgKey,
StartDate,
EndDate,
AdditionalDiscount,
EntryDateTime,
UpdateDateTime,
MembershipID,
AgentID,
PlanVersionID,
ForceThroughReference,
IsForceThrough,
NextTermPrePaid,
IsBillingMonthly,
LastPaymentDate,
PaidToDate,
IsIndeterminate
FROM MembershipTerm
WHERE MembershipTermID = #MembershipTermID
INSERT INTO MembershipTermAudit(StampUser,StampDateTime,StampAction,MembershipTermID,MemberStatusProgKey,StartDate,EndDate,AdditionalDiscount,EntryDateTime,UpdateDateTime,MembershipID,AgentID,PlanVersionID,ForceThroughReference,IsForceThrough,NextTermPrePaid,IsBillingMonthly,LastPaymentDate,PaidToDate,IsIndeterminate)
VALUES ('SYSTEM',#TodaysDate,'I',MembershipTermID,MemberStatusProgKey,StartDate,EndDate,AdditionalDiscount,EntryDateTime,UpdateDateTime,MembershipID,AgentID,PlanVersionID,ForceThroughReference,IsForceThrough,NextTermPrePaid,IsBillingMonthly,LastPaymentDate,PaidToDate,IsIndeterminate)
--ABOVE NOT FINISHED, NEED TO ADD AUDIT RECORD CORRECTLY
--Members
DECLARE #MembersTable TABLE
(
MembershipTermID int,
MemberStatusProgKey nvarchar (50),
StartDate datetime,
EndDate datetime,
AdditionalDiscount float,
EntryDateTime datetime,
UpdateDateTime datetime,
MembershipID int,
AgentID smallint,
PlanVersionID int,
ForceThroughReference nvarchar (255),
IsForceThrough bit,
NextTermPrePaid bit,
IsBillingMonthly bit,
LastPaymentDate datetime,
PaidToDate datetime,
IsIndeterminate bit
)
INSERT INTO #MembersTable
SELECT * FROM [MembershipTermPerson] WHERE MembershipTermID = #MembershipTermID
--Vehicles
FETCH NEXT FROM cur INTO #MembershipTermID , #EndDate , #MembershipID <VARIABLES>
END
CLOSE cur
DEALLOCATE cur
I think this would be a good case for a INSERT INTO SELECT statement
Something like
INSERT INTO MyTable (ColA, ColB, ColC)
SELECT
GETDATE(), A.MyCol, 'MyValue'
FROM MyOtherTable A
WHERE a.MyValue = 'What I Want'
Basically you skip the temp table, and just grab the value and inject everything at once.