Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If all you want to do is print the names of the databases the table is in, your script can be much simpler (I fail to see the point of retrieving the table name and printing it every time - what could it be other than <code>SourceDestinations</code>?):</p> <pre><code>DECLARE @sql NVARCHAR(MAX); SET @sql = N''; SELECT @sql = @sql + 'IF EXISTS (SELECT 1 FROM ' + QUOTENAME(name) + '.sys.tables WHERE name = ''SourceDestinations'') PRINT ''' + name + ''';' FROM sys.databases WHERE name LIKE 'PULL_%'; EXEC sp_executesql @sql; </code></pre> <p>I suspect though that you want to do more with this once you've determined where the actual tables live. Ed is absolutely right, you can't concatenate a string to pass into sp_executesql as you are executing it, you must build it beforehand. This is true for all stored procedure calls, e.g. you can't say:</p> <pre><code>EXEC sp_who2 'act' + 'ive'; </code></pre> <p>Even though it should be the same thing as:</p> <pre><code>EXEC sp_who2 'active'; </code></pre> <p>You seem to know this already, at least to some extent, because you declared a <code>@sql</code> variable (though you never use it).</p> <p>I would change your code to:</p> <pre><code>DECLARE d CURSOR LOCAL STATIC FORWARD_ONLY READ_ONLY FOR SELECT name FROM sys.databases WHERE name LIKE 'Pull_%'; DECLARE @db_name NVARCHAR(128), @sql NVARCHAR(MAX); OPEN d; FETCH NEXT FROM d INTO @db_name; WHILE @@FETCH_STATUS = 0 BEGIN PRINT @db_name; SET @sql = N'IF EXISTS (SELECT 1 FROM ' + QUOTENAME(@db_name) + '.sys.tables WHERE name = ''SourceDestinations'') PRINT ''' + @db_name + ''';' EXEC sp_executesql @sql; FETCH NEXT FROM d INTO @db_name; END CLOSE d; DEALLOCATE d; </code></pre> <p>A couple of key points:</p> <ol> <li>Don't use the default cursor options. In this case it's probably not a huge deal, but it's a bad habit to get into. I highlight one case of performance impact in this blog post: <a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2011/03/08/t-sql-tuesday-16-this-is-not-the-aggregate-you-re-looking-for.aspx" rel="nofollow">http://sqlblog.com/blogs/aaron_bertrand/archive/2011/03/08/t-sql-tuesday-16-this-is-not-the-aggregate-you-re-looking-for.aspx</a></li> <li>Don't use <code>varchar(50)</code> for database/table names. These should be <code>nvarchar(128)</code> according to the rules for identifiers (<a href="http://msdn.microsoft.com/en-us/library/aa223962%28SQL.80%29.aspx" rel="nofollow">this doc is from 2000</a> but <a href="http://msdn.microsoft.com/en-us/library/ms175874.aspx" rel="nofollow">similar docs exist for newer versions</a>).</li> <li>Also you should be checking for schema as well. You may get multiple hits for <code>SourceDestinations</code> if someone has inadvertently created one in their own default schema.</li> <li>Your <code>@sql</code> string should probably be longer than 100 characters. I typically use <code>MAX</code> in these cases because the performance difference isn't worth sitting there wondering whether 255 or 1024 etc. will be enough characters.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload