Results 1 to 7 of 7

Thread: Check if exist

  1. #1
    Join Date
    Sep 2005
    Posts
    74

    Unanswered: Check if exist

    Hi guys help please..is there a function in MS SQL that check if a particular value exist in a row and would return a boolean value base from what found, Return True if it found something and False if it does not found one. I've try the EXISTS function but I cant get the rigth syntax..Any help will be greatly appreciated!

    OR Maybe you can help me directly with my problem. I want to check first in my Table 1 with 3 columns if value X exists in column 1 and if X exists UPDATE that column with value Y and if value X does not exists INSERT something in the Table 1. Any suggestion or Comments will be greatly appreciated!
    Last edited by daimous; 08-30-07 at 04:05.

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hi

    Post what you've got for your exists syntax. It should merely require some tweaking.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Sep 2005
    Posts
    74
    Here it is.
    EXISTS(select Sales_Date from CFREE_Sales where Sales_Date = '8/31/2007 12:00:00 AM')

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Try:
    Code:
    IF EXISTS(select Sales_Date from CFREE_Sales where Sales_Date = '20070831') BEGIN
    PRINT 'It exists'
    END
    ELSE BEGIN
    PRINT 'It does not exist'
    END
    What is the result
    Testimonial:
    pootle flump
    ur codings are working excelent.

  5. #5
    Join Date
    Sep 2005
    Posts
    74
    Yah...Thats what I need..Thanks a lot!

  6. #6
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    An alternative to
    Code:
    if exists (select * from #t1 where c1='x') begin
      update #t1 set c2=c2+100 where c1='x'  
    end 
    else begin
      insert into #t1 values ('x',100)
    end
    is
    Code:
    update #t1 set c2=c2+100 where c1='x'
    if @@rowcount=0 begin
      insert into #t1 values ('x',100)
    end

  7. #7
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by pdreyer
    Code:
    update #t1 set c2=c2+100 where c1='x'
    if @@rowcount=0 begin
      insert into #t1 values ('x',100)
    end
    bingo!
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •