SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Prepared statement with dynamic table reference?


Prepared statement with dynamic table reference?

Author
Message
Ed-997158
Ed-997158
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 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'''

EXEC(@CheckRow)



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?

TIA,
Ed
Welsh Corgi
Welsh Corgi
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10158 Visits: 4894
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...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search