SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Development Tools
 Reporting Services Development
 Display Data on the basis of Parameter
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sunny_10
Yak Posting Veteran

72 Posts

Posted - 12/12/2012 :  09:25:54  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/12/2012 :  09:37:00  Show Profile  Reply with Quote
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 - 12/12/2012 :  10:05:24  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/12/2012 :  10:24:54  Show Profile  Reply with Quote
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 - 12/12/2012 :  10:52:51  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/12/2012 :  11:35:31  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000