I'm trying to bulk insert a table with the code below:
DROP TABLE #temp_FeirasLivres
CREATE TABLE #temp_FeirasLivres
(
ID INT null,
LONG BIGINT null,
LAT BIGINT null,
SETCENS BIGINT null,
AREAP BIGINT null,
CODDIST INT null,
DISTRITO NVARCHAR(100) null,
CODSUBPREF INT null,
SUBPREFE NVARCHAR(200) null,
REGIAO5 NVARCHAR(200) null,
REGIAO8 NVARCHAR(200) null,
NOME_FEIRA NVARCHAR(200) null,
REGISTRO NVARCHAR(50) null,
LOGRADOURO NVARCHAR(100) null,
NUMERO NVARCHAR(200) null default('S/N'),
BAIRRO NVARCHAR(50) null default(''),
REFERENCIA NVARCHAR(100) null
)
BULK INSERT #temp_FeirasLivres
FROM 'DEINFO_AB_FEIRASLIVRES_2014.csv'
WITH
(
FORMAT = 'CSV',
FirstRow = 1
);
The content of file has 880 rows, but I'll show here enough to validate what I'm saying:
879,-46610849,-23609187,355030827000078,3550308005044,27,CURSINO,13,IPIRANGA,Sul,Sul 1,CERRACAO,4025-8,RUA LINO GUEDES,109.000000,MOINHO VELHO,ALTURA DA VERGUEIRO 7450
880,-46450426,-23602582,355030833000022,3550308005274,32,IGUATEMI,30,SAO MATEUS,Leste,Leste 2,JD.BOA ESPERANCA,5171-3,RUA IGUPIARA,S/N,JD BOA ESPERANCA
The error is about the last row has fewer columns than the other rows (there is no, after the previous value).
If I put a "," after BOA ESPERANCA, it works, but I want to know if there is anything I can do on source to save time from always opening and fixing the CSV file.
PS: The last row has a line breaker after it, and I've tried with rowterminator on bulk options, but can try again.
As #Larnu saids in comments:
SQL Server expects the file to be well formed; that means that it has to have the same amount of columns in every row. if the file is malformed (which is appears to be), you'll need to fix the file first, and then BULK INSERT it.
So, it's the best answer.
Related
I have a SQL Server database with the following table:
CREATE TABLE [dbo].[Kanji] (
[KanjiId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
[Text] NVARCHAR (5) NOT NULL,
[Freq] INT NULL,
[Grade] INT NULL,
}
There are a large number of rows in this table and I would like to be able to select just 10%. Ideally I would like to have a column (maybe a computed column) in the table that was something like a hash value for the contents of the [Text] column. A hash that was a number from 0-9 so I could then select all rows that had this value.
Is there some way that I can create a column like this?
Try this:
CREATE TABLE [dbo].[Kanji] (
[KanjiId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
[Text] NVARCHAR (5) NOT NULL,
[Freq] INT NULL,
[Grade] INT NULL,
[Hash] AS (CONVERT([bigint],hashbytes('md5',[Name]))%(5)+(5)),
}
You'll get values from -9 to 9, for example.
The problem I have been provided is:
Write the DDL to construct the Facebook User and Post tables in SQL server. You need to populate the user table with the user’s information provided as in Figure 1 and additional attributes may be needed for the design. Use the ID in the excel file to identify the other Facebook user. For example ID 612831408762037 is David Cunliffe. Import the excel sheets to SQL server and then write effective SQL to merge the two tables into the Post table.
The code I have written so far is:
IF EXISTS(
SELECT *
FROM sys.tables
WHERE name = N'FacebookUser'
)
DROP TABLE FacebookUser;
IF EXISTS(
SELECT *
FROM sys.tables
WHERE name = N'Post'
)
DROP TABLE Post;
CREATE TABLE FacebookUser
(UserID varchar(15) primary key not null,
UserName varchar(30) not null,
UserDescript varchar(500),
JoinDate date not null,
HomeTown varchar(30),
Affiliation varchar(30),
Country varchar(30),
CurrentOffice varchar(100),
Gender varchar(6) not null,
Email varchar(30),
OtherAccounts varchar(100),
Website varchar(500),
);
CREATE TABLE Post
(id char(28) not null,
message varchar(8000) not null,
type varchar(15) not null,
created_time datetime not null,
updated_time datetime not null,
shares.count int not null,
count of likes.name int not null,
count of comments.message int not null,
Link varchar(50) not null,
name varchar(50) not null,
Description varchar(200) not null,
);
INSERT INTO FacebookUser
VALUES('612831408762037', 'Some Name', 'Some very long text to insert in my row just for test cases',
'02/18/2009', 'New Lynn/Auckland', 'New Zealand Labour Party', 'New Zealand', 'Office: Member of Parliament for New Lynn Party: New Zealand Labour Party', 'Male',
'Mailadress#example.com', 'Some Name (Other Service)', 'www.example.com');
INSERT INTO FacebookUser
VALUES('1119736203', 'Another Name', 'Some other example text, with some extend ', '02/20/2008', NULL,
NULL, 'New Zealand', 'Office: Prime Minister Party: NZ National Party', 'Male', 'a.someone#example.com', NULL,
'http://example.com');
I have imported the data, but am unsure how merge the data into the second table, and I get errors on these lines of code:
shares.count int not null,
Link varchar(50) not null,
name varchar(50) not null,
Description varchar(200) not null,
I'm completely lost from here. Is anybody able to help?
You will have to use square brackets for field names which are keywords or containing spaces or invalid characters
[shares.count] int not null,
[count of likes.name] int not null,
[count of comments.message] int not null,
In addition you will have to make sure that the fields are big enough to keep the values you want to store, other ways you will get errors like String or binary data would be truncated.
maybe I can get some feedback from some folks on this. I created two tables and inserted data into one table and i put a constraint (Foreign key) on the table std_individual_address.
I get the following error message when I try to execute the insert now:
Msg 515, Level 16, State 2, Line 43 Cannot insert the value NULL into
column 'individual_GUID', table 'ABLE.dbo.std_individual_address';
column does not allow nulls. INSERT fails. The statement has been
terminated.
Here is all my code:
--Create the std_individual table
CREATE TABLE std_individual(
individual_GUID INT NOT NULL IDENTITY,
individual_First_Name VARCHAR(50) NULL,
individual_Last_Name VARCHAR(50) NULL,
individual_email VARCHAR(40) NULL,
PRIMARY KEY (individual_GUID));
--Create the std_individual_address table
CREATE TABLE std_individual_address
(
individual_address_GUID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
individual_address_line1 VARCHAR(100) NULL,
individual_address_line2 VARCHAR(100) NULL,
individual_address_line3 VARCHAR(100) NULL,
individual_address_city VARCHAR(50) NULL,
individual_address_state VARCHAR(30) NULL,
individual_address_zipcode VARCHAR(30) NULL,
individual_GUID INT NOT NULL,
CONSTRAINT fk_std_individual_address_std_individual FOREIGN KEY (individual_GUID) REFERENCES std_individual (individual_GUID)
)
--Insert Individual Data
INSERT INTO std_individual
(individual_First_Name,individual_Last_Name,individual_email)
VALUES
('Terry','Smith','tsmith#example.net'),
('Ronald','Smegan','ronald#example.net'),
('Arnold','Aggassi','aaggassi#example.edu'),
('Jerry','Brukheimer','bbrukheimer#example.edu');
--Mind the Constraint
INSERT INTO std_individual_address(individual_GUID) SELECT individual_GUID from std_individual
--Attempt to insert rest of the data
INSERT INTO std_individual_address
(individual_address_line1,individual_address_line2,individual_address_city,individual_address_state,
individual_address_zipcode )
VALUES
('8200 Greensboro Drive','Ste 1500','Mclean','Virgina','22102'),
('1121 14th Street, NW','Ste 1000','Washington' ,'District of Columbia','20005'),
('1700 Connecticut Ave,NW','Ste 300','Washington' ,'District of Columbia','20009'),
('205 Pennsylvania Ave,SE','','Washington','District of Columbia','20003');
Then I get the error message above. Any ideas on how to combat that issue?
Please help, how to correct this procedure??
Right now, I'm getting an error:
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.
Code:
CREATE PROCEDURE SP_FILEUPLOAD
#UID int,
#APPCODE varchar(50) = NULL ,
#APPEXPIRED datetime = NULL ,
#SIGNIMAGE image = NULL ,
#SIGNFILE varbinary(MAX) = NULL ,
#HEADUID int,
#PRINCIPALFROM money,
#PRINCIPALTO money,
#EXCEPTIONUSER bit,
#LastUpdate datetime
AS
BEGIN
SET NOCOUNT ON
INSERT INTO APPUSERDTL
(UID,
APPCODE,
APPEXPIRED,
SIGNIMAGE,
SIGNFILE,
HEADUID,
PRINCIPALFROM,
PRINCIPALTO,
EXCEPTIONUSER,
LastUpdate)
VALUES('#UID',
'#APPCODE',
'#APPEXPIRED',
'#SIGNIMAGE',
'#SIGNFILE',
'#HEADUID',
'#PRINCIPALFROM',
'#PRINCIPALTO',
'#EXCEPTIONUSER',
'#LastUpdate')
END
GO
Below is the table structure
[dbo].[APPUSERDTL](
[UID] [int] NOT NULL,
[APPCODE] [varchar](50) NULL,
[APPEXPIRED] [datetime] NULL,
[SIGNIMAGE] [image] NULL,
[SIGNFILE] [varbinary](max) NULL,
[HEADUID] [int] NULL,
[PRINCIPALFROM] [money] NOT NULL,
[PRINCIPALTO] [money] NOT NULL,
[EXCEPTIONUSER] [bit] NOT NULL,
[LastUpdate] [datetime] NOT NULL )
It seems you are passing a varchar value to #SIGNFILE parameter.
Please check the value you are passing
eg. You can replicate this issue
declare #test varbinary(max)=''
select #test
Update
Why you are passing with '' to insert statement? It will consider all the values as varchar if you use within quotes. Remove quotes and try
You can do as below
INSERT INTO APPUSERDTL
(UID,
APPCODE,
APPEXPIRED,
SIGNIMAGE,
SIGNFILE,
HEADUID,
PRINCIPALFROM,
PRINCIPALTO,
EXCEPTIONUSER,
LastUpdate)
VALUES(#UID,
#APPCODE,
#APPEXPIRED,
#SIGNIMAGE,
#SIGNFILE,
#HEADUID,
#PRINCIPALFROM,
#PRINCIPALTO,
#EXCEPTIONUSER,
#LastUpdate)
When the SQL Server trying to perform conversion from the empty string ('') to the varbinary(max) the error occurred. The only way to do is to avoid implicit conversion. If you need to convert the empty string to varbinary type, use the cast(#your_variable as varbinary(max)) construction (in the above code where you calling to you SP SP_FILEUPLOAD from).
I'm fairly new to SQL Server any input and advice would help greatly.
I have 3 tables which are in one-to-many relationships.
Table Person holds customer info
CREATE TABLE [dbo].[Person](
[PID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](255) NULL,
[LastName] [varchar](255) NULL,
[CAddress] [varchar](255) NULL,
[Ccity] [varchar](255) NULL,
[Cstate] [varchar](2) NULL,
[Czipcode] [varchar](20) NULL,
[Ccountry] [varchar](255) NULL,
[Cphone] [varchar](25) NULL,
[Cemail] [varchar](255) NULL,
[CipAddress] [varchar](255) NULL)
Table Transaction holds their transaction
CREATE TABLE [dbo].[Transaction](
[TID] [int] IDENTITY(1,1) NOT NULL,
[PID] [int] NOT NULL,
[DateOfTransaction] [date] NULL)
with a third table, TransactionDetail, which holds transaction details
CREATE TABLE [dbo].[TransactionDetail](
[TDID] [int] IDENTITY(1,1) NOT NULL,
[TID] [int] NULL,
[ProductID] [int] NULL,
[ProductName] [varchar](255) NULL,
[ProductQTY] [int] NULL,
[ProductPrice] [decimal](18, 2) NULL)
I would like to create a stored procedure to insert once into the Person table then insert multiple details into the third table.
this is what i got i'm not sure if this is correct?
CREATE TYPE dbo.TransactionTableType AS TABLE
( TID int, ProductID int, ProductName varchar(255), ProductQTY int, ProductPrice decimal(18,2) )
go
CREATE PROCEDURE insertTransacion
#NewProduct dbo.TransactionTableType READONLY,
#FirstName varchar(255),
#LastName varchar(255),
#CAddress varchar(255),
#Ccity varchar(255),
#Cstate varchar(2),
#Czipcode varchar(20),
#Ccountry varchar(255),
#CPhone varchar(25),
#Cemail varchar(255),
#CipAddress varchar(255),
#DateOfTrans date
as
begin
SET NOCOUNT ON;
DECLARE #Pid int
insert into Person(FirstName,LastName,CAddress,Ccity,Cstate,Czipcode,Ccountry,Cphone,Cemail,CipAddress) values (#FirstName,#LastName,#CAddress,#Ccity,#Cstate,#Czipcode,#Ccountry,#CPhone,#Cemail,#CipAddress)
SET #Pid = SCOPE_IDENTITY()
insert into PTransactions(PID, DateOfTransaction) values (#Pid, #DateOfTrans)
DECLARE #Tid int
SET #Tid = SCOPE_IDENTITY()
insert into TransactionDetail(TID, ProductID, ProductName, ProductQTY, ProductPrice) Select #Tid, ntd.ProductID, ntd.ProductName, ntd.ProductQTY, ntd.ProductPrice from #NewProduct as ntd
end
Not sure how to do this in a stored procedure I know how to do it programmatically in asp.net using ado, however I'm trying to avoid that. Sorry for the grammar.
Short answer is you can't, although there are a couple of options open to you.
You can either create a SP to enter the person data, and a separate one to insert the data - one line at a time. Return the person id value with the first call and use that in the subsequent SP calls to insert the data. If you go down this path, make sure you wrap the calling code up in transaction objects so you can roll back the whole lot if you have a problem. You don't state what language you're using for the rest of your code?
Second option is to look at the SQL Bulk Insert command - this is best if you have a lot of data to add into the third table. But involves writing that data out to a file first - slight pain but it's then very fast. Very good if you have thousands or more rows to add.
Couple of other options out there as well depending on your development language.
Cheers
Simon