IF condition in view in SQL Server

Views only allow select statements as stated in here

if you need to do if on column values you can use a

SELECT
CASE WHEN COLUMN1 = 1 THEN COLUMNX ELSE COLUMNY END
FROM TABLE1

if your need exceeds this you should create a select from a table valued function instead of a view.

What you need is a simple Procedure

CREATE PROCEDURE DOSOMETHING
(   
    @ID INT
)
AS
BEGIN
    IF @ID > 100
        SELECT 1 AS ID,'ME' AS NAME, GETDATE() AS VARIABLEDATECOL, NEWID() AS VARIABLEGUID
    ELSE
        SELECT 2 AS ID, 'YOU' AS NAME
END

No I don't believe this is possible.

You could use a stored procedure instead to achieve this functionality.


You could try something sneaky with a UNION :

SELECT {fieldlist}
FROM Table1
WHERE EXISTS(SELECT EmpID FROM EmployeeDetails WHERE ID = 200)

UNION ALL

SELECT {fieldlist}
FROM Table2
WHERE NOT EXISTS(SELECT EmpID FROM EmployeeDetails WHERE ID = 200)

This method would require both SELECT statements to return the same set of fields, although their sources might be different.