Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to check and assign boolean true false value

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2014-12-18 : 11:05:10
I am using teh below if condition, can you please provide the correct way of handling below criteria.

@ContractID is a varchar(70) text value

declare @CountCTR as bit

set @CountCTR = (select top 1 contractno from TAB_Contracts where contractno = @ContractID)

If @CountCTR <> true
BEGIN
will run an Insert query
END

Thanks a lot for the helpful info.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-12-18 : 15:28:55
Are you trying to insert if there is no row in the TAB_Contracts table where contractno column is equal to the value of @ContractID? If so
IF NOT EXISTS
(
SELECT * FROM TAB_Contracts
WHERE contractno = @ContractID
)
BEGIN
-- insert query here
END
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-18 : 15:47:27
OR:


insert into ...
select ...stuff to insert
where not exists (
select 1 from TAB_Contracts
where contractno = @ContractID
)
Go to Top of Page
   

- Advertisement -