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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Create View with the IF or CASE statment

Author  Topic 

Jewel_33
Starting Member

3 Posts

Posted - 2006-09-19 : 14:03:37
Hello.
Guys, I really need your help!
I need to create a View, but check the ServerID. If ServerID = 4, I need to make one set of select statment, Else - do other select. For example:
Create View View_Name
IF ServerID =4 then
BEGIN
SELECT * from TableName1
END
ELSE
BEGIN
SELECT * from Table Name2
END

I have a SQL syntax error. But maybe, here is another way to do some thing like this?? Thank you!

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-09-19 : 14:11:03
What is ServerID? What table is it stored in?



CODO ERGO SUM
Go to Top of Page

Jewel_33
Starting Member

3 Posts

Posted - 2006-09-20 : 10:28:22
Hello. The ServerId is a variable from
function dbo.GetServerID(). But anyway, I am not going to use this variable, I will use just a dbo.GetServerID().
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-20 : 10:32:49
I don't think you can use IF in a query.
However, you can use it in a function.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-09-20 : 10:33:39
Instead of view, you need to use Function which returns Table as return data type.

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-09-20 : 11:00:36
You could do the view like this.

Create View View_Name
as
SELECT * from TableName1 where dbo.GetServerID() = 4
union all
SELECT * from TableName2 where dbo.GetServerID() <> 4


CODO ERGO SUM
Go to Top of Page

Jewel_33
Starting Member

3 Posts

Posted - 2006-09-20 : 11:30:39
I don't think I can do UNION in this case. The problem is the number of expressions in the select. In my example, I did not show the real SQL statment. In the real word, the first select is just a several joins, but second is select * from AnotherView. The number of the expressions in the both select are different. The Union will not work!
Thank you all for the help!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-09-20 : 11:45:42
Well, this is a tad confusing. Maybe if you explain what you are trying to do it might help.

Also, read the hint link in my sig to help you formulate your post.



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-09-20 : 12:01:53
quote:
Originally posted by Jewel_33

I don't think I can do UNION in this case. The problem is the number of expressions in the select. In my example, I did not show the real SQL statment. In the real word, the first select is just a several joins, but second is select * from AnotherView. The number of the expressions in the both select are different. The Union will not work!
Thank you all for the help!



It's good that you waited till after the question was answered to explain the true situation. I love playing 20 questions.






CODO ERGO SUM
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-09-20 : 12:07:27
In any case...dynamic View creation based on the server you are on?

That's pretty out there.

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-09-20 : 12:15:50
quote:
Originally posted by Michael Valentine Jones

quote:
Originally posted by Jewel_33

I don't think I can do UNION in this case. The problem is the number of expressions in the select. In my example, I did not show the real SQL statment. In the real word, the first select is just a several joins, but second is select * from AnotherView. The number of the expressions in the both select are different. The Union will not work!
Thank you all for the help!



It's good that you waited till after the question was answered to explain the true situation. I love playing 20 questions.






CODO ERGO SUM



So, you want a single View to return completely different columns depending on some parameter? Even if this was allowed, how would you expect to ever use this View if you don't know in advance which columns it will return?

I think you should really step back and explain what your situation is and why you think this is what you need. I guarantee you it is not. i.e., don't tell us *what* you are trying to do, try to explain to us *why* you are trying to do it.

- Jeff
Go to Top of Page
   

- Advertisement -