how to enter values in rowguid column? - sql

can anyone please tell me the right way to insert values in the rowguid column of the table? I m using sql server management studio

use the NEWID() function to generate one:
CREATE TABLE myTable(GuidCol uniqueidentifier
,NumCol int)
INSERT INTO myTable Values(NEWID(), 4)
SELECT * FROM myTable
or you can set it as a default value:
CREATE TABLE myTable(GuidCol uniqueidentifier DEFAULT NEWSEQUENTIALID()
,NumCol int)
INSERT INTO myTable (NumCol) Values(4)
SELECT * FROM myTable

You can Set NEWSEQUENTIALID() as Default in table
CREATE TABLE GuidTable
(
ID UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID() PRIMARY KEY,
TEST INT
)
Read more about it here
http://msdn.microsoft.com/en-us/library/ms189786.aspx

It's a uniqueidentifier column
You can send a value like "6F9619FF-8B86-D011-B42D-00C04FC964FF", or use NEWID/NEWSEQUENTIALID functions to generate one

I was able to insert by using the following:
Insert Into Table FOO(Col1, Col2, RowGuidCol)
Values (5,'Hello,'1A49243F-1B57-5848-AA62-E4704544BB34')
It is very important to follow to place the dashes in the correct place. You do not need to have the 0x in the beginning of the string. FYI-Updates are not allow on columns whith the rowguid col property set. I hope this helps.

Assuming that you have a uniqueidentifier column rg in your table t which is set to be the rowguid:
INSERT INTO TABLE t (rg) VALUES (NEWID())
or
INSERT INTO TABLE t (rg) VALUES (NEWSEQUENTIALID())

Related

generate sequence in sql server 2014

I am trying to generate a sequence varchar text type, but I do not want to have to create another column to get the id to format it and insert it I want to generate it in the same column, help
create table tbl (
Id int identity not null,
CusId as 'CUS' + format(Id, '00000'),
-- ...
)
You can use sequence object that appeared in SQL Server 2012 + default value like this:
create sequence dbo.ids as int
minvalue 1;
create table dbo.tbl (
CusId varchar(100) default 'CUS' + format(NEXT VALUE FOR dbo.ids, '00000'));
insert into dbo.tbl (CusId) default values;
insert into dbo.tbl (CusId) default values;
insert into dbo.tbl (CusId) default values;
select *
from dbo.tbl;
-----
--CusId
--CUS00001
--CUS00002
--CUS00003
Believe the only viable solution is using 2 columns as you mentioned, and discussed here:
Autoincrement of primary key column with varchar datatype in it
Have not seen it achieved in a single column on its own.

How to insert null value to NOT NULL column using the DEFAULT Constraint

These is my example table definition:
CREATE TABLE [MyTable]
(
[ColumnName] [bit] NOT NULL
)
ALTER TABLE [MyTable] ADD DEFAULT ((0)) FOR [ColumnName]
I want to be able to pass a Null value to my stored procedure's #ColumnValue, something like:
#ColumnValue = null;
INSERT INTO [MyTable] ([ColumnName])
VALUES (#ColumnValue)
But I'm getting this error:
"Cannot insert the value NULL into column... INSERT fails with"
Why the DEFAULT constraints not working?
Solved:
as #J.D. Pace said: The default value will be inserted only if the value is not specified on the Insert statement.
so as #dotNET suggested, i have specified the default value in the INSERT query statement using the ISNULL:
ISNULL - The SQL Server ISNULL() function lets you return an alternative value when an expression is NULL:
#ColumnValue = null;
INSERT INTO [MyTable] (
[ColumnName]
)
VALUES (
ISNULL(#ColumnValue, 0)
)
A table with only one column of bit type with a default value seems to be a bad design. This stuff can almost certainly be stored in a different and better way. On the other hand, if there are other columns in the table that you didn't include in the post, just skip this particular column in your INSERT query and it will work fine. Lastly, you can specify the default value in your INSERT query too.

Specify "NEXT VALUE" for INSERT statement using identity column in SQL Server

Consider the following table and SQL from Microsoft's INSERT documentation that deals with IDENTITY columns:
CREATE TABLE dbo.T1 (column_1 int IDENTITY, column_2 VARCHAR(30));
GO
INSERT T1 (column_2) VALUES ('Row #2');
The INSERT statement does not specify column_1 as a column of the table, and SQL Server auto-populates the next value for that identity column. This is the normal way identity columns are handled.
How can I have the same behavior, while also specifying the column name?
For example, I'm looking for something like:
INSERT INTO T1 (column_1, column_2)
VALUES (NEXT VALUE, 'Row #3');
GO
I don't believe NEXT VALUE works here, but is there something that does work? Is there a key token or function that will indicate that the identity column should be used?
Note: the reason I ask is that the framework I'm using requires all columns to be specified in the column list.
If you are on SQL Server 2012 and later, you can use sequence. But you must remove the IDENTITY property from Column1 first. This can only be done by copy-and-rename a new table.
CREATE SEQUENCE Column1_Sequence
AS int
START WITH 0;
CREATE TABLE T1
(
Column1 int DEFAULT (NEXT VALUE FOR Column1_Sequence) PRIMARY KEY
, Column2 nvarchar(30)
)
After that, you can insert data into the table in 2 ways:
INSERT INTO T1 (Column1, Column2)
SELECT NEXT VALUE FOR Column1_Sequence
, 'Row #2'
INSERT INTO T1 (Column2)
SELECT 'Hello world'
Can you set the identity insert on before inserting and then set the identity insert off
You cannot set value for identity column unless you set identity_insert on for this table (one at time). Some examples:
create table #tmp (id int identity(1,1), name varchar(10))
insert #tmp (id,name) values (2,'test')
--error Cannot insert explicit value for identity column in table '#tmp
set identity_insert #tmp on --for one table in DB
insert #tmp (id,name) values (2,'qwas')
select * from #tmp
set identity_insert #tmp off -- good practice
--works
--see current identity value
SELECT IDENT_CURRENT ('#tmp') AS Current_Identity;
--Reset identity value
DBCC CHECKIDENT (#tmp, RESEED, 999)
--next insert will be 1000
Of course, if you reset next identity to a value which conflicts with PK (common usage of identity) you will have Violation of PRIMARY KEY constraint error
I am pretty sure there is no way to do that with SQL Server. Two workarounds that I can think of:
Fix the library if possible.
If the library supports it, you can create a view and INSERT into that instead. For example:
CREATE TABLE MyTable
(
ID INT IDENTITY(1, 1),
SomeColumn VARCHAR(100)
)
GO
CREATE VIEW MyTableView
AS
SELECT SomeColumn
FROM MyTable
GO
INSERT INTO MyTableView (SomeColumn) VALUES ('Test')

Avoid NULL columns using DEFAULT Empty String

how can I create a Column with the default value being an empty string?
thanks guys!
You can read up on the subject here
CREATE TABLE dbo.Test (ID INTEGER, EmptyString VARCHAR(32) DEFAULT '')
INSERT INTO dbo.Test (ID) VALUES (1)
INSERT INTO dbo.Test (ID) VALUES (2)
SELECT * FROM dbo.Test
DROP TABLE dbo.Test
Something like:
CREATE TABLE foobar (string_column VARCHAR(100) NOT NULL DEFAULT '')
In SQL server you can set "Column properties > Default value or binding" section to (''). NOTE: It includes single quotation and parenthesis

How do I insert into a table and get back the primary key value?

I have a primary key set up to auto increment.
I am doing multiple queries and I need to retrieve that primary key value to use as a foreign key in another table (IsIdentity = TRUE).
Is there any elegant way to get back the primary key value when I do an insert query? Right now I am requerying and getting the highest value in that column which seems really hacky.
Any suggestions?
If you are using SQL Server 2005 or later, you can use the OUTPUT clause.
create table T(
pk int identity primary key,
dat varchar(20)
);
go
insert into T
output inserted.pk
values ('new item');
go
drop table T;
The output can be directed to a table as well as to the client. For example:
create table T(
pk int identity primary key,
dat varchar(20)
);
create table U(
i int identity(1001,1) primary key,
T_pk int not null,
d datetime
);
go
insert into T
output inserted.pk, getdate()
into U(T_pk,d)
values ('new item'), ('newer item');
go
select * from T;
select * from U;
go
drop table T, U;
Beginning with SQL Server 2008, you can use "composable DML" for more possibilities.
insert into YourTable values (...)
get the new PK with scope_identity()
select scope_identity()
INSERT INTO YourTable (1, 2, etc.)
OUTPUT inserted.yourIDcolumn
VALUES (value1, value2, value...)
Note: This is for MS SQL 2005 and greater
SCOPE_IDENTITY() is probably what you want. It returns the ID of the last record inserted by the same code context in which it executes.
IDENT_CURRENT('tablename') is subject to concurrency issues. That is, there's no guarantee that another record won't be inserted between the INSERT and the call to IDENT_CURRENT.
I must confess, I'm not sure to what source of amazement the VillageIdiot's outburst refers, but I myself am quite astonished that this question does not appear to be a duplicate at all.
holy crap!!!
just call SCOPE_IDENTITY() function:
insert into your_talble(col1,col2) values('blah','more blah')
select scope_identity()
because selecting highest value will return error if any other statement make an insert. the function scope_identity() returns the identity created in current context (that is by your statement)
You should use scope_identity(). And I recommend to wrap insert statement and scope_identity() into transaction.