Question: I'm trying this and getting an error.

```
SELECT Count(*) FROM
        CASE @Type
            WHEN 'UserName' THEN Users WHERE UserName = @Name;                              
            WHEN 'UserGroup' THEN Groups Where GroupName = @GroupName AND GroupType = 'USERGROUP'
        END

```

I know you generally use case after an '=' sign ... but is there a way to do what I am trying here? 

sql server 2008.

Answer: 
You need to either write dynamic SQL and execute that, or us an IF statement and two queries...

```
IF (@type = 'UserName')
  SELECT COUNT(*) FROM Users WHERE UserName = @name
ELSE IF (@type = 'UserGroup')
  SELECT COUNT(*) FROM Groups WHERE GroupName = @GroupName AND GroupType = 'USERGROUP'

```
**Nope.**

The only way to change your fields, tables, or database dynamically in a query is to use [Dyanmic SQL.](http://www.sommarskog.se/dynamic_sql.html) It's a dangerous thing though, so tread carefully.