LearnItFirst User Forum

SQL Server 2008 SSAS Training Videos
Welcome Guest Search | New Posts | Members | Log In | Register

Using 'IF Condition' in Table-valued Functions Options
ravurugopinath@gmail.com
Posted: Saturday, March 28, 2009 12:40:45 AM
Rank: Newbie

Joined: 3/28/2009
Posts: 3
Points: -91
Where do you live?: bangalore,India
I am passing a flag value which will store either 0 or 1 .

Then when flag value is 1 then i need to execute a query and when flag value is 0 then i need to execute other query

i.e for example

if(@flag=1)
begin
select * from tblCustomer
end

if(@flag=0)
begin
select * from tblEmployee
end

the two tables does not contains any flag column field.

One more thing i have done like this in stored procedures but why not i am not able to do this functions?

Really i am confused tried many times. I am calling functions inside the stored procedures.

Please any body help me.
Scott Whigham
Posted: Monday, March 30, 2009 10:53:33 AM


Rank: Super Mod

Joined: 3/20/2006
Posts: 476
Points: 1,053
Where do you live?: Dallas, TX
Hi there -

The only way you can do that is with a multistatement table function a la:
Quote:
CREATE FUNCTION MyFunction(@ShouldI BIT)
RETURNS @MyTable TABLE (ColumnA INT)
AS
BEGIN
IF(@ShouldI = 1)
BEGIN
INSERT @MyTable (ColumnA)
SELECT OtherColumn FROM OtherTable1
END
ELSE
BEGIN
INSERT @MyTable (ColumnA)
SELECT OtherColumn FROM OtherTable2
END

RETURN
END
I didn't check syntax but that's the basic gist of it.
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.