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
 Development Tools
 Reporting Services Development
 Display Data on the basis of Parameter

Author  Topic 

sunny_10
Yak Posting Veteran

72 Posts

Posted - 2012-12-12 : 09:25:54
hi

I have 1 parameter month . If records exists in 1 table for that month then it should show data from that table , if not then it show data from another table. I am using Sql 2005 Sql Business Development Studio.

Thanks

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-12-12 : 09:37:00
You can do that in the query/stored procedure that populates the dataset - something like this
IF EXISTS 
(
SELECT *
FROM Table1
WHERE Monthcol = @Month
)
SELECT col1,
col2,
col3
FROM Table1
WHERE Monthcol = @Month
ELSE
SELECT col1,
col2,
col3
FROM Table2
WHERE Monthcol = @Month;
Go to Top of Page

sunny_10
Yak Posting Veteran

72 Posts

Posted - 2012-12-12 : 10:05:24
Hi

I have written this it says Incorrect Syntax near select

if exists
select * from [@temp] WHERE Monthcol = @Month
else
select * from [@m_temp1] WHERE Monthcol = @Month
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-12-12 : 10:24:54
Change it to
if EXISTS (select * from [@temp] WHERE Monthcol = @Month)
select * from [@temp] WHERE Monthcol = @Month
else
select * from [@m_temp1] WHERE Monthcol = @Month
Go to Top of Page

sunny_10
Yak Posting Veteran

72 Posts

Posted - 2012-12-12 : 10:52:51
Hi

Can u please tell me why u have written this line again

select * from [@temp] WHERE Monthcol = @Month

Secondly how this statement works

Thanks
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-12-12 : 11:35:31
IF EXISTS requires you to provide a subquery that will return 0 or more rows http://msdn.microsoft.com/en-us/library/ms188336.aspx

You will see what I mean if you to through the four selects that I have in the example below
-- This query returns 0 rows
SELECT 1 WHERE 0 = 1

-- So this query prints "No"
IF EXISTS (SELECT 1 WHERE 0 = 1)
PRINT 'Yes';
ELSE
PRINT 'No';
-------------------------------
-- This query returns one row
SELECT 1;

-- So this query prints "yes"
IF EXISTS (SELECT 1)
PRINT 'Yes';
ELSE
PRINT 'No';
Go to Top of Page
   

- Advertisement -