I am trying to add this name -> NumāTwó into a table in MS sql server along with the accents. But it is only getting inserted as -> NumaTwó (without ā). I tried many encodings but doesn't seem to work. I have given the DDL of the table below. Please help
CREATE TABLE [dbo].[test](
[testname] [nvarchar](40) COLLATE SQL_Latin1_General_CP1253_CI_AI NULL
) ON [PRIMARY]
----------- Insert-----------
insert into test values ('NumāTwó');
use N as Prefix for Unicode character
CREATE TABLE [dbo].[test](
[testname] [nvarchar](40) COLLATE SQL_Latin1_General_CP1253_CI_AI NULL
) ON [PRIMARY]
----------- Insert-----------
insert into test values (N'NumāTwó');
Try to use N before the string while inserting like this:
insert into test values (N'NumāTwó');
Related
Is there a way to "bypass" the SQL collate logic? I want two different bytearrays to give me two different keys no matter what and without SQL trying to collate them.
I prefer to use the nvarchar data type if possible.
The most generic COLLATE setting I have found is 'Latin1_General_100_BIN2' but even that gives me a conflict in this example:
CREATE TABLE [dbo].[test] (
[Feature] [nvarchar](450) COLLATE Latin1_General_100_BIN2 NOT NULL,
CONSTRAINT [PK_dbo.test] PRIMARY KEY CLUSTERED ([Feature] ASC)
)
insert into test values ('a')
insert into test values ('a ')
I get the error
Violation of PRIMARY KEY constraint 'PK_dbo.test'. Cannot insert duplicate key in object 'dbo.test'. The duplicate key value is (a ).
I am using the SQL server in MS Azure.
I do not recommend that you use the Feature field as the primary key.
If possible, you can try to use below script.
declare #tb table(id int identity,name varchar(20))
insert into #tb(name) values('a'),(' a'),('a ')
select * from #tb --where name=' a' and datalength(name)=datalength(' a')
Running result:
In the database, for spaces, currently only len and datalength can be used to filter data. When inserting data in the primary key, space characters should be filtered out. So it should not be supported.
For more details or questions, you can raise a support on the portal, and the official will give you documentation or a satisfactory answer.
A bit of an odd requirement this one.
There is a column on a database (SQL) that is nullable.
I have a constrain in place that populates the column is null/default is provided and it populates from a sequence.
Is it possible to put a constraint in place that ignores any data provided by the insert statement and always puts in the next sequence value?
my current table/constraint is:
CREATE TABLE [dbo].[testmembership](
[id] [int] NOT NULL,
[name] [nvarchar](50) NOT NULL,
[membershipno] [nvarchar](50) NULL
) ON [PRIMARY]
alter table testmembership
add constraint DF_mytblid
default
'PREFIX-'+cast((next value for membershipseq) as nvarchar(50))
for membershipno
If I do the following:
insert into testmembership (id,name,membershipno) values (12,'test',default)
it yeilds the correct sequence generated value.
However, I want it to still have that value from the sequence even if i call this:
insert into testmembership (id,name,membershipno) values (12,'test','ignoreme')
I don't think you can do what you want using a constraint, but you can define a trigger that replaces the normal insert behaviour using the instead of option:
Something like this might work:
create trigger tr on testmembership instead of insert as
insert testmembership (id, name, membershipno)
select id, name, 'PREFIX-' + cast((next value for membershipseq) as nvarchar(50))
from inserted;
When I attempt to create a database diagram, I get the following error:
Cannot insert the value NULL into column 'diagram_id', table 'MyDB.dbo.sysdiagrams'; column does
not allow nulls. INSERT fails.
The statement has been terminated.
The 'sp_creatediagram' procedure attempted to return a status of NULL, which is not allowed. A status of
0 will be returned instead. (.Net SqlClient Data Provider)
I am using SSMS 2012.
The database is set at a compatibility level of SQL Server 2012 (110)
##Version is Microsoft SQL Server 2012 - 11.0.5343.0 (X64)
Your problem is the diagram_ID when the table was created probably looked something like this
CREATE TABLE <table_name>
( diagram_ID INT NOT NULL PRIMARY KEY,
n...,
)
This basically means that a NULL value cannot be inserted into that column because of the NOT NULL condition. So an insert statement like:
INSERT INTO <table_name>
(Diagram_ID, n...,)
VALUES
(NULL, n...,)
Would fail because of the NULL you would need to have a value in there like (since I called it an integer):
INSERT INTO <table_name>
(Diagram_ID, n...,)
VALUES
(23, n...,)
The column may also be an indentity column in which case you have no controll over what can be inserted into the table.
Go to system tables and look for systemdiagrams table, and turn to YES the "indentity Specification" property for the field diagram_id
Hope this will help you, this script solved my issues
DROP TABLE dbo.sysdiagrams;
GO
CREATE TABLE [dbo].[sysdiagrams]
(
[name] [sysname] NOT NULL,
[principal_id] [int] NOT NULL,
[diagram_id] [int] IDENTITY(1,1) PRIMARY KEY,
[version] [int] NULL,
[definition] [varbinary](max) NULL,
CONSTRAINT [UK_principal_name] UNIQUE ([principal_id],[name])
);
GO
EXEC sys.sp_addextendedproperty
#name=N'microsoft_database_tools_support',
#value=1 ,
#level0type=N'SCHEMA',
#level0name=N'dbo',
#level1type=N'TABLE',
#level1name=N'sysdiagrams';
GO
I have a comma delimited text file with the structure
field1 field2 field3 field4
1 2 3 4
I wrote the following script to bulk insert the text file, but I wanted to leave out column 3
create table test (field1 varchar(50),field2 varchar(50),field4 varchar(50))
go
bulk insert test
from 'c:\myFilePath'
with
(fieldterminator=',',
rowterminator='\n'
)
The insert worked fine, but the results of the insert made field4 look like
field3,field4, so the field 3 was actually just concatenated onto field4. The flat files I'm working with are several gigs and can't be easily modified. Is there a way to use bulk insert but have it ignore the columns that aren't declared in the create table statement?
The easiest way is to create a view that has just the columns you require.
Then bulk insert into that view.
Example:
create table people (name varchar(20) not null, dob date null, sex char(1) null)
--If you are importing only name from list of names in names.txt
create view vwNames as
select name from people
bulk insert 'names.txt'
You can use a format file to do this:
http://msdn.microsoft.com/en-gb/library/ms178129.aspx
http://msdn.microsoft.com/en-gb/library/ms179250.aspx
Or if you want a slightly cheekier way, just import it all and drop a column afterwards. ;)
you cant ignore a field while doing bulk insert , insted of doing that .. Load all 4 column and drop the colum which you dont want
create table test (field1 varchar(50),field2 varchar(50), field3 varchar(50),field4 varchar(50))
go
bulk insert test
from 'c:\myFilePath'
with
(fieldterminator=',',
rowterminator='\n'
)
ALTER TABLE test DROP column [field3]
You can create a temporary table and insert any data there. After that, you can do whatever you want with it.
CREATE TABLE #TmpTable(
[CategoryId] [int] IDENTITY(1,1) NOT NULL,
[ParentCategoryId] [int] NULL,
[CategoryTypeId] [int] NOT NULL,
[Name] [varchar](255) NOT NULL,
[Code] [varchar](255) NOT NULL,
[AlternateName] [varchar](900) NOT NULL
)
BULK INSERT [dbo].[#TmpTable]
FROM 'C:\tmp\Categories.csv'
WITH
(
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '0x0a' --Use to shift the control to next row
)
-- original table insert
INSERT INTO [dbo].[Categories]
(
CategoryId,
ParentCategoryId,
CategoryTypeId,
Name,
Code,
AlternateName,
IsDeleted,
SystemCreated
)
SELECT
CategoryId,
ParentCategoryId,
CategoryTypeId,
Name,
Code,
AlternateName,
0, -- custom value missed by file
GETDATE() -- custom value missed by file
FROM #TmpTable
-- remove tmp table
DROP TABLE #TmpEventCategories
In MySql I could have a table with an auto increment column and insert that way:
INSERT INTO tbl VALUES(null, "bla", "bla");
Is there any way to do this with Microsoft SQL Server Express?
Instead of this:
INSERT INTO tbl(`v1`, `v2`) VALUES ("bla", "bla");
Thanks
In Sql Server, you do not need to specify a value for identity columns (I'm guessing this is what the null in mysql is doing).
So, the short syntax for inserting into your table would be:
Insert Into tbl Values('bla', 'bla')
This works regardless of whether the column is declared to be the primary key of the table and also works for any position of the column in the table. That is, even if the column is in the middle as shown below:
Create Table tbl (
[Col1] [varchar](50) NULL,
[Id] [int] IDENTITY(1,1) NOT NULL,
[Col2] [varchar](50) NULL
)
Sql Server will still quite happily interpret the insert statement and take care of the identity insert.
As other posters have mentioned, in Sql Server you actually need to issue the Set Identity_Insert tbl On command to be able to specify a value for identity columns.
I think you are asking whether you can include a reference to the identity column in your insert statement and pass a null or some other magic value in the Values or Select clause. No, you cannot. You cannot pass a value for the identity column unless you use SET IDENTITY_INSERT [table] ON and pass an actual value.
INSERT INTO tbl VALUES(null, 'bla', 'bla');
Works; use single quotes.
From http://msdn.microsoft.com/en-us/library/ms174335.aspx
"If the values in the Value list are
not in the same order as the columns
in the table or do not have a value
for each column in the table,
column_list must be used to explicitly
specify the column that stores each
incoming value."