|
|
|
Old Hand
      
Group: General Forum Members
Last Login: 2 days ago @ 12:47 PM
Points: 360,
Visits: 1,072
|
|
Good afternoon everyone! I'm not a regular developer so please excuse my question for being easy for what would be a good developer. I'm trying to create a statement that returns database names for all servers. However, I only want the principal database names on mirrored servers. However, I receive the error: Incorrect syntax near '='. Any advice is greatly appreciated!
Select d.name, d.compatibility_level FROM sys.database_mirroring M inner join SYS.DATABASES d on m.database_id = d.database_id where CASE WHEN B.mirroring_state is NOT NULL THEN mirroring_role_desc ='Principal' and D.database_id >=5 WHEN B.mirroring_state is NULL THEN D.database_id >=5 END as MirroringState ORDER BY A.NAME
¤ §unshine ¤
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 10:41 AM
Points: 3,788,
Visits: 5,538
|
|
sunshine-587009 (12/7/2012) Good afternoon everyone! I'm not a regular developer so please excuse my question for being easy for what would be a good developer. I'm trying to create a statement that returns database names for all servers. However, I only want the principal database names on mirrored servers. However, I receive the error: Incorrect syntax near '='. Any advice is greatly appreciated!
Select d.name, d.compatibility_level FROM sys.database_mirroring M inner join SYS.DATABASES d on m.database_id = d.database_id where CASE WHEN B.mirroring_state is NOT NULL THEN mirroring_role_desc='Principal' and D.database_id >=5 WHEN B.mirroring_state is NULL THEN D.database_id >=5 END as MirroringState ORDER BY A.NAME
I believe you are misunderstanding the function of the CASE expression. Like any other expression (column name, variable, function, calculation) , CASE only returns a single scalar value. You are trying to use it to generate SQL statements dynamically, which is quite a different matter. Try this instead:
where D.database_id >=5 and mirroring_role_desc like CASE WHEN B.mirroring_state is NOT NULL then 'Principal' else null end
By the way, where is your B.mirroring_state supposed to come from? There is no table in your query that is aliased "B".
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller Stop, children, what's that sound? -- Stephen Stills
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 7:46 PM
Points: 145,
Visits: 541
|
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: 2 days ago @ 12:47 PM
Points: 360,
Visits: 1,072
|
|
Thanks Dixie! My oops on the referencing aliases.
When I run it on CMS it only brings me back the principal databases on the mirrored servers which is great! But I also want all database names on the rest of the servers that are not mirrored.
¤ §unshine ¤
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: 2 days ago @ 12:47 PM
Points: 360,
Visits: 1,072
|
|
Here's my updated script with correct aliases and your suggestion.
Select d.name, d.compatibility_level FROM sys.database_mirroring M inner join SYS.DATABASES d on m.database_id = d.database_id where D.database_id >=5 and mirroring_role_desc like CASE WHEN M.mirroring_state is NOT NULL then 'Principal' else null end ORDER BY D.NAME
¤ §unshine ¤
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: 2 days ago @ 12:47 PM
Points: 360,
Visits: 1,072
|
|
I think I have the case statement in the wrong place?
My logic would be: If it is mirrored: Select database names from mirrored servers that are principal role
But if it is not: select all database names.
¤ §unshine ¤
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 10:41 AM
Points: 3,788,
Visits: 5,538
|
|
sunshine-587009 (12/7/2012) Here's my updated script with correct aliases and your suggestion.
Select d.name, d.compatibility_level FROM sys.database_mirroring M inner join SYS.DATABASES d on m.database_id = d.database_id where D.database_id >=5 and mirroring_role_desc like CASE WHEN M.mirroring_state is NOT NULL then 'Principal' else '%' end ORDER BY D.NAME
Select d.name, d.compatibility_level FROM sys.database_mirroring M inner join SYS.DATABASES d on m.database_id = d.database_id where D.database_id >=5 and mirroring_role_desc like CASE WHEN M.mirroring_state is NOT NULL then 'Principal' else '%' -- like ANYTHING end ORDER BY D.NAME
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller Stop, children, what's that sound? -- Stephen Stills
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: 2 days ago @ 12:47 PM
Points: 360,
Visits: 1,072
|
|
Still only brings back the principal databases from mirrored servers.
¤ §unshine ¤
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 10:41 AM
Points: 3,788,
Visits: 5,538
|
|
Select d.name, d.compatibility_level, m.mirroring_state, mirroring_role_desc FROM sys.database_mirroring M inner join SYS.DATABASES d on m.database_id = d.database_id where D.database_id >=5 and ISNULL(mirroring_role_desc,'') like CASE WHEN M.mirroring_state is NOT NULL then 'Principal' else '%' -- like anything (except nulls) end ORDER BY D.NAME
The problem was the mirroring_role_desc column contained NULL values. The LIKE comparison wouldn't return NULLs, they have to be tested for with an IS NULL comparison. The solution is to use ISNULL to set NULL mirroring_role_desc values to blank ('') and then the LIKE '%' test can work against the result.
When trying to debug problem queries, I always include any columns in the output set that I am testing against. After the correct rows are being delivered, the extraneous columns can be removed.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller Stop, children, what's that sound? -- Stephen Stills
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 10:41 AM
Points: 3,788,
Visits: 5,538
|
|
I feel compelled to add this. As a general rule, you should not test against functions like ISNULL, because that can prevent the optimizer from taking advantage of a suitable index in the query plan. In the case at hand, there aren't enough rows to make a huge difference. I presume this query is only going to be run occassionally.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller Stop, children, what's that sound? -- Stephen Stills
|
|
|
|