Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Prepared statement with dynamic table reference? Expand / Collapse
Posted Thursday, February 25, 2010 2:23 PM


Group: General Forum Members
Last Login: Tuesday, July 9, 2013 2:43 PM
Points: 17, Visits: 55
Is is possible to dynamically reference a database and / or a table in a prepared statement?

I'm reviewing a shared stored procedure that takes nvarchar arguments for the name of a database and several tables. It uses them in dynamic sql that is called many times, and I think there would be a significant performance improvement if the dynamic sql could be replaced by a prepared statement.

The proc creates some temp tables and populates them with information retreived from INFORMATION_SCHEMA of the passed-in database, via dynamic sql.

The proc then interates over the temp tables, and in each iteration, builds a string for dynamic sql execution, using both temp table column values and the passed-in database and table values. If it wasn't for the database and table values, I could rewrite the string outside of the iteration loop and execute it via sp_executesql.

For example, if it weren't for the @database reference in the following code:

		  set @CheckRow =
'insert #RowsUsed ' +
'select ''' + @CheckRowAlias + ''', ''' + @CheckRowTable + ''', ''' + @CheckRowColumn + ''' ' +
'where exists (select 1 ' +
'from [' + @database + '].information_schema.ROUTINE_COLUMNS ' +
'where TABLE_NAME = ''' + @Function + ''' ' +
'and COLUMN_NAME = ''' + @CheckRowAlias + '.' + @CheckRowColumn + ''') ' +
'and not exists (select 1 ' +
'from #PKUsage ' +
'where [PK] = ''' + @CheckRowTable + '.' + @CheckRowColumn + ''') ' +
'and ''' + @CheckRowColumn + ''' != ''PersonPartyID'''


It could be rewritten as:

		  set @CheckRow =
N'insert #RowsUsed ' +
'select ''@CheckRowAlias'', ''@CheckRowTable'', ''@CheckRowColumn'' ' +
'where exists (select 1 ' +
'from [ExplicitDatabase].information_schema.ROUTINE_COLUMNS ' +
'where TABLE_NAME = ''@Function'' ' +
'and COLUMN_NAME = ''@CheckRowAlias.@CheckRowColumn'') ' +
'and not exists (select 1 ' +
'from #PKUsage ' +
'where [PK] = ''@CheckRowTable.@CheckRowColumn'') ' +
'and ''@CheckRowColumn'' != ''PersonPartyID'''

set @checkArgs =
N'@CheckRowAlias nvarchar(100),@CheckRowTable nvarchar(100),@checkRowColumn nvarchar(100), @Function nvarchar(100)'
EXEC sp_executesql @CheckRow,@checkArgs,@CheckRowAlias,@CheckRowTable,@CheckRowColumn,@Function

I know there's no way to pass in a table parameter, but is there some way to obtain a reference, or perhaps clone the table to a temp table somehow?

Post #873010
Posted Thursday, February 25, 2010 5:12 PM

SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:44 PM
Points: 4,843, Visits: 4,694
Converting it to an sp_executesql would be better than embedded Dynamic SQL for various reasons but it would be much better if you create a Stored Procedure and call the SP...

Please consider Security, SQL Injection, Execution Plans, Reusability of code, etc.

Also when you deploy your code to production it makes it more difficult to identify the source of the problem and is conducive to finger pointing between develop[developers and DBA's.

It makes me cringe when I hear a DBA say that the would prefer to have less Stored Procedures and Functions because it is more work for them.

These are not Mom & Pop Shops but very large Fortune 100 to 1000 Companies...

There are a number of other reasons why you should call SP's.

For better, quicker answers on T-SQL questions, click on the following...

For better answers on performance questions, click on the following...

Post #873105
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse