Friday, February 24, 2012

About SQL Query

Hi Friends,

I have one Query

select Name from Test1 where ID in (Select ID from (
if ('a' = 'a')
begin
Select DISTINCT ID,Name from Test2
End
) xxx )

and i want the query in same format....but when i execute this it will give an error

Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'If'.
Server: Msg 170, Level 15, State 1, Line 26
Line 26: Incorrect syntax near ')'.

And if drop or remove if, begin ,end functionl;ity then it working fine....

Anyone help me solve this...

ASAP

AshutoshThere is no IF available in Transact-SQL. In general, the replacement for "IF" would be "CASE". But simply using CASE in the statement you provided will not work.

Maybe you could explain what it is you are trying to do, along with data examples...

Terri|||Hi Friend,

Actually i have a only query

if ('a' = 'a')
begin
Select DISTINCT ID,Name from Test2
End

Like this and that query

select Name from Test1 where ID in (Select ID from (
if ('a' = 'a')
begin
Select DISTINCT ID,Name from Test2
End
) xxx )

is generated automatically so error came there that query is not proper i can chage only my query and not the above ...so if any possibility to change that query above one only with if and begin then that problem will solve.....else tell me the other way.....i can change only this query it has 3 if statement so.......

select Name from Test1 where ID in (Select ID from (
if ('a' = 'a')
begin
Select DISTINCT ID,Name from Test2
End
) xxx )

might be u clear what i want now...still if not then i can explan u well

Ashutosh|||> There is no IF available in Transact-SQL.

errr... yes there is, though I agree not in the context you're thinking of. check Books Online.|||Hi Friends,

so how can i solve this problem? no way to solve this??

Ashutosh|||Sorry - I should have been clearer. IF is only available as a flow control command, not within a query.

Ashutosh -- I am still not clear. What is the purpose of the "xxx"? Could you please provide data examples to show the data you have and the result you are looking for?

Terri|||> so how can i solve this problem?

IF should be used so

IFsome criteria
BEGIN
sql statements here
END
ELSE
BEGIN
some other SQL statements here
END

GO

not in the middle of the statement as you've tried to use it. it should enclose complete SQL statements. Check the docs!

No comments:

Post a Comment