Comments on: ExecuteSQL() and @@ObjectName Are Lying To Me https://thomaslarock.com/2010/10/executesql-and-objectname-are-lying-to-me/ Thomas LaRock is an author, speaker, data expert, and SQLRockstar. He helps people connect, learn, and share. Along the way he solves data problems, too. Sat, 03 Mar 2018 17:08:52 +0000 hourly 1 https://wordpress.org/?v=6.8 By: ThomasLaRock https://thomaslarock.com/2010/10/executesql-and-objectname-are-lying-to-me/#comment-14149 Wed, 11 May 2016 16:45:00 +0000 http://thomaslarock.com/?p=4894#comment-14149 In reply to Dave Brown.

First, I’m giving you a special #datahug for leaving a comment on a blog post that is 5+ years old. That’s awesome.

Second, I need to clean this post up, the formatting looks awful. This is the issue when you have over 1,000 posts spread across many different themes. I’m starting to think we should go back to plain text and green screens.

Lastly, thanks for sharing your workaround!

]]>
By: Dave Brown https://thomaslarock.com/2010/10/executesql-and-objectname-are-lying-to-me/#comment-14147 Wed, 11 May 2016 16:00:00 +0000 http://thomaslarock.com/?p=4894#comment-14147 Unfortunately there is a problem with the Index facet
that will still restrict your ability to use a technique I use to get around
this issue but I can tell you how to get the Index name into your ExecuteSql()
query. With the Index facet the @ID property is the index for the TABLE, not
for the DATABASE, as the documentation states (Microsoft needs to add another
property to indicate which TABLE the ID is referring to). However, if you have
a naming standard that every index must have a unique name (within the
database) then you’re set to go.

There is a @Name property for the Index Facet – just about all facets have one.
So you can concatenate values together to input that property into the
statement you are going to execute. Let’s pretend that we want to get the index
type description (column [type_desc] from sys.indexes) and say that all indexes
that start with PK must be CLUSTERED. We include a table filter to handle the
index names that start with PK so we are left with returning the index type
description. The SQL Statement would be:

DECLARE @IndexName VARCHAR(128);

SET @IndexName = ‘Your Index Name Here’;

SELECT [type_desc] FROM [sys].[indexes] WHERE [name] = @IndexName;

To run and execute this from the Index facet you assemble this statement,
replacing the “Your Index Name Here” with the value of the @Name property. You
do this by multiple iterations of the Concatenate function. Then you use
ExecuteSql with the new statement that you’ve created. So the left side of your
expression looks like this:

ExecuteSql(‘STRING’, Concatenate(Concatenate(‘DECLARE @IndexName
VARCHAR(128);SET @IndexName = ”’, @Name), ”’;SELECT [type_desc] FROM
[sys].[indexes] WHERE [name] = @IndexName;’))

I like to set a variable up front so that I don’t have to keep concatenating
the value into the query.

Of course in most objects you have an @ID column. In those cases I use the
STRING(@ID) function during the concatenation.

]]>