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 2005 Forums
 Transact-SQL (2005)
 record count

Author  Topic 

chriztoph
Posting Yak Master

184 Posts

Posted - 2009-05-26 : 23:23:50
how to make condition like

if record_count(records) = 0 then
insert
else
insert


sample:

if recordcount = 0
BEGIN
INSERT INTO CustomerDetails
(Customer_ID,
Customer_First_Name,
Customer_Last_Name,
Address,
Telephone_Number)
VALUES
('CD001',
dbo.initcap('Fazna'),
dbo.initcap('Hudah'),
dbo.initcap('100, Shrubbery Gardens, Colombo - 04.'),
'011 2625691')

END
ELSE
BEGIN
INSERT INTO CustomerDetails
(Customer_ID,
Customer_First_Name,
Customer_Last_Name,
Address,
Telephone_Number)
--VALUES
SELECT MAX(dbo.AutoIDWithString(Customer_ID)),
dbo.initcap('Fazna'),
dbo.initcap('Hudah'),
dbo.initcap('100, Shrubbery Gardens, Colombo - 04.'),
'011 2625691'
FROM customerdetails
END

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-27 : 00:00:32
[code]
declare @recordcount int

select @recordcount = count(*)
from sometable

if @recordcount = 0
begin
. . .
end
else
begin
. . .
end
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

chriztoph
Posting Yak Master

184 Posts

Posted - 2009-05-27 : 01:01:09
quote:
Originally posted by khtan


declare @recordcount int

select @recordcount = count(*)
from sometable

if @recordcount = 0
begin
. . .
end
else
begin
. . .
end



KH
[spoiler]Time is always against us[/spoiler]






thanks..
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-05-27 : 02:43:12
You can also use

IF Exists(select........)
.
.
ELSE
.
.


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -