473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Defining Database with Local Variable

I am trying to dynamically set the Database Name using a local variable...

I want to give access to a user to set the database name (New DBName Each Month) but not have access to the Script I'm writing... so I'm thinking the following

Declare @DBName nvarchar (255)
USE DBNameDatabase
Select @DBName = DBName From DBNameTable
Use @DBName

--Rest of script using @DBName as Database


Obviously doesn't work... any help would be appreciated

Thx
Oct 24 '06 #1
6 13571
actd
3
Anyone out there have any ideas on this - I'd like to do exactly the same thing :-)
Nov 29 '06 #2
iburyak
1,017 Expert 512MB
Where is your create database statement?

USE master

CREATE DATABASE @DBName
Nov 29 '06 #3
actd
3
Ah, a misunderstanding - it's the USE statement I want to be dynamic, and

USE @DBName just doesn't seem to work.

Any ideas would be greatly appreciated.


Where is your create database statement?

USE master

CREATE DATABASE @DBName
Dec 5 '06 #4
almaz
168 Expert 100+
I am trying to dynamically set the Database Name using a local variable...
Unfortunately you cannot dynamically change the database. All you can do is to prepare dynamic SQL and execute it:
Expand|Select|Wrap|Line Numbers
  1. declare @a nvarchar(4000)
  2. set @a = 'master'
  3. exec ('use ' + @a + ' select * from sysobjects')
  4.  
Dec 5 '06 #5
actd
3
That might do the trick - simple when you know how :-). I'll give it a go when I get a chance. Many thanks for this solution (assuming it does work for me).

Unfortunately you cannot dynamically change the database. All you can do is to prepare dynamic SQL and execute it:
Expand|Select|Wrap|Line Numbers
  1. declare @a nvarchar(4000)
  2. set @a = 'master'
  3. exec ('use ' + @a + ' select * from sysobjects')
  4.  
Dec 6 '06 #6
@actd
yes definitely it works
Feb 19 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
7
by: NS Develop | last post by:
What's wrong with the following: void SomeClass::someMethod(void) { A *ptrA = NULL; ptrA = &A(); .... }
2
by: Kench | last post by:
I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
3
by: Mike P | last post by:
I keep getting the error 'Use of unassigned local variable' in my code, which I have used before and it works fine : SqlTransaction Trans1, Trans2; SqlConnection...
2
by: silverburgh.meryl | last post by:
Can you please tell me what is the meaning this error in general? UnboundLocalError: local variable 'colorIndex' referenced before assignment In my python script, I have a variable define and...
5
by: FireHead | last post by:
Hello All, Its hard to explain but here it goes: char &free_malloc(char* object_copy){ Here ------------------------------------------------------------------ object_copy is parametre were...
14
by: Spitfire | last post by:
Hi All, I've this weird question about pointers. I would like to know how to return the address of a local variable, safely!! Isn't that a unrecommended procedure? Doesn't it have possibilities...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.