How do i number the rows in the table? My data does not have any unique
values if that helpsThere is a RowNumber(<scope>) function, where <scope> is a string that
identifies a data region, dataset or grouping if you need that context. For
just a simple running row total, use RowNumber(Nothing). You can also use
it for visual effects, and the most frequent example of this is to create
"green bar" reports by setting the background colour of a table row with the
expression:
=iif(RowNumber(Nothing) Mod 2, "Green", "White")
Cheers, Mark
"Marvin" <Marvin@.discussions.microsoft.com> wrote in message
news:25E67CAB-76C0-450A-B4DE-87788DD19E80@.microsoft.com...
> How do i number the rows in the table? My data does not have any unique
> values if that helps
Showing posts with label unique. Show all posts
Showing posts with label unique. Show all posts
Wednesday, March 28, 2012
Friday, March 9, 2012
Nullable Unique Constraint
I have a table and want to put a unique constraint on a nullable column.
I don't think I can do this and I think I will have to check for uniqueness
on
an Insert / Update trigger (selecting from the inserted buffer where column
is
not null).
Is there a way to do this with a constraint instead of a trigger?WJ
Thank to Steve Kass
CREATE TABLE dupNulls (
pk int identity(1,1) primary key,
X int NULL,
nullbuster as (case when X is null then pk else 0 end),
CONSTRAINT dupNulls_uqX UNIQUE (X,nullbuster)
)
INSERT INTO dupNulls(X) VALUES (1)
INSERT INTO dupNulls(X) VALUES (NULL)
INSERT INTO dupNulls(X) VALUES (NULL)
GO
SELECT pk, X, nullbuster FROM dupNulls
UPDATE dupNulls SET X = 1 WHERE pk = 2
GO
SELECT pk, X, nullbuster FROM dupNulls
UPDATE dupNulls SET X = 2 WHERE pk = 2
SELECT pk, X, nullbuster FROM dupNulls
DROP TABLE dupNulls
"WJ" <wj@.nospam.com> wrote in message
news:%23FteefWbFHA.3196@.TK2MSFTNGP14.phx.gbl...
> I have a table and want to put a unique constraint on a nullable column.
> I don't think I can do this and I think I will have to check for
uniqueness on
> an Insert / Update trigger (selecting from the inserted buffer where
column is
> not null).
> Is there a way to do this with a constraint instead of a trigger?
>|||You can do it, but nulls are considered equal in this context, so only one
null value will be allowed which may not be what you want.
Alternative is create a view that includes the nullable column but excludes
the rows where this column is null. Then create an index on the view. Either
way is typically better than via trigger.
Mike John
"WJ" <wj@.nospam.com> wrote in message
news:%23FteefWbFHA.3196@.TK2MSFTNGP14.phx.gbl...
>I have a table and want to put a unique constraint on a nullable column.
> I don't think I can do this and I think I will have to check for
> uniqueness on
> an Insert / Update trigger (selecting from the inserted buffer where
> column is
> not null).
> Is there a way to do this with a constraint instead of a trigger?
>|||Another solution is to create view as
SELECT MyUniqueColumn FROM ... WHERE MyUniqueColumn IS NOT NULL
and uniquely index the MyUniqueColumn
HTH,
Gert-Jan
WJ wrote:
> I have a table and want to put a unique constraint on a nullable column.
> I don't think I can do this and I think I will have to check for uniquenes
s on
> an Insert / Update trigger (selecting from the inserted buffer where colum
n is
> not null).
> Is there a way to do this with a constraint instead of a trigger?
I don't think I can do this and I think I will have to check for uniqueness
on
an Insert / Update trigger (selecting from the inserted buffer where column
is
not null).
Is there a way to do this with a constraint instead of a trigger?WJ
Thank to Steve Kass
CREATE TABLE dupNulls (
pk int identity(1,1) primary key,
X int NULL,
nullbuster as (case when X is null then pk else 0 end),
CONSTRAINT dupNulls_uqX UNIQUE (X,nullbuster)
)
INSERT INTO dupNulls(X) VALUES (1)
INSERT INTO dupNulls(X) VALUES (NULL)
INSERT INTO dupNulls(X) VALUES (NULL)
GO
SELECT pk, X, nullbuster FROM dupNulls
UPDATE dupNulls SET X = 1 WHERE pk = 2
GO
SELECT pk, X, nullbuster FROM dupNulls
UPDATE dupNulls SET X = 2 WHERE pk = 2
SELECT pk, X, nullbuster FROM dupNulls
DROP TABLE dupNulls
"WJ" <wj@.nospam.com> wrote in message
news:%23FteefWbFHA.3196@.TK2MSFTNGP14.phx.gbl...
> I have a table and want to put a unique constraint on a nullable column.
> I don't think I can do this and I think I will have to check for
uniqueness on
> an Insert / Update trigger (selecting from the inserted buffer where
column is
> not null).
> Is there a way to do this with a constraint instead of a trigger?
>|||You can do it, but nulls are considered equal in this context, so only one
null value will be allowed which may not be what you want.
Alternative is create a view that includes the nullable column but excludes
the rows where this column is null. Then create an index on the view. Either
way is typically better than via trigger.
Mike John
"WJ" <wj@.nospam.com> wrote in message
news:%23FteefWbFHA.3196@.TK2MSFTNGP14.phx.gbl...
>I have a table and want to put a unique constraint on a nullable column.
> I don't think I can do this and I think I will have to check for
> uniqueness on
> an Insert / Update trigger (selecting from the inserted buffer where
> column is
> not null).
> Is there a way to do this with a constraint instead of a trigger?
>|||Another solution is to create view as
SELECT MyUniqueColumn FROM ... WHERE MyUniqueColumn IS NOT NULL
and uniquely index the MyUniqueColumn
HTH,
Gert-Jan
WJ wrote:
> I have a table and want to put a unique constraint on a nullable column.
> I don't think I can do this and I think I will have to check for uniquenes
s on
> an Insert / Update trigger (selecting from the inserted buffer where colum
n is
> not null).
> Is there a way to do this with a constraint instead of a trigger?
nullable and unique column
I created a table and one of its column can be null, or
unique value.
it turned out that I could only insert one null record for
this column,
I had 'duplicate key...' error when trying to insert the
second null for this column.
what is the way to make a column that is nullable for more
than one record, and
is unique if the data is not null?
thanks!One way to achieve that is to create a view that selects only the
non-NULL rows and create a unique clustered index on the relevant
columns.
HTH,
Gert-Jan
amy wrote:
> I created a table and one of its column can be null, or
> unique value.
> it turned out that I could only insert one null record for
> this column,
> I had 'duplicate key...' error when trying to insert the
> second null for this column.
> what is the way to make a column that is nullable for more
> than one record, and
> is unique if the data is not null?
> thanks!|||Hi Amy,
Thank you for using MSDN Newsgroup! My name is Billy and it's my pleasure to assist you
with your issue.
From your description, I understand that you would like to allow multiple nulls in a UNIQUE
column. Have I fully understood you? If there is anything I misunderstood, please feel free to
let me know.
Unfortunately, this feature is not allowed in relational databases that adhere to the SQL
standard. As we know, Null, by definition is an "unknown" value and is not equal to any other
value even the null itself.
However, UNIQUE constraint regards Null as other normal values. In this way, a unique
column only allow one Null. On the other hand, for the purposes of unique indexes, SQL Server
also treats all NULLs as equal so you cannot have more than one.
It's better to not allow Null in a unique column as SQL Server allows only one null (I understand
this may be a problem when you insert rows and left that column null/blank). If you need to
perform like that, I recommend you use some check mechanism such as triggers to check
rows inserted into the table first to avoid the duplicated nulls errors.
Amy, does this answer your question? If there is anything more I can do to assist you, please
feel free to post it in the group
Best regards,
Billy Yao
Microsoft Online Support
----
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Please reply to newsgroups only. Thanks.|||Thank you Billy Yao !
Yes, now I'm using a trigger to check the data during
insert.
-amy
>--Original Message--
>Hi Amy,
>Thank you for using MSDN Newsgroup! My name is Billy and
it's my pleasure to assist you
>with your issue.
>From your description, I understand that you would like
to allow multiple nulls in a UNIQUE
>column. Have I fully understood you? If there is anything
I misunderstood, please feel free to
>let me know.
>Unfortunately, this feature is not allowed in relational
databases that adhere to the SQL
>standard. As we know, Null, by definition is an "unknown"
value and is not equal to any other
>value even the null itself.
>However, UNIQUE constraint regards Null as other normal
values. In this way, a unique
>column only allow one Null. On the other hand, for the
purposes of unique indexes, SQL Server
>also treats all NULLs as equal so you cannot have more
than one.
>It's better to not allow Null in a unique column as SQL
Server allows only one null (I understand
>this may be a problem when you insert rows and left that
column null/blank). If you need to
>perform like that, I recommend you use some check
mechanism such as triggers to check
>rows inserted into the table first to avoid the
duplicated nulls errors.
>Amy, does this answer your question? If there is anything
more I can do to assist you, please
>feel free to post it in the group
>Best regards,
>Billy Yao
>Microsoft Online Support
>----
>Get Secure! - www.microsoft.com/security
>This posting is provided "as is" with no warranties and
confers no rights.
>Please reply to newsgroups only. Thanks.
>
>.
>
unique value.
it turned out that I could only insert one null record for
this column,
I had 'duplicate key...' error when trying to insert the
second null for this column.
what is the way to make a column that is nullable for more
than one record, and
is unique if the data is not null?
thanks!One way to achieve that is to create a view that selects only the
non-NULL rows and create a unique clustered index on the relevant
columns.
HTH,
Gert-Jan
amy wrote:
> I created a table and one of its column can be null, or
> unique value.
> it turned out that I could only insert one null record for
> this column,
> I had 'duplicate key...' error when trying to insert the
> second null for this column.
> what is the way to make a column that is nullable for more
> than one record, and
> is unique if the data is not null?
> thanks!|||Hi Amy,
Thank you for using MSDN Newsgroup! My name is Billy and it's my pleasure to assist you
with your issue.
From your description, I understand that you would like to allow multiple nulls in a UNIQUE
column. Have I fully understood you? If there is anything I misunderstood, please feel free to
let me know.
Unfortunately, this feature is not allowed in relational databases that adhere to the SQL
standard. As we know, Null, by definition is an "unknown" value and is not equal to any other
value even the null itself.
However, UNIQUE constraint regards Null as other normal values. In this way, a unique
column only allow one Null. On the other hand, for the purposes of unique indexes, SQL Server
also treats all NULLs as equal so you cannot have more than one.
It's better to not allow Null in a unique column as SQL Server allows only one null (I understand
this may be a problem when you insert rows and left that column null/blank). If you need to
perform like that, I recommend you use some check mechanism such as triggers to check
rows inserted into the table first to avoid the duplicated nulls errors.
Amy, does this answer your question? If there is anything more I can do to assist you, please
feel free to post it in the group
Best regards,
Billy Yao
Microsoft Online Support
----
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Please reply to newsgroups only. Thanks.|||Thank you Billy Yao !
Yes, now I'm using a trigger to check the data during
insert.
-amy
>--Original Message--
>Hi Amy,
>Thank you for using MSDN Newsgroup! My name is Billy and
it's my pleasure to assist you
>with your issue.
>From your description, I understand that you would like
to allow multiple nulls in a UNIQUE
>column. Have I fully understood you? If there is anything
I misunderstood, please feel free to
>let me know.
>Unfortunately, this feature is not allowed in relational
databases that adhere to the SQL
>standard. As we know, Null, by definition is an "unknown"
value and is not equal to any other
>value even the null itself.
>However, UNIQUE constraint regards Null as other normal
values. In this way, a unique
>column only allow one Null. On the other hand, for the
purposes of unique indexes, SQL Server
>also treats all NULLs as equal so you cannot have more
than one.
>It's better to not allow Null in a unique column as SQL
Server allows only one null (I understand
>this may be a problem when you insert rows and left that
column null/blank). If you need to
>perform like that, I recommend you use some check
mechanism such as triggers to check
>rows inserted into the table first to avoid the
duplicated nulls errors.
>Amy, does this answer your question? If there is anything
more I can do to assist you, please
>feel free to post it in the group
>Best regards,
>Billy Yao
>Microsoft Online Support
>----
>Get Secure! - www.microsoft.com/security
>This posting is provided "as is" with no warranties and
confers no rights.
>Please reply to newsgroups only. Thanks.
>
>.
>
Subscribe to:
Posts (Atom)