OuhscBbmc / BbmcResources

Public-facing resources and documents related to the BBMC (http://ouhsc.edu/bbmc/)
GNU General Public License v2.0
4 stars 0 forks source link

Dynamic `USE` sql statement #20

Closed wibeasley closed 7 years ago

wibeasley commented 7 years ago

@mand9472, please figure out a way to do the USE statement w/ variables.

Instead of

USE [go_round_in_circles]

Something like this (although this doesn't work):

DECLARE @database_name nvarchar(255); SET @database_name = '[go_round_in_circles]'
DECLARE @use nvarchar(max); SET @use = 'USE ' + @database_name
print 'database use  : ' + @use
EXECUTE sp_executesql @use

Here are two possible helpers:

mand9472 commented 7 years ago

The below sql should work

DECLARE @database_name nvarchar(255); SET @database_name = '[go_round_in_circles]' DECLARE @use nvarchar(max); SET @use = 'USE ' + @database_name print 'database use : ' + @use EXEC ( @use + ';' + 'any other statements to execute')

I have also emailed the sql for the above more descriptively ,Im closing this issue please let me know if you need this to be reopened.