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_NameIF ServerID =4 then BEGIN SELECT * from TableName1 ENDELSE BEGIN SELECT * from Table Name2 ENDI 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 |
 |
|
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(). |
 |
|
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 LarssonHelsingborg, Sweden |
 |
|
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 AthalyeIndia."Nothing is Impossible" |
 |
|
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_NameasSELECT * from TableName1 where dbo.GetServerID() = 4union allSELECT * from TableName2 where dbo.GetServerID() <> 4 CODO ERGO SUM |
 |
|
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! |
 |
|
X002548
Not Just a Number
15586 Posts |
|
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 |
 |
|
X002548
Not Just a Number
15586 Posts |
|
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 |
 |
|
|