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

Sunday, March 25, 2012

check my SP [its not working fine]

Hello everyone, im using SQL Server 2000, here is my table:

Table1: Buyers


BuyerID int
ParentID int
MerchantCode varchar(20) (each buyer has unique merchant code we say that its login is Merchant Code)
PinCode (this is used as a password for the buyers login)
ApprovalStatusCode int (FK, data is for authenticationStick out tongueending Approval, Approved, Cancelled)
IsCliEnabled smallint
Clis varchar(100) (this is the CSV: 12345, 2346,....)[as each buyer has more that 1 CLi values so this field is in CSV form data)

//Now buyers also has SubAccounts (Table2) that was made for his workers or some one else (Not necessary every buyer has SubAccounts)

Table2: Buyers SubAccounts


BuyerID
AccountNumber smallint
PinCode varchar(64)
CreateDate datetime

now here is my SP,
CREATE PROCEDURE IvrAuthenticateBuyer

@.MerchantCode varchar(20),
@.PinCode varchar(64),
@.CLI varchar(15)

AS
-- For testing i give values at here
declare @.MerchantCode varchar(20)
set @.merchantCode='000000010'
declare @.PinCode varchar(64)
set @.PinCode='1234656'
declare @.CLI varchar(15)
set @.CLI='12345'
-

declare @.BuyerID int
declare @.ApprovalStatusCode smallint
declare @.IsCliEnabled smallint
declare @.Clis varchar(1000)


-- buyerID get by checking only 8 digits of merchant code (omit last one)
-- if last digit of merchant code are > "0" then
-- get them in a variable
-- check if the account is approved
-- check if CLI is enabled, if yes, check if @.cli is in the list


SELECT @.BuyerID = BuyerID, @.ApprovalStatusCode = ApprovalStatusCode,
@.IsCliEnabled = IsCLIEnabled, @.Clis = coalesce(@.Clis+',','')+CLIs
FROM Buyers
WHERE MerchantCode = @.MerchantCode

--select @.BuyerID
--select @.IsCliEnabled
--select @.ApprovalStatusCode
--select @.Clis
-- chk all conditions

if @.ApprovalStatusCode <> 2
raiserror('Account is not Approved',16,1)

select @.BuyerID

return

if (@.IsCliEnabled=1) --check whether true
begin --Main Begin
--charindex will return value greater than 0 if CLI is found in list
-- if charindex('34534',@.Clis)>0
if ','+@.Clis+',' like '%,'+@.CLI +',%'
begin
print 'CLI found in CSV list'
end
else
begin
raiserror('CLI NOT found in CSV list',16,1)
end
end --Main End
else
begin
raiserror('CLI NOT ENABLED',16,1)
end

-- Get Last Digit of Merchant Code and stored them in a variable

declare @.SubAccountNo varchar(2)
select @.SubAccountNo = Substring(@.MerchantCode,8,1)

-- Check It is SubAccount or the Buyer's Main Account

if convert(int, @.SubAccountNo) > 0
select 1
from BuyerSubAccounts
where PinCode=@.PinCode
else
select 1
from Buyers
where PinCode=@.PinCode


that is used for Buyer Authenticate, but i dont know whats wrong with this as its not working fine now,
as if i give wrong password then it also retuns the error of if i give wrong CLi same its runs without giving any error. Kindly
check it whats wrong with this as i m in much trouble still can't find out the actual problem in my SP. So i requested to all of u
plz help me and make my SP correct.

Thanx in Advance.

I believe I have found your problem. See code snippet below.

Code Snippet

CREATE PROCEDURE IvrAuthenticateBuyer

@.MerchantCode varchar(20),
@.PinCode varchar(64),
@.CLI varchar(15)

AS
-- For testing i give values at here
declare @.MerchantCode varchar(20)
set @.merchantCode='000000010'
declare @.PinCode varchar(64)
set @.PinCode='1234656'
declare @.CLI varchar(15)
set @.CLI='12345'
-

declare @.BuyerID int
declare @.ApprovalStatusCode smallint
declare @.IsCliEnabled smallint
declare @.Clis varchar(1000)


-- buyerID get by checking only 8 digits of merchant code (omit last one)
-- if last digit of merchant code are > "0" then
-- get them in a variable
-- check if the account is approved
-- check if CLI is enabled, if yes, check if @.cli is in the list


SELECT @.BuyerID = BuyerID, @.ApprovalStatusCode = ApprovalStatusCode,
@.IsCliEnabled = IsCLIEnabled, @.Clis = coalesce(@.Clis+',','')+CLIs
FROM Buyers
WHERE MerchantCode = @.MerchantCode

--select @.BuyerID
--select @.IsCliEnabled
--select @.ApprovalStatusCode
--select @.Clis
-- chk all conditions

if @.ApprovalStatusCode <> 2

BEGIN
raiserror('Account is not Approved',16,1)

select @.BuyerID

return

END

if (@.IsCliEnabled=1) --check whether true
begin --Main Begin
--charindex will return value greater than 0 if CLI is found in list
-- if charindex('34534',@.Clis)>0
if ','+@.Clis+',' like '%,'+@.CLI +',%'
begin
print 'CLI found in CSV list'
end
else
begin
raiserror('CLI NOT found in CSV list',16,1)
end
end --Main End
else
begin
raiserror('CLI NOT ENABLED',16,1)
end

-- Get Last Digit of Merchant Code and stored them in a variable

declare @.SubAccountNo varchar(2)
select @.SubAccountNo = Substring(@.MerchantCode,8,1)

-- Check It is SubAccount or the Buyer's Main Account

if convert(int, @.SubAccountNo) > 0
select 1
from BuyerSubAccounts
where PinCode=@.PinCode
else
select 1
from Buyers
where PinCode=@.PinCode

An IF statement will only apply to the statement immediately following it, unless that statement is BEGIN, then it will execute until the next END is found.

Thursday, March 22, 2012

Check if string could be converted to number

I need to check if string could be converted to a int without throwing any
errors.
I need to do something like this
DECLARE @.s varchar(20)
DECLARE @.i int
--if following is possible
@.i=CAST (@.s as int)
--then
SELECT @.1
--else
SELECT 0
if string is not a number I really don't need to deal with it in the first
place.
The real life example of my scenario is checking uniqueness of check number
for bank transactions. If user writes ATM for check number we don't need to
check the uniqueness.
Could it be done?
Thanks,
Shimon.There's a built-in function in SQL - ISNUMERIC. Look it up in Books Online.
However, it has some issues. They are illustrated here:
http://www.aspfaq.com/show.asp?id=2390
..and more! ;)
ML|||Hi Shimon
you can use ISNUMERIC for this
select isnumeric(@.s)
For eg:
if Value of @.s is '123' then the value returned is 1
if Value of @.s is '123a' then the value returned is 0
please let me know if u have any questions
best Regards,
Chandra
http://chanduas.blogspot.com/
http://www.SQLResource.com/
---
"Shimon Sim" wrote:

> I need to check if string could be converted to a int without throwing any
> errors.
> I need to do something like this
> DECLARE @.s varchar(20)
> DECLARE @.i int
> --if following is possible
> @.i=CAST (@.s as int)
> --then
> SELECT @.1
> --else
> SELECT 0
> if string is not a number I really don't need to deal with it in the first
> place.
> The real life example of my scenario is checking uniqueness of check numbe
r
> for bank transactions. If user writes ATM for check number we don't need t
o
> check the uniqueness.
> Could it be done?
> Thanks,
> Shimon.
>
>|||Shimon wrote on Wed, 17 Aug 2005 08:25:30 -0400:

> I need to check if string could be converted to a int without throwing any
> errors.
> I need to do something like this
> DECLARE @.s varchar(20)
> DECLARE @.i int
> --if following is possible
> @.i=CAST (@.s as int)
> --then
> SELECT @.1
> --else
> SELECT 0
> if string is not a number I really don't need to deal with it in the first
> place.
> The real life example of my scenario is checking uniqueness of check
> number for bank transactions. If user writes ATM for check number we don't
> need to check the uniqueness.
> Could it be done?
> Thanks,
> Shimon.
Try
DECLARE @.s varchar(20)
DECLARE @.i int
/*set value of @.s here*/
SET @.s = 'test'
IF (ISNUMERIC(@.s) = 1)
SET @.i = CAST(@.s as int)
ELSE
SET @.i = 0
SELECT @.i
You'll get a response of 0. Change 'test' to '1000', you'll get 1000.
Dan|||Oh, one thing I missed in my reply - if the string is numeric, but too large
to fit into an int, you'll get an error, so you should have some check on
the string length to determine if it'll fit, or cast into the largest
numeric datatype.
Dan|||Thanks a lot. Exactly what I needed.
Shimon.
"Chandra" <chandra@.discussions.microsoft.com> wrote in message
news:880E02E2-DA50-4466-9566-FC429802FB37@.microsoft.com...
> Hi Shimon
> you can use ISNUMERIC for this
> select isnumeric(@.s)
> For eg:
> if Value of @.s is '123' then the value returned is 1
> if Value of @.s is '123a' then the value returned is 0
> please let me know if u have any questions
>
> --
> best Regards,
> Chandra
> http://chanduas.blogspot.com/
> http://www.SQLResource.com/
> ---
>
> "Shimon Sim" wrote:
>|||Shimon Sim,
Do you think it is enough using "like" operator?
Example:
select
cast(c1 as int)
from
(
select cast('1080' as varchar(10))
union all
select cast('atm' as varchar(10))
union all
select cast('1081' as varchar(10))
union all
select cast('atm' as varchar(10))
union all
select cast('atm' as varchar(10))
union all
select cast('1082' as varchar(10))
) as t1(c1)
where
c1 not like '%[^0-9]%'
AMB
"Shimon Sim" wrote:

> I need to check if string could be converted to a int without throwing any
> errors.
> I need to do something like this
> DECLARE @.s varchar(20)
> DECLARE @.i int
> --if following is possible
> @.i=CAST (@.s as int)
> --then
> SELECT @.1
> --else
> SELECT 0
> if string is not a number I really don't need to deal with it in the first
> place.
> The real life example of my scenario is checking uniqueness of check numbe
r
> for bank transactions. If user writes ATM for check number we don't need t
o
> check the uniqueness.
> Could it be done?
> Thanks,
> Shimon.
>
>|||Thank you for this note.
Shimon.
"Daniel Crichton" <msnews@.worldofspack.co.uk> wrote in message
news:OnUdojyoFHA.3256@.TK2MSFTNGP12.phx.gbl...
> Oh, one thing I missed in my reply - if the string is numeric, but too
> large to fit into an int, you'll get an error, so you should have some
> check on the string length to determine if it'll fit, or cast into the
> largest numeric datatype.
> Dan
>|||I am not sure if it will work in my scenario.
Thank you
Shimon.
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in message
news:D57DFB0A-33B2-4239-AA77-BA15195B6789@.microsoft.com...
> Shimon Sim,
> Do you think it is enough using "like" operator?
> Example:
> select
> cast(c1 as int)
> from
> (
> select cast('1080' as varchar(10))
> union all
> select cast('atm' as varchar(10))
> union all
> select cast('1081' as varchar(10))
> union all
> select cast('atm' as varchar(10))
> union all
> select cast('atm' as varchar(10))
> union all
> select cast('1082' as varchar(10))
> ) as t1(c1)
> where
> c1 not like '%[^0-9]%'
>
> AMB
> "Shimon Sim" wrote:
>

Tuesday, March 20, 2012

Check if column (int) is true or false (bool)

Is there some way I may "convert" an int column to boolean with the
SQL query? I have a database with an int column containing only 0s and
1s - using it as a Boolean. Now, I need the Boolean values false and
true in order to use the .net checkbox.
I have tried the following:
SELECT userID, (chkName = 1) AS bChkName, name
FROM User
When chkName = 1 it will return true, and vice versa when it equals 0.
This should produce something like this:
userID bChkName name
2 true Peter
3 true Linda
4 false John Doe
according to the int in chkName.
This of course didn't work and now I am hoping for your help... do you
know of any solution?
royend.
This really does not solve my problem, as it returns a string and not
a Boolean value.
The asp:checkbox requires a boolean in order to become checked or
unchecked.
In worst case scenario I'll have to code inside some of Visual's
predefined and automatically added code...
Still, I am a bit surprised that SQL cannot return a bool value for
one of its column based on a simple test.
Thanks for your answer though.
royend
On 25 Jun, 13:02, "vt" <vinu.t.1...@.gmail.com> wrote:
> royend
> there is no direct way to do this
> select userID, case when chkName =1 then 'true' else 'false',name from user
> Regards
> VT
> Knowledge is power, share it...http://oneplace4sql.blogspot.com/"royend" <roy...@.gmail.com> wrote in message
> news:1182768635.843541.88960@.c77g2000hse.googlegro ups.com...
>
>
>
>
> - Vis sitert tekst -
|||> This really does not solve my problem, as it returns a string and not
> a Boolean value.
And ASP.Net can't turn 'true' or 'false' into Boolean values? Isn't that an
ASP.Net problem, not a SQL Server problem?
|||On Mon, 25 Jun 2007 04:44:18 -0700, royend wrote:

>This really does not solve my problem, as it returns a string and not
>a Boolean value.
Hi royend,
Unfortunately, SQL Server doesn't support boolean values. Quite logical,
if you consider that all predicate logic in an RDBMS is three-valued,
whereas boolean algebra is two-valued. Put a PITA in some situations
nonetheless.
The closest SQL Server has to offer would be bit. Something like
CASE WHEN chkName = 1 THEN CAST(1 as bit) ELSE CAST(0 AS bit) END
But then you'd have to rely on the conversion from bit to boolean by ASP
treating 1 as true and 0 as false - frankly, I'd rather use code that
returns a string 'True' or 'False' (maybe shortened to 'T' and 'F') and
convert it with explicit ASP code then to rely on the implicit
conversion from SQL bit to ASP bool (that is, as far as I know,
undocumented) doing what I hope it does, and still doing it next year.
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Check if column (int) is true or false (bool)

Is there some way I may "convert" an int column to boolean with the
SQL query? I have a database with an int column containing only 0s and
1s - using it as a Boolean. Now, I need the Boolean values false and
true in order to use the .net checkbox.
I have tried the following:
SELECT userID, (chkName = 1) AS bChkName, name
FROM User
When chkName = 1 it will return true, and vice versa when it equals 0.
This should produce something like this:
userID bChkName name
2 true Peter
3 true Linda
4 false John Doe
according to the int in chkName.
This of course didn't work and now I am hoping for your help... do you
know of any solution?
royend.royend
there is no direct way to do this
select userID, case when chkName =1 then 'true' else 'false',name from user
Regards
VT
Knowledge is power, share it...
http://oneplace4sql.blogspot.com/
"royend" <royend@.gmail.com> wrote in message
news:1182768635.843541.88960@.c77g2000hse.googlegroups.com...
> Is there some way I may "convert" an int column to boolean with the
> SQL query? I have a database with an int column containing only 0s and
> 1s - using it as a Boolean. Now, I need the Boolean values false and
> true in order to use the .net checkbox.
> I have tried the following:
> SELECT userID, (chkName = 1) AS bChkName, name
> FROM User
> When chkName = 1 it will return true, and vice versa when it equals 0.
> This should produce something like this:
> userID bChkName name
> 2 true Peter
> 3 true Linda
> 4 false John Doe
> according to the int in chkName.
> This of course didn't work and now I am hoping for your help... do you
> know of any solution?
> royend.
>|||This really does not solve my problem, as it returns a string and not
a Boolean value.
The asp:checkbox requires a boolean in order to become checked or
unchecked.
In worst case scenario I'll have to code inside some of Visual's
predefined and automatically added code...
Still, I am a bit surprised that SQL cannot return a bool value for
one of its column based on a simple test.
Thanks for your answer though.
royend
On 25 Jun, 13:02, "vt" <vinu.t.1...@.gmail.com> wrote:
> royend
> there is no direct way to do this
> select userID, case when chkName =1 then 'true' else 'false',name from user
> Regards
> VT
> Knowledge is power, share it...http://oneplace4sql.blogspot.com/"royend" <roy...@.gmail.com> wrote in message
> news:1182768635.843541.88960@.c77g2000hse.googlegroups.com...
>
> > Is there some way I may "convert" an int column to boolean with the
> > SQL query? I have a database with an int column containing only 0s and
> > 1s - using it as a Boolean. Now, I need the Boolean values false and
> > true in order to use the .net checkbox.
> > I have tried the following:
> > SELECT userID, (chkName = 1) AS bChkName, name
> > FROM User
> > When chkName = 1 it will return true, and vice versa when it equals 0.
> > This should produce something like this:
> > userID bChkName name
> > 2 true Peter
> > 3 true Linda
> > 4 false John Doe
> > according to the int in chkName.
> > This of course didn't work and now I am hoping for your help... do you
> > know of any solution?
> > royend.- Skjul sitert tekst -
> - Vis sitert tekst -|||Try this...
select userID, convert(bit,chkName) as BoolName,name from user|||> This really does not solve my problem, as it returns a string and not
> a Boolean value.
And ASP.Net can't turn 'true' or 'false' into Boolean values? Isn't that an
ASP.Net problem, not a SQL Server problem?|||On Mon, 25 Jun 2007 04:44:18 -0700, royend wrote:
>This really does not solve my problem, as it returns a string and not
>a Boolean value.
Hi royend,
Unfortunately, SQL Server doesn't support boolean values. Quite logical,
if you consider that all predicate logic in an RDBMS is three-valued,
whereas boolean algebra is two-valued. Put a PITA in some situations
nonetheless.
The closest SQL Server has to offer would be bit. Something like
CASE WHEN chkName = 1 THEN CAST(1 as bit) ELSE CAST(0 AS bit) END
But then you'd have to rely on the conversion from bit to boolean by ASP
treating 1 as true and 0 as false - frankly, I'd rather use code that
returns a string 'True' or 'False' (maybe shortened to 'T' and 'F') and
convert it with explicit ASP code then to rely on the implicit
conversion from SQL bit to ASP bool (that is, as far as I know,
undocumented) doing what I hope it does, and still doing it next year.
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelissql

Check if column (int) is true or false (bool)

Is there some way I may "convert" an int column to boolean with the
SQL query? I have a database with an int column containing only 0s and
1s - using it as a Boolean. Now, I need the Boolean values false and
true in order to use the .net checkbox.
I have tried the following:
SELECT userID, (chkName = 1) AS bChkName, name
FROM User
When chkName = 1 it will return true, and vice versa when it equals 0.
This should produce something like this:
userID bChkName name
2 true Peter
3 true Linda
4 false John Doe
according to the int in chkName.
This of course didn't work and now I am hoping for your help... do you
know of any solution?
royend.royend
there is no direct way to do this
select userID, case when chkName =1 then 'true' else 'false',name from user
Regards
VT
Knowledge is power, share it...
http://oneplace4sql.blogspot.com/
"royend" <royend@.gmail.com> wrote in message
news:1182768635.843541.88960@.c77g2000hse.googlegroups.com...
> Is there some way I may "convert" an int column to boolean with the
> SQL query? I have a database with an int column containing only 0s and
> 1s - using it as a Boolean. Now, I need the Boolean values false and
> true in order to use the .net checkbox.
> I have tried the following:
> SELECT userID, (chkName = 1) AS bChkName, name
> FROM User
> When chkName = 1 it will return true, and vice versa when it equals 0.
> This should produce something like this:
> userID bChkName name
> 2 true Peter
> 3 true Linda
> 4 false John Doe
> according to the int in chkName.
> This of course didn't work and now I am hoping for your help... do you
> know of any solution?
> royend.
>|||This really does not solve my problem, as it returns a string and not
a Boolean value.
The asp:checkbox requires a boolean in order to become checked or
unchecked.
In worst case scenario I'll have to code inside some of Visual's
predefined and automatically added code...
Still, I am a bit surprised that SQL cannot return a bool value for
one of its column based on a simple test.
Thanks for your answer though.
royend
On 25 Jun, 13:02, "vt" <vinu.t.1...@.gmail.com> wrote:
> royend
> there is no direct way to do this
> select userID, case when chkName =1 then 'true' else 'false',name from us
er
> Regards
> VT
> Knowledge is power, share it...http://oneplace4sql.blogspot.com/"royend"
<roy...@.gmail.com> wrote in message
> news:1182768635.843541.88960@.c77g2000hse.googlegroups.com...
>
>
>
>
>
>
> - Vis sitert tekst -|||Try this...
select userID, convert(bit,chkName) as BoolName,name from user|||> This really does not solve my problem, as it returns a string and not
> a Boolean value.
And ASP.Net can't turn 'true' or 'false' into Boolean values? Isn't that an
ASP.Net problem, not a SQL Server problem?

Check for username

Hello,
I have the following Table:
CREATE TABLE [dbo].[tb_User] (
[usr_id] [int] IDENTITY (1, 1) NOT NULL ,
[usg_groupID] [int] NOT NULL ,
[usr_createdBy] [int] NOT NULL ,
[usr_isActive] [int] NOT NULL ,
[usr_dateCreated] [datetime] NOT NULL ,
[usr_userName] [varchar] (25) NULL ,
[usr_password] [varchar] (50) NULL ,
[usr_firstName] [varchar] (50) NULL ,
[usr_lastName] [varchar] (50) NULL ,
[usr_eMail] [varchar] (50) NULL ,
[usr_phoneNumber] [varchar] (50) NULL ,
[usr_notes] [varchar] (512) NULL
)
I want to set up a stored procedure that will take two parameters
(@.UserName varchar(50) and @.Password varchar(50)) that I want to use to
check for the following
1. Check to see that the username exisits and that the column isActive = 1
2. If step 1 is true, then check that the @.Password Parameter is the
same for the user
3. If step 2 above is true return 1
4. If step 1 about is false (either the username does not exist, of if
it does it's not active (isActive = 0) return 0
I am going to use the Return value in my C# application to warn the user
that either the username does not exist, or it's not active or the
password is wrong.
Anything anyone can provide me with will be greatly appreciated!It would be simple as this:
e.g.
create proc usp
@.user varchar(25),
@.pass varchar(50)
as
set nocount on
if exists(select 1 from tb_User where usr_userName=@.user and usr_isActive=1
and usr_password=@.pass)
return 0
else
return 1
go
-oj
"Ed_P." <ed_p@.no-email.com> wrote in message
news:eX3GmN0CFHA.2180@.TK2MSFTNGP12.phx.gbl...
> Hello,
> I have the following Table:
> CREATE TABLE [dbo].[tb_User] (
> [usr_id] [int] IDENTITY (1, 1) NOT NULL ,
> [usg_groupID] [int] NOT NULL ,
> [usr_createdBy] [int] NOT NULL ,
> [usr_isActive] [int] NOT NULL ,
> [usr_dateCreated] [datetime] NOT NULL ,
> [usr_userName] [varchar] (25) NULL ,
> [usr_password] [varchar] (50) NULL ,
> [usr_firstName] [varchar] (50) NULL ,
> [usr_lastName] [varchar] (50) NULL ,
> [usr_eMail] [varchar] (50) NULL ,
> [usr_phoneNumber] [varchar] (50) NULL ,
> [usr_notes] [varchar] (512) NULL
> )
> I want to set up a stored procedure that will take two parameters
> (@.UserName varchar(50) and @.Password varchar(50)) that I want to use to
> check for the following
> 1. Check to see that the username exisits and that the column isActive = 1
> 2. If step 1 is true, then check that the @.Password Parameter is the same
> for the user
> 3. If step 2 above is true return 1
> 4. If step 1 about is false (either the username does not exist, of if it
> does it's not active (isActive = 0) return 0
> I am going to use the Return value in my C# application to warn the user
> that either the username does not exist, or it's not active or the
> password is wrong.
> Anything anyone can provide me with will be greatly appreciated!

Sunday, March 11, 2012

Check Constraint fails!

Hi Everybody,
Can anybody help me on the following query...
I have a table structure as follows
CREATE TABLE [dbo].[event_logs] (
[WSE_Idx] [int] NULL ,
[WSE_Type] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WSE_Date_Generated] [datetime] NULL ,
[WSE_lDate_Generated] [datetime] NULL ,
[WSE_Date_Written] [datetime] NULL ,
[WSE_lDate_Written] [datetime] NULL ,
[WSE_tzname] [varchar] (254) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WSE_Source] [varchar] (254) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WSE_Category] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WSE_Event] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WSE_User] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WSE_User_Type] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WSE_Computer] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WSE_Message] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WSE_Agent] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WSE_Log_Type] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)
go
It contains data. I tried to create the following 'check constraint' to the above table
alter table event_logs
add constraint ck_event_logs
check((WSE_Category = 'application' and wse_log_type in ('Audit Success','error')) OR
(WSE_Category = 'system' and wse_log_type in ('Warning')) OR
(WSE_Category = 'security' and wse_log_type in ('Audit Failure')))
It is giving the following error...
Server: Msg 547, Level 16, State 1, Line 1
ALTER TABLE statement conflicted with TABLE CHECK constraint 'ck_event_logs'.
The conflict occurred in database 'slm', table 'event_logs'.
Even I modified the above alter table script as follows, still it is giving the same error.
alter table event_logs
add constraint ck_event_logs
check(WSE_Category like '%applica%')
I created the similar table structure with different table name and applied the check constraint,
it works. No error. Ofcourse table doesn't have data (Empty table).
I have created RULE on this 'event_logs' table (with data). It works fine. No Error.
Can anybody tell me why this 'Check Constraint' is giving problem?.
tks in advance,
vasumData in a table are not valid for 'check constraint' that you specified.
You mast correct data in your table or in ALTER TABLE statement put WITH
NOCHECK option.
Look ALTER TABLE in BOL.
"vasum" <anonymous@.discussions.microsoft.com> wrote in message
news:748BD3CB-E545-4DEA-B05B-103EEF45BFAE@.microsoft.com...
> Hi Everybody,
> Can anybody help me on the following query...
> I have a table structure as follows
> CREATE TABLE [dbo].[event_logs] (
> [WSE_Idx] [int] NULL ,
> [WSE_Type] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [WSE_Date_Generated] [datetime] NULL ,
> [WSE_lDate_Generated] [datetime] NULL ,
> [WSE_Date_Written] [datetime] NULL ,
> [WSE_lDate_Written] [datetime] NULL ,
> [WSE_tzname] [varchar] (254) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [WSE_Source] [varchar] (254) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [WSE_Category] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [WSE_Event] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [WSE_User] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [WSE_User_Type] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [WSE_Computer] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [WSE_Message] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [WSE_Agent] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [WSE_Log_Type] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> )
> go
> It contains data. I tried to create the following 'check constraint' to
the above table
> alter table event_logs
> add constraint ck_event_logs
> check((WSE_Category = 'application' and wse_log_type in ('Audit
Success','error')) OR
> (WSE_Category = 'system' and wse_log_type in ('Warning')) OR
> (WSE_Category = 'security' and wse_log_type in ('Audit Failure')))
> It is giving the following error...
> Server: Msg 547, Level 16, State 1, Line 1
> ALTER TABLE statement conflicted with TABLE CHECK constraint
'ck_event_logs'.
> The conflict occurred in database 'slm', table 'event_logs'.
> Even I modified the above alter table script as follows, still it is
giving the same error.
> alter table event_logs
> add constraint ck_event_logs
> check(WSE_Category like '%applica%')
> I created the similar table structure with different table name and
applied the check constraint,
> it works. No error. Ofcourse table doesn't have data (Empty table).
> I have created RULE on this 'event_logs' table (with data). It works fine.
No Error.
> Can anybody tell me why this 'Check Constraint' is giving problem?.
> tks in advance,
> vasum
>|||thanks for the timely help. I works. I used 'with nocheck' option. Able to create new check constraint and this new check constraint is validating the any new rows coming into the table

Thursday, March 8, 2012

CHECK constraint

I've asked this before, without answer (though msnews.microsoft.com seems to
not show everything).
Imagine:
CREATE colors (
colorid INT PRIMARY KEY,
description VARCHAR(20)
)
CREATE TABLE tables (
tableid INT PRIMARY KEY,
colorid INT FOREIGN KEY colors(colorid)
)
CREATE TABLE table_chairs (
chairid INT PRIMARY KEY,
colorid INT FOREIGN KEY colors(colorid),
goeswithtable INT FOREIGN KEY tables(tableid),
CHECK ( ...... )
)
Please don't question the layout of my database tables. This is just an
example to illustrate my question.
So in table_chairs, you have chairs that belong to (makes a nice set with) a
certain table.
I wish to add a check constraint to ensure that the colorid of the chairid
is equal to the colorid of the table.
I can not use TRIGGERS.. can this be done with CHECK constraints or are they
themselves constrained in that they can not reference other tables?
Lisathe easiest way is just not to have colorid in table_chairs at all -
you can retrieve it from tables.
Yet if you really need to have the redundant column (BTW why?), define
a unique constraint on tables(tableid , colorid ) and have a FK point
to it from table_chairs|||It's nto redundant..
The colorid in table_chairs describes the colorid of chairid. The colorid in
tables is the colorid of tables.
The idea is that blue chairs may only be linked to a blue table.
But I need to find a way to CHECK that the colorid of the table (in
table_chairs) matches the colorid of the table (tables), without using
triggers.. is it possible?
"Alexander Kuznetsov" <AK_TIREDOFSPAM@.hotmail.COM> wrote in message
news:1141851166.856693.323390@.v46g2000cwv.googlegroups.com...
> the easiest way is just not to have colorid in table_chairs at all -
> you can retrieve it from tables.
> Yet if you really need to have the redundant column (BTW why?), define
> a unique constraint on tables(tableid , colorid ) and have a FK point
> to it from table_chairs
>|||> is it possible?
*untested"
alter table tables add unique(tableid, colorid)
alter table table_chairs add foreign key(goeswithtable, colorid)
references tables(tableid, colorid)

Sunday, February 12, 2012

char vs int

Hi
Yesterday somebody told me that use char field like a primary key is more
faster than integer field when you run a query.
Is that True, any suggestions.
Thanks.
Pablo Salazar.
> Yesterday somebody told me that use char field like a primary key is more
> faster than integer field when you run a query.
It really depends. Size of CHAR key? Volume of data? I can certainly
create an example that shows a small table with a two-character primary key
and 99 rows that will respond to queries faster than a large table with an
integer primary key and 99,999 rows.
With everything being the same, I really doubt it will be faster.

> Is that True, any suggestions.
Test it! Since you know the size of data you'll be dealing with and the
type you'll need to create, you already have all the tools at your disposal
to answer your own question... and are much better equipped than us at
determining which is faster.
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
|||Although the OP should test it in his or her own envrionment, I have too
much time on my hands so I just did a very simplistic test of SELECT
COUNT(*) from an INNER JOIN of two tables with integer columns and two
tables with char(7) columns, each with 6436343 rows.
Integer returned results twice as fast as char(7) in this test. Note that
the char(7) could have been reduced to char(5) if I'd made up some scheme to
convert numbers to AAAAA for 1, AAAAB for 2, etc, but that's more work than
even I, with too much time on my hands, want to do for the sake of this
test...
So I would encourage the OP to place the burden of proof on whoever made the
suggestion...
"Aaron Bertrand [MVP]" <aaron@.TRASHaspfaq.com> wrote in message
news:O0b5Y7XIEHA.2300@.tk2msftngp13.phx.gbl...[color=darkblue]
more
> It really depends. Size of CHAR key? Volume of data? I can certainly
> create an example that shows a small table with a two-character primary
key
> and 99 rows that will respond to queries faster than a large table with an
> integer primary key and 99,999 rows.
> With everything being the same, I really doubt it will be faster.
>
> Test it! Since you know the size of data you'll be dealing with and the
> type you'll need to create, you already have all the tools at your
disposal
> to answer your own question... and are much better equipped than us at
> determining which is faster.
> --
> Aaron Bertrand
> SQL Server MVP
> http://www.aspfaq.com/
>

char vs int

Hi
Yesterday somebody told me that use char field like a primary key is more
faster than integer field when you run a query.
Is that True, any suggestions.
Thanks.
Pablo Salazar.> Yesterday somebody told me that use char field like a primary key is more
> faster than integer field when you run a query.
It really depends. Size of CHAR key? Volume of data? I can certainly
create an example that shows a small table with a two-character primary key
and 99 rows that will respond to queries faster than a large table with an
integer primary key and 99,999 rows.
With everything being the same, I really doubt it will be faster.

> Is that True, any suggestions.
Test it! Since you know the size of data you'll be dealing with and the
type you'll need to create, you already have all the tools at your disposal
to answer your own question... and are much better equipped than us at
determining which is faster.
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/|||Although the OP should test it in his or her own envrionment, I have too
much time on my hands so I just did a very simplistic test of SELECT
COUNT(*) from an INNER JOIN of two tables with integer columns and two
tables with char(7) columns, each with 6436343 rows.
Integer returned results twice as fast as char(7) in this test. Note that
the char(7) could have been reduced to char(5) if I'd made up some scheme to
convert numbers to AAAAA for 1, AAAAB for 2, etc, but that's more work than
even I, with too much time on my hands, want to do for the sake of this
test...
So I would encourage the OP to place the burden of proof on whoever made the
suggestion...
"Aaron Bertrand [MVP]" <aaron@.TRASHaspfaq.com> wrote in message
news:O0b5Y7XIEHA.2300@.tk2msftngp13.phx.gbl...
more
> It really depends. Size of CHAR key? Volume of data? I can certainly
> create an example that shows a small table with a two-character primary
key
> and 99 rows that will respond to queries faster than a large table with an
> integer primary key and 99,999 rows.
> With everything being the same, I really doubt it will be faster.
>
> Test it! Since you know the size of data you'll be dealing with and the
> type you'll need to create, you already have all the tools at your
disposal
> to answer your own question... and are much better equipped than us at
> determining which is faster.
> --
> Aaron Bertrand
> SQL Server MVP
> http://www.aspfaq.com/
>

char vs int

Hi
Yesterday somebody told me that use char field like a primary key is more
faster than integer field when you run a query.
Is that True, any suggestions.
Thanks.
Pablo Salazar.> Yesterday somebody told me that use char field like a primary key is more
> faster than integer field when you run a query.
It really depends. Size of CHAR key? Volume of data? I can certainly
create an example that shows a small table with a two-character primary key
and 99 rows that will respond to queries faster than a large table with an
integer primary key and 99,999 rows.
With everything being the same, I really doubt it will be faster.
> Is that True, any suggestions.
Test it! Since you know the size of data you'll be dealing with and the
type you'll need to create, you already have all the tools at your disposal
to answer your own question... and are much better equipped than us at
determining which is faster.
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/|||Although the OP should test it in his or her own envrionment, I have too
much time on my hands so I just did a very simplistic test of SELECT
COUNT(*) from an INNER JOIN of two tables with integer columns and two
tables with char(7) columns, each with 6436343 rows.
Integer returned results twice as fast as char(7) in this test. Note that
the char(7) could have been reduced to char(5) if I'd made up some scheme to
convert numbers to AAAAA for 1, AAAAB for 2, etc, but that's more work than
even I, with too much time on my hands, want to do for the sake of this
test...
So I would encourage the OP to place the burden of proof on whoever made the
suggestion...
"Aaron Bertrand [MVP]" <aaron@.TRASHaspfaq.com> wrote in message
news:O0b5Y7XIEHA.2300@.tk2msftngp13.phx.gbl...
> > Yesterday somebody told me that use char field like a primary key is
more
> > faster than integer field when you run a query.
> It really depends. Size of CHAR key? Volume of data? I can certainly
> create an example that shows a small table with a two-character primary
key
> and 99 rows that will respond to queries faster than a large table with an
> integer primary key and 99,999 rows.
> With everything being the same, I really doubt it will be faster.
> > Is that True, any suggestions.
> Test it! Since you know the size of data you'll be dealing with and the
> type you'll need to create, you already have all the tools at your
disposal
> to answer your own question... and are much better equipped than us at
> determining which is faster.
> --
> Aaron Bertrand
> SQL Server MVP
> http://www.aspfaq.com/
>