Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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 = @MonthELSE SELECT col1, col2, col3 FROM Table2 WHERE Monthcol = @Month;
sunny_10
Yak Posting Veteran
72 Posts
Posted - 2012-12-12 : 10:05:24
Hi I have written this it says Incorrect Syntax near selectif existsselect * from [@temp] WHERE Monthcol = @Monthelseselect * from [@m_temp1] WHERE Monthcol = @Month
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 = @Monthelseselect * from [@m_temp1] WHERE Monthcol = @Month
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 againselect * from [@temp] WHERE Monthcol = @MonthSecondly how this statement worksThanks
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.aspxYou will see what I mean if you to through the four selects that I have in the example below
-- This query returns 0 rowsSELECT 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 rowSELECT 1;-- So this query prints "yes"IF EXISTS (SELECT 1) PRINT 'Yes';ELSE PRINT 'No';