Kale
Kale

Reputation: 65

If else query SQL

I want to load some tables into my application but the only tables I want are the tables that have been updated.

I got in some of the databases I got an table called "Updated" but I don't have this table in every database in other databases I use the last 100 from the field ID.

What I want to do is an if/else statement where something like this happens:

IF EXISTS SELECT * FROM table1 WHERE column1 = updated 
ELSE SELECT TOP 100 table1 FROM column1 ORDER BY column1 DESC

I hope I made myself clear and hope you guys can help me.

Upvotes: 1

Views: 98

Answers (2)

Anant Dabhi
Anant Dabhi

Reputation: 11104

you cant start else condition into second line if you not use begin and and

IF EXISTS SELECT * FROM table1 WHERE column1 = updated 
BEGIN
-- logic
END


ELSE 
BEGIN
SELECT TOP 100 table1 FROM column1 ORDER BY column1 DESC    
END

for your above quety you can write direcly

IF NOT EXISTS SELECT * FROM table1 WHERE column1 = updated 
      SELECT TOP 100 table1 FROM column1 ORDER BY column1 DESC  

Upvotes: 1

Indian
Indian

Reputation: 527

Your Sql-query is not correct. You should use somethine like this:

IF EXISTS (SELECT * FROM table1 WHERE column1 = updated)
SELECT * FROM table1 WHERE column1 = updated 
ELSE SELECT TOP 100 table1 FROM column1 ORDER BY column1 DESC

Upvotes: 1

Related Questions