integrating data into DB shows question marks as value - sql

I have a dataset named "supplier_dim" in an excel sheet, and one of the columns "SUPPLIER NAME" has names in Russian supplier_name_data_input
So when I tried creating a table to integrate data into:
create table supplier_DIM
(
ID_supplier int primary key identity (1,1),
supplier_name nvarchar(50),
supplier_code varchar(50)
)
Then I inserted data into this table:
insert into supplier_DIM (supplier_name, supplier_code)
values ('Шпаркасе Лизинг ДОО', 'DC000325')
I get this result when I select all columns:
How can I fix the question mark value problem?

'Шпаркасе Лизинг ДОО' is a varchar literal, and will not preserve the characters in a database without a special collation. Instead use an nvarchar literal, eg
supplier_DIM (supplier_name,supplier_code) values (N'Шпаркасе Лизинг ДОО','DC000325')

Related

Populate snowflake table with default values without selecting default column values from the file data

I am trying to load a table (drop table and load the data - similar to truncate and load) dynamically. Let us assume that table needs to have 4 fields, ID, Name, SeqNo, and DtTimeStamp.
The data is being selected from an externally staged csv\text file that has only two fields (ID and Name). The below query gives an error for the nonmatching of a number of columns. How to resolve that issue?
CREATE OR REPLACE TABLE SOMETABLENAME(ID NUMBER(38,0), Name
VARCHAR(255), SeqNo NUMBER(38,0) NOT NULL AUTOINCREMENT, DtTimeStamp
TIMESTAMP_NTZ(9) NOT NULL DEFAULT CURRENT_TIMESTAMP()) AS SELECT A.$1
AS ID, A.$2 AS Name FROM #EXTERNALSTAGE/SOME_FILE.CSV A;
If you carefully look at the above SQL statement, my table has two extra fields that need to be auto-populated for every row it loads. But I am unable to make it work?
Any suggestions are highly appreciated.
Thanks in Advance!
Sathya
CREATE TABLE … AS SELECT (CTAS)
CREATE TABLE <table_name> ( <col1_name> , <col2_name> , ... ) AS SELECT ...
The number of column names specified must match the number of SELECT list items in the query; the types of the columns are inferred from the types produced by the query.
To resolve it, CTAS and INSERT INTO could be two separate steps:
CREATE OR REPLACE TABLE SOMETABLENAME(
ID NUMBER(38,0),
Name VARCHAR(255),
SeqNo NUMBER(38,0) NOT NULL AUTOINCREMENT,
DtTimeStamp TIMESTAMP_NTZ(9) NOT NULL DEFAULT CURRENT_TIMESTAMP()
);
-- here INSERT/SELECT have matching column list
INSERT INTO SOMETABLENAME(ID, Name)
SELECT A.$1 AS ID, A.$2 AS Name FROM #EXTERNALSTAGE/SOME_FILE.CSV A;

SQL Server 2014 : help creating tables

I am new to MSSQL 2014 Server, my professor listed these steps to make a table, I don't know the proper steps to create tables in the pictures listed below, please help.
Create and populate (insert values) the following tables per table description and data values provided
DEPARTMENT
EMPLOYEE
PROJECT
ASSIGNMENT
Add a SQL Comment to include /* * Your First Name_Your Last Name* */ when inserting corresponding values for each table.
What I tried so far:
CREATE TABLE DEPARTMENT(
DepartmentName Text(35) PRIMARY KEY,
BudgetCode Text(30) NOT NULL,
OfficeNumber Text(15) NOT NULL,
Phone Text(12) NOT NULL, );
I have put this to my query and the error is
Msg 2716, Level 16, State 1, Line 1 Column, parameter, or variable #1: Cannot specify a column width on data type text.
Try this(I assume that your table exists in dbo schema):
IF OBJECT_ID(N'dbo.DEPARTMENT', N'U') IS NOT NULL
BEGIN
DROP TABLE DEPARTMENT
END
GO
CREATE TABLE DEPARTMENT(
DepartmentName varchar(35) PRIMARY KEY,
BudgetCode varchar(30) NOT NULL,
OfficeNumber varchar(15) NOT NULL,
Phone varchar(12) NOT NULL
);
You can not define width for Text data type. In case which you need to define width you can use char or varchar data types. Also keep in mind that if you need to work with Unicode characters then you will need to use nchar or nvarchar instead.

Can not add a column to existing table

I have a table viz. expenses with three columns as under
ExpenseId int NOT NULL,
ExpenseName varchar(50) NOT NULL,
Invalid bit NOT NULL
To add a new column (OldCode char(4) not null), I used design feature for tables in Microsoft SQL Server Management Studio. But I get following error
'Expenses' table
- Unable to modify table. Cannot insert the value NULL into column 'OldCode', table 'TransportSystemMaster.dbo.Tmp_Expenses'; column does not allow nulls. INSERT fails. The statement has been terminated.
Incidentally I have been able to add same column with same specifications to other tables of the same database.
Any help?
Your Table Consist of Existing Records
and you are pushing a new column of type NOT NULL.
so for older records the data have to be something.
try something like this
ALTER TABLE MY_TABLE ADD Column_name INT NULL
GO
UPDATE MY_TABLE <set valid not null values for your column>
GO
ALTER TABLE MY_TABLE ALTER COLUMN Column_name INT NOT NULL
GO
Since OldCode is NOT NULL, you should specify a default value for it.
when you have some rows on your table you can't add a column that is not nullable you should provide a default value for it
Alter Table table_name add OldCode int not null DEFAULT(0);
You have to specify values for all the 4 fields of the table, its purely because, while designing the table you set the definition of the columns to be not null. Again you are adding a new column called OldCode and setting to be not null, all ready existing records hasn't got a value. So that is the reason its complains

why can't I insert data into tables I created in sql server managment studio 2012? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I created the tables with this:
CREATE TABLE COSTUMER(
COSTUMER_ID INT,
TAXI_ID INT,
COSTUMER_PHONE_NUMBER INT,
COSTUMER_NAME VARCHAR(40)
DESTINATION VARCHAR(40)
);
CREATE TABLE TAXI(
TAXI_ID INT,
COSTUMER_ID INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_PHONE_NUMBER INT,
DRIVER_NAME INT,
DESTINATION
);
CREATE TABLE BOOKING(
DESTINATION VARCHAR(40),
TAXI_ID INT,
COSTUMER_ID,
COSTUMER_PHONE_NUMBER INT,
DRIVER_PHONE_NUMBER INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40)
COSTUMER_NAME VARCHAR(40)
TIME_OF_ARRIVAL DATETIME,
);
After this my table appeared with all the attributes. Then I set my PKs and FKs manually.
now am trying to insert some data to each of the tables but I keep getting error 213:
Column name or number of supplied values does not match table definition
I searched and searched and still don't understand.
also when I insert the code for such as SELECT * FROM Costumer (that's the table name) it won't show in the small suggestion box as if it doesn't exist???
The below code should work for your requirement unless you have specified an identity column manually as you said that you made some changes manually.
In case you specified an identity column manually then you should not insert the value for the Identity column (say Customer_ID) or you should set SET IDENTITY_INSERT COSTUMER ON before inserting the ID column
CREATE TABLE COSTUMER(
COSTUMER_ID INT,
TAXI_ID INT,
COSTUMER_PHONE_NUMBER INT,
COSTUMER_NAME VARCHAR(40),
DESTINATION VARCHAR(40)
);
SELECT * FROM COSTUMER
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID,COSTUMER_PHONE_NUMBER,COSTUMER_NAME,DESTINATION) VALUES (1, 33,123211241, 'John','Mexico' )
I think you are trying to insert more/less input to table
INSERT INTO [TableName] (Column1, Column2, Column3)
VALUES (Value1, Value2, Value3)
Number of columns should be equal to Number of Values. Please check your INSERT statement.
If you not included (Column1, Column2, Column3) in your statement, include it.
Post the insert statement you used. I see a few problems with the current code you posted.
You don't provide datatypes for a few of your attributes, for instance in the Booking table you need to provide some type for the CUSTOMER_ID attribute. Also you need the attributes and datatypes to be comma separated, except the last one doesn't need a trailing comma. For instance you have:
...
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40)
COSTUMER_NAME VARCHAR(40)
TIME_OF_ARRIVAL DATETIME,
...
This will throw an error probably saying incorrect syntax near COSTUMER_NAME.
You need something like:
CREATE TABLE BOOKING(
DESTINATION VARCHAR(40),
TAXI_ID INT,
COSTUMER_ID INT,
COSTUMER_PHONE_NUMBER INT,
DRIVER_PHONE_NUMBER INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40),
COSTUMER_NAME VARCHAR(40),
TIME_OF_ARRIVAL DATETIME
);
For an insert try something along the lines of:
INSERT INTO <table_name> (<column1>,<column2>,<column3>)
VALUES (<col1Value>,<col2Value>,<col3Value>)
If you aren't getting auto completion you might try and refresh the DB in the management studio by right click > refresh, but I wouldn't rely on auto completion at all in SQL Server nor IntelliSense unless you use someting like this.
If you post the insertion script I might be able to help more.
Update: So, the problem you got was with arithmetic overflow, specifically trying to put 07854625781 into a int. All datatypes have maximum/minimums or other "constraints", an int can be in the range of -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647) (4 bytes). The value you enter exceeds that, it is being interpreted as 07,854,625,781 (7.8 billion) while the maximum for an int is only 2.147 billion. If you change the datatype of phone number to bigint that can hold -2^63 (-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807) (8 bytes) it will work, However I would advise you not to store a phone number in that way, rather, use char(length) if you know that the size and format will always be the same, if you think the length could vary (maybe from extensions or international numbers) I would use something along the lines of varchar(40).
Through this datatype change you will just have to surround input in 'single quotes' when you insert.
Cheers
The Problem that you are facing in the below insert query is the value 07854625781 has become too large to handle for int type. Int range 2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647). So change the datatype of COSTUMER_PHONE_NUMBER to Bigint.
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID, COSTUMER_PHONE_NUMBER, COSTUMER_NAME, DESTINATION) values (1, 32, 07854625781, 'Denzel Washington', 'Heathrow Airport')
use the below query to change the datatype to bigint and then try insert(above command)
ALTER TABLE COSTUMER ALTER COSTUMER_PHONE_NUMBER bigint;
Steps to Follow
ALTER TABLE COSTUMER ALTER COSTUMER_PHONE_NUMBER bigint;
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID, COSTUMER_PHONE_NUMBER, COSTUMER_NAME, DESTINATION) values (1, 32, 07854625781, 'Denzel Washington', 'Heathrow Airport');
Now do
Select * from COSTUMER
you will find the record inserted

unable to insert rows in a new table in sql, getting error as string or binary data would be truncated

Hi I have the below table created
create table person
(
sno int primary key identity(1,1) not null,
firstname nvarchar,
lastname nvarchar,
city nvarchar,
zip int
);
I have written the insert statement as
insert into person
(firstname,lastname,city,zip)
values
('firstname','lastname','city','123456')
but i am getting the following error when i try to insert values into the table
string or binary data would be truncated
The statement has been terminated
Please help to overcome my problem , i am not sure where i did i went wrong.
Please help,
Thanks In Advance.
in your script there are 2 issues:
You did not specify the length of the varchar columns.
Take a look here.
Do not define columns, variables and parameters using VARCHAR, and
NVARCHAR data types without specifying length attribute. This will not
produce a dynamic length string data, but will make SQL Server choose
default length of 1 (NOTE: In some scenarios it the length can be 30).
Replace it with:
create table person
(
sno int primary key identity(1,1) not null,
firstname nvarchar(50),
lastname nvarchar(50),
city nvarchar(50),
zip int
);
2..You are attempting to insert a varchar zip rather as int (which is the columns type).
Replace the insert with:
insert into person
(firstname,lastname,city,zip)
values
('firstname','lastname','city',123456)
Whenever you see this error
string or binary data would be truncated its a column length` issue.
Check the length of the column in the sql field definition
You are trying to insert data of more length than the field length
So either you limit the data to the length in database or increase the column length.
You are inserting a string for an int (zip), try this:
insert into person
(firstname,lastname,city,zip)
values
('firstname','lastname','city',123456)
EDIT:
you should also declare your nvarchar length as it only creates it as a nvarchar(1) if you don't , so change all of your nvarchar's to nvarchar(50) or whatever length you want for max
Per TSQL Technet Article on NVARCHAR:
nvarchar[(n)]: When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.