Test if a set exists before trying to drop it

心已入冬 提交于 2019-12-12 19:40:22

问题


In SQL before dropping a table I will check if it exists first so as not to cause an error, like so:

IF OBJECT_ID('TEMPDB..#table') IS NOT NULL 
  BEGIN 
      DROP TABLE #table 
  END

In MDX I could do with a way of checking if a set or member already exists before trying to drop it.

Currently I have the following stucture within some of my .mdx files. I add a custom set to the cube and then use that set in several scripts that follow i.e. they are mult-batch scripts.

/*
//I run the following manually...
DROP SET [XCube].[xSet]
*/

CREATE 
    SET [XCube].[xSet] AS
        blah blah

1.
SELECT x FROM XCube USING variousconditions AND xSet
GO

2.
SELECT y FROM XCube USING variousconditions AND ySet
GO

3.
SELECT y FROM XCube USING variousconditions AND ySet
GO

Putting the above context to one side my question is quite simple:

How do I test if xSet exists so that I can execute DROP SET [XCube].[xSet] only if required?


回答1:


I can give you a partial answer: You can find information about the objects of your cubes in schema rowsets, which are similar to the INFORMATION_SCHEMA views in relational databases. From SQL Server 2008 onwards, you can access these with a limited subset of SQL (no joins, just simple column_name = 'value' conditions in the where clause, ...) like this:

select * 
  from $SYSTEM.MDSCHEMA_SETS
 where SET_NAME = 'xSet'
   and CUBE_NAME = 'XCube'

Analysis Services automatically detects if the query is SQL or MDX (or, from SQL2012, DAX).

This would give you one record back if the set exists, and none if it does not exist. But I am not aware of a mechanism within MDX to execute code conditionally based on the result.



来源:https://stackoverflow.com/questions/18375416/test-if-a-set-exists-before-trying-to-drop-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!