Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 20th, 2007, 06:19 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default If Exist then Update

Hi,

I want to Update a SQL Table value on condition that a 2nd Table exists.

ie.
IF Table2 exists

Code:
UPDATE dbo.Update_Status
    SET dbo.Update_Status.Status = '1'
WHERE EXISTS (SELECT 1 
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_TYPE='BASE TABLE' 
    AND TABLE_NAME='Table2')


The above code produces (0) Rows Affected

How do I output the value of '1'?

Thanks in advance,


Neal

A Northern Soul
__________________
Neal

A Northern Soul
 
Old July 21st, 2007, 01:36 PM
Registered User
 
Join Date: Jul 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I experienced unhealthy results when using EXISTS or NOT EXISTS so I switched to using COUNT(*); you can write your code a little bit different, use this syntax “I use it all the time and it works without issues”

If (SELECT COUNT(*)
    FROM INFORMATION_SCHEMA.TABLES
        WHERE TABLE_TYPE='BASE TABLE'
        AND TABLE_NAME='Table2') > 0
BEGIN
    UPDATE dbo.Update_Status
        SET dbo.Update_Status.Status = '1'
END



Database Analyst/Arch
 
Old July 21st, 2007, 03:41 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

I'm curious what sort of "unhealthy results" you experienced.

The EXISTS predicate is much more efficient than computing a COUNT. It is simply a test for a non-empty set, whereas COUNT must actually go and, er, count things...

Jeff Mason
[email protected]
 
Old July 23rd, 2007, 12:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Neal,

Having ZERO as output for the Update Statement that you posted, I dont think TABLE2 exists. Did you try executing the subquery part separately to find if that works as expected?
Code:
SELECT 1 
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_TYPE='BASE TABLE' 
    AND TABLE_NAME='Table2
    Cheers

_________________________
- Vijay G
Strive for Perfection
 
Old July 25th, 2007, 07:49 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Thanks guys. Very much appreciated.


Neal

A Northern Soul





Similar Threads
Thread Thread Starter Forum Replies Last Post
if an attribute exist kgoldvas XSLT 4 July 4th, 2007 07:24 AM
record exist or not keyvanjan Classic ASP Basics 2 May 5th, 2006 11:39 AM
Table Exist? elansolutionsltd Access VBA 5 January 23rd, 2006 03:43 AM
Cookie Exist Gimble ASP.NET 1.0 and 1.1 Basics 2 September 3rd, 2004 03:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.