home
training courses
why choose us?
solutions
support
company
LearnItFirst User Forum
Welcome Guest
Search
|
New Posts
|
Members
|
Log In
|
Register
SQL Server Forum - LearnItFirst.com
»
Transact-SQL Programming
»
Stored Procedures, Triggers, & Functions
»
Using 'IF Condition' in Table-valued Functions
Using 'IF Condition' in Table-valued Functions
Options
Previous Topic
·
Next Topic
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.
Back to top
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.
Back to top
Users browsing this topic
Guest
Forum Jump
SQL Server Database Administration
- General SQL Database Question & Answer
- Backup, Recovery and Disaster Recovery
- SQL Server Security
- Integration Services (SSIS) and DTS
Transact-SQL Programming
- DML (SELECT, INSERT, UPDATE, DELETE) Questions
- Stored Procedures, Triggers, & Functions
SQL Server Scripts, Code Samples and SSMS Custom Reports
- All SQL Server Versions
- SQL Server 2005/2008
- SQL Server Management Studio Custom Reports
Customer Service
- Video Requests
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.
SQL Server 2005 DBA Training Videos
SQL Server 2008 DBA Training Videos
Email this topic
RSS Feed
Watch this topic
Print this topic
Normal
Threaded