How I shoud use BIT in MS SQL 2005

Posted by adopilot on Stack Overflow See other posts from Stack Overflow or by adopilot
Published on 2010-04-27T14:24:09Z Indexed on 2010/04/27 14:33 UTC
Read the original article Hit count: 375

Regarding to SQL performance.

I have Scalar-Valued function for checking some specific condition in base, It returns BIT value True or False,

I now do not know how I should fill @BIT parameter

If I write.

set @bit = convert(bit,1)

or

set @bit = 1

or

set @bit='true'

Function will work anyway but I do not know which method is recommended for daily use.

Another Question, I have table in my base with around 4 million records, Daily insert is about 4K records in that table. Now I want to add CONSTRAINT on that table whit scalar valued function that I mentioned already

Something like this

ALTER TABLE fin_stavke
ADD CONSTRAINT fin_stavke_knjizenje CHECK ( dbo.fn_ado_chk_fin(id)=convert(bit,1))

Where is filed "id" primary key of table fin_stavke and dbo.fn_ado_chk_fin looks like

create FUNCTION fn_ado_chk_fin
(
    @stavka_id int
)
RETURNS bit
AS
BEGIN
declare @bit bit

if exists (select * from fin_stavke where id=@stavka_id and doc_id is null and protocol_id is null)
        begin
            set @bit=0


        end
    else
        begin
            set @bit=1
        end
return @bit;
END
GO

Will this type and method of cheeking constraint will affect badly performance on my table and SQL at all ? If there is also better way to add control on this table please let me know.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server