Import CSV into SQL Server - sql-server

I need import a .csv file into SQL Server.
I tried with bulk but that didn't work
I need create table with field left and column with field right .
Example
CREATE TABLE CARGA_TRAFICO_MED_MES_DATASET_IT
(
ID INT NOT NULL IDENTITY(1, 1),
NAME VARCHAR(200),
STATUS varchar(20),
PRIMARY KEY(ID)
);
INSERT INTO CARGA_TRAFICO_MED_MES_DATASET_IT
VALUES("Job_Activity_8 (JOB JOB_CARGA_TRAFICO_DATASET_IT)", "status=1")

Related

Getting an "Invalid object name" error in ADS after creating a new table

I'm working on a new Jupyter Notebook, using SQL as the Kernel, to create a new table, populate it with a couple records, etc., then drop the table. I've written the CREATE TABLE DDL, then ran it. However, when I ran it in ADS it gave me an error on the table name ("Invalid object name ") and each column in the new table ("Invalid column name "). But it created the table, nonetheless. Huh? What's going on?
I've looked for similar questions posted here on SO, but none of the match my situation. For example, one of them the user had created the table as one name, but then tried to do a SELECT against a different table name, that was slightly different from the one they created. That's not the case for me. Here's the SQL DDL for creating the table:
IF OBJECT_ID('[dbo].[Bozo]', 'U') IS NOT NULL
DROP TABLE [dbo].[Bozo]
GO
-- Create the table in the specified schema
CREATE TABLE [dbo].[Bozo]
(
[Id] INT NOT NULL PRIMARY KEY, -- Primary Key column
[FirstName] NVARCHAR(50) NOT NULL,
[LastName] NVARCHAR(50) NOT NULL,
-- Specify more columns here
Bool1 BIT DEFAULT 1,
Bool2 BIT DEFAULT 1,
BoolValue AS Bool1 & Bool2
);
GO
And here's my SQL INSERT statements:
NSERT INTO Bozo (FirstName, LastName)
VALUES ('George', 'Washington');
INSERT INTO Bozo (FirstName, LastName, Bool2)
VALUES ('John', 'Adams', 0);
I discovered that the SQL Script that Azure Data Studio uses to create a new table does not define the Id column as an IDENTITY column. Changing that fixed the issue, so now it is:
IF OBJECT_ID('[dbo].[Bozo]', 'U') IS NOT NULL
DROP TABLE [dbo].[Bozo]
GO
-- Create the table in the specified schema
CREATE TABLE [dbo].[Bozo]
(
[Id] INT IDENTITY(1,1) NOT NULL PRIMARY KEY, -- Primary Key column
[FirstName] NVARCHAR(50) NOT NULL,
[LastName] NVARCHAR(50) NOT NULL,
-- Specify more columns here
Bool1 BIT DEFAULT 1,
Bool2 BIT DEFAULT 1,
BoolValue AS Bool1 & Bool2
);
GO

Import a file without identity key into table with identity key via bcp

I have a text file with 6 columns and 200 million rows and none of them is unique. I'd like to import them into a table in SQL Server and want to define an Identity column as primary key.
Therefore I created the following table first:
CREATE TABLE dbo.Inventory
(
ProductID NUMERIC(18,3) NOT NULL,
RegionID NUMERIC(18,3) NULL,
ShopCode INT NULL,
QTY FLOAT NULL,
OLAPDate VARCHAR(6) NULL,
R Float NULL,
ID BIGINT NOT NULL PRIMARY KEY IDENTITY(1,1)
)
Then I use the below command for importing the text file into the table:
bcp ETLDB.dbo.Inventory in D:\SCM\R.txt -T -b 10000 -t "," -c -e D:\SCM\Errors.txt
and I got these errors:
I am not sure if the errors are because of the identity id column which is in my table design and not in my original text file or not. Because when I delete the identity id key from the table, the bcp works fine. But I want the bcp defines the identity id in the process of importing my file into table.
The sample text file:
Any help would be appreciated.
Create a view that looks like what you want to load into and load into that
CREATE VIEW dbo.Inventory_Stage
AS SELECT
ProductID,
RegionID,
ShopCode,
QTY,
OLAPDate,
R Float
FROM Inventory
Now load into Inventory_Stage instead of Inventory
also, use -F to start loading at the second row, because the first row has column names
bcp ETLDB.dbo.Inventory_Stage in -F 1 D:\SCM\R.txt -T -b 10000 -t "," -c -e D:\SCM\Errors.txt
Also, seriously consider if you want to use float. For your sample data I recommend NUMERIC(19,6)
There is a workaround I tried for a similar case.
Step 1:
Create a Table with the columns available to your CSV/TXT file.
Step 2:
Push the data using the BCP script.
bcp dbo.<tablename> in <file location in local folder> -S <server_name> -d <database_name> -U <username> -P <password> -b 20000 -q -c -t"<column delimiter>"
Step 3:
Once the data is available on your destination table you can now alter the table with the below SQL command:
ALTER TABLE <Table Name>
ADD <Identity Column> BIGINT IDENTITY(1,1)
Adding Few SQL Statement to help you understand Update-Insert Script for Incremental Load.
CREATE TABLE Employees
(
ID INT IDENTITY(1,1),
Name VARCHAR(100),
Salary INT,
InsertDate DATETIME,
UpdateDate DATETIME
)
INSERT INTO Employees
VALUES
('Kristeen',1420,NULL,NULL)
,('Ashley',2006,NULL,NULL)
,('Julia',2210,NULL,NULL)
,('Maria',3000,NULL,NULL)
CREATE PROCEDURE dbo.InsertOrUpdateEmployee
#Name VARCHAR(100),
#Salary INT
AS BEGIN
CREATE TABLE #tmpData
(
Name VARCHAR(50),
Salary INT
)
INSERT INTO #tmpData(Name,Salary)
VALUES(
#Name,
#Salary
)
UPDATE A
SET A.Name = B.Name,
A.Salary = B.Salary,
A.updatedate = GETDATE(),
A.IsNewRecord = 0
FROM Employees A
JOIN #tmpData B
ON A.Name = B.Name
AND A.Salary = B.Salary
INSERT INTO Employees
(
Name,
Salary,
InsertDate,
IsNewRecord
)
SELECT
S.Name,
S.Salary,
GETDATE(),
1
FROM #tmpData S
LEFT JOIN Employees D
ON S.Name = D.Name
AND S.Salary = D.Salary
WHERE D.Name IS NULL
AND D.Salary IS NULL
DROP TABLE #tmpData
END
EXEC InsertOrUpdateEmployee 'Gaurav',4500000
You need to modify a bit with the code above as the above code is to insert the data through SP parameter, but in your case, you might need to use the Source Table in place of a temporary table and in the end you can truncate the source table after moving the complete data into the Destination table.
The issue is that you are trying not passing last column, which is an INT column.
"-E Specifies that identity value or values in the imported data file
are to be used for the identity column. If -E is not given, the
identity values for this column in the data file being imported are
ignored."
You have three options...
Add an INT column to the source data as the first row and have it incremented like an IDENTITY would be incremented and continue to pass the -E option. Doing this will allow the data from the source to be used as the IDENTITY column.
Add a random INT to the last column of your source data, say 1 for every row, then do not pass in the -E. According to the documentation, when -E is not provided it will ignore the values for the identity column and start at the currently seeded value and auto-increment.
Leverage a format file to specify which columns from your data file go into which columns in our SQL table.
How to specify the format file
How to construct a format file
Updated Answer
When you don't have option to modify the source data, then please remove the identity columns and perform as below:
- Remove Identity Column from the table
- Do your Import
- After successful of import, please add the identity column as below:
Alter Table Names
Add Id_new BigInt Identity(1, 1)
Go
As Marc_s mentioned here
Don't BULK INSERT into your real tables directly.
I would always
insert into a staging table dbo.Employee_Staging (without the IDENTITY column) from the CSV file
possibly edit / clean up / manipulate your imported data
and then copy the data across to the real table with a T-SQL statement like:
INSERT INTO dbo.Employee(Name, Address)
SELECT Name, Address
FROM dbo.Employee_Staging

Import data into multiple tables from excel sheet in sqlserver

I am able to populate excel sheet's data into a datatable , that table is tempInput (sql fiddle)
Now from that table I want to split data into two tables tbl1,tbl2 (fiddle)
I am facing problem while inserting into tbl2.. let me show you those tables first
create table tempInput
(
question_text nvarchar(100),
description nvarchar(100),
option_1 nvarchar(20),
option_2 nvarchar(20),
option_3 nvarchar(20),
option_4 nvarchar(20),
right_option nvarchar(50)
)
create table tbl1
(
question_id int primary key identity(1,1),
name nvarchar(100),
description nvarchar(100)
)
create table tbl2
(
option_id int IDENTITY(1,1) primary key ,
option_text nvarchar(30),
is_right_option bit,
question_id int
)
lets populate some sample data into tempInput
now I have to split sampleInput's data into two tables as follows..
How can I do that, please find it here

add date in bulk insert

I have a text file that I receive from a partner. The file gets imported into our SQL using a temp table in a SQL Job. The process is we create a temp table, then bulk insert the text file into the temp file and then into the SQL table. Here is what it looks like:
CREATE TABLE #apt_mkdn(
[PJ_RES_ID_PRG] [int] NULL,
[PJ_REBATE_ID] [int] NULL,
[PJ_REBATE_DATE] [datetime] NULL
)
bulk insert #apt_mkdn from '\markdown.txt'
with (FIELDTERMINATOR='|',ROWTERMINATOR='0x0a', FIRSTROW=2)
go
insert into PROJECT_INFO(PJ_RES_ID_PRG,PJ_REBATE_ID,PJ_REBATE_DATE)
SELECT
PJ_RES_ID_PRG,PJ_REBATE_ID,PJ_REBATE_DATE FROM #apt_mkdn
go
drop table #apt_mkdn
go
In the table PROJECT_INFO there is a field for import date. What I need to do is to add this date using getdate() or similar function to the table for all of the records that have just been inserted. I hope this makes sense.
I have tried to add importdate to both the insert statement and the select statement like
insert into PROJECT_INFO(PJ_RES_ID_PRG,PJ_REBATE_ID,PJ_REBATE_DATE, ImportDate)
SELECT
PJ_RES_ID_PRG,PJ_REBATE_ID,PJ_REBATE_DATE, getdate()
FROM #apt_mkdn
go
I get errors each time.

Cannot insert explicit value for identity column

I am migrating my application form one database to other with keeping table structure as it is. I am creating same tables in new table and inserted value using db link.
I am getting error message like "Cannot insert explicit value for identity column in table 'XYZ' when IDENTITY_INSERT is set to OFF." because table XYZ have ScreenConfigSettingAccessId as an identity column
Below is the script I am using for creating table and inserting value
CREATE TABLE [dbo].[XYZ](
[ScreenConfigSettingAccessId] [int] IDENTITY(1,1) NOT NULL,
[APP_ID] [int] NOT NULL,
[ScreenConfigSettingId] [int] NOT NULL,
[RSRC_ID] [char](20) NOT NULL)
)
INSERT INTO [dbo].[XYX]
(
[ScreenConfigSettingAccessId] ,
[APP_ID] ,
[ScreenConfigSettingId] ,
[RSRC_ID]
)
SELECT
[ScreenConfigSettingAccessId] ,
[APP_ID] ,
[ScreenConfigSettingId] ,
[RSRC_ID]
FROM [olddatabase].[database name].[dbo].[XYX]
in old table the value of ScreenConfigSettingAccessId is 3 and 4.
I want to inset the same data which old table have so set IDENTITY_INSERT to ON and tried but it still not allowing to insert.
Looking for you suggestions
You need to specify the table. Check out the command syntax in SQL Books Online: SQL 2000 or SQL 2012 (the syntax hasn't changed).

Resources