Showing posts with label int. Show all posts
Showing posts with label int. Show all posts

Monday, March 26, 2012

How to create hash function for NTEXT

I have a table
create table t
(
val ntext(16),
id int not null identity,
hash nvarchar(32)
)
I'd like to avoid rows with double ntext values, thats why I want to compare
hast values only.
But how can I create hash for ntext ?
ThanksOleg,
You could try using checksum_agg after chunking the ntext column
into 8000 byte pieces. Here's an example using image (there is a long
image column in a Northwind table that makes it easier to write a repro).
checksum() and checksum_agg() are not good hash functions, but they
will help you identify rows that may be the same. You will still have to
check to see if they are the same, as you would with any hash function.
create table Integers (
chunk int primary key
)
insert into Integers select ProductID-1 from Northwind..Products
go
create table textVals (
pk int identity primary key,
t image,
hashVal int
)
insert into textVals select photo, null from Northwind..Employees
go
update textVals set
hashVal = (
select checksum_agg(ca) from (
select checksum(substring(tv.t,1+8000*chunk,8000)) as ca
from Integers, textVals as tv
where tv.pk = textVals.pk and chunk <= datalength(t)/8000
) T
)
select datalength(t) as t_len, hashVal from textVals
go
drop table Integers, textVals
-- Steve Kass
-- Drew University
Oleg Cherkasenko wrote:
>I have a table
>create table t
>(
>val ntext(16),
>id int not null identity,
>hash nvarchar(32)
>)
>I'd like to avoid rows with double ntext values, thats why I want to compare
>hast values only.
>But how can I create hash for ntext ?
>Thanks
>
>

Friday, March 9, 2012

How to create a constraint like this:

Hi,

Suppose the following table definition in Sql Server 2005,

create table CompanySymbol
CompanyId int,
SymbolId int,
IsPrimarySymbol bit

Primary Key (CompanyId, SymbolId)

How can I create a constraint which wil ensure that IsPrimarySymbol will be set to 1(true) only once per CompanyId while allowing it to be set to 0 an unlimited amount of time per CompanyId. i.e.:

CompanyId SymbolId IsPrimarySymbol
--
1 1 1 (IsPrimarySymbol to 1 for CompanyId 1)
1 2 0
1 3 0
2 1 1 (IsPrimarySymbol to 1 for CompanyId 2)
2 2 0
3 1 0
4 1 1 (IsPrimarySymbol to 1 for CompanyId 4)
4 2 0
4 3 0

Thanks

Your best bet is to use a trigger to enforce this rule. It can be coded to either reject any Insert/Update that leaves the table with two rows flagged as IsPrimarySymbol. Alternatively, it could be coded to zero out all but the latest record set to IsPrimarySymbol. Be sure to consider Inserts and Updates of more than one record at a time.

It can be accomplished with a Constraint, but you need to also add a computed column.

create table CompanySymbol
CompanyId int,
SymbolId int,
IsPrimarySymbol bit
Guard as Case when IsPrimarySymbol = 1 Then 0 else SymbolId End

Now you can create a Unique Constraint( CompanyId, Guard )

My vote is for the trigger.

|||

Thanks for the answer, this should do the trick.