Quick Question: How Do I Get The Name Of The Current Database?
Jul 23, 2005
Is there an sp_zzzzzz function to return the name of the current database?
I would like to use this name as a variable in a stored procedure in order
to create names for further databases (by appending a tag, such as
MYDATABASE_BLOB001, ..._BLOB002 etc.
Any way to build a small GUI that a user can input the parameter, then click a submit button, which will in turn run a script on the database and pass the value to the query. I am sure there is a quick and easy way, but cannot think of one at the moment.
This is a quick script that I am running for a user too often, and don't really want to give them server access or ssms on their machine.
I have the MS SQL2000 database failed to recover at computer restart. Now the database is marked suspect. How can I manage to recover the data? Thank you.
We have installation of Dbase Engine and SSIS that is PRODUCTION, and want to replace with newer hardware. In "the old days", we built "boxname_new" and installed SQL with "sqlname_new", took PROD users off-line, and quickly renamed original boxes/SQL and new boxes/SQL to original name, copied data and off we went with upgrade.
NOW, the "renaming" option for SQL tools is not supported, but with re-installation.
Has anyone developed game plan steps for accomplishing hardware upgrade, including SQL environment swap with MINIMAL downtime for PRODUCTION environment? Can you share?
Guys,1st of all.. sorry about my english ok? Im from Brazil...Im trying to get a RS connecting through 2 databases with the querybelow:SELECTscar..osbalcao.numeroos,ccell.dbo.atrib_os.osFROMccell.dbo.atrib_osJOIN scar.dbo.osbalcao on ccell.dbo.atrib_os.os =scar.dbo.osbalcao.numeroosWHERE scar.dbo.osbalcao.localprod = '257'AND scar.dbo.osbalcao.status in ('EM')On a SQL IDE i got a good RS, but, when i try to use that same query onDelphi + ZEOS, the SQL Server tells me that "The database namecomponent of the object qualifier must be the name of the currentdatabase".I really dont understand why. I can only tell that the error isreturned from SQL Server.Have anyone got the same trouble?[]'sTiago Gigli
Hi, I want to know a sql query that would show the current database name. I have tried looking into the online books but couldn't find the right query.I hope u can do better.This is urgent.Thanxs. -- Vijay
I need to know the current database name in executing a stored procedure, which will trancatte the log of that database. (in a dynamic multiple customerID oriented database environment, ex. custID_101_db, custID_102_db, .....)
I was wondering if it was possible to check what server database you are currently connected to, using T-SQL, when executing commands in Query Analyzer. Even though you choose the server and database when connection, sometimes by habit you may connect to the wrong server & DB, and execute an .SQL file. Is it possible to put a line of T-SQL at the beginning that performs this pseudo-code, as a safety feature:-- While in Query Analyzer with a certain .SQL file open:If current server <> 'TheCorrectServer' and current DB <> 'TheCorrectDB' then cancel this .SQL file execution.
I have a *.sql script that creates database tables, and I need to modify the database to enable the service broker. In addition, the actual name of the database is not known in advance - it is set per instance of the application.
I know I need to do:
ALTER DATABASE dbname SET ENABLE_BROKER
But I must avoid including the name of the database in the script. I did wonder if this would work:
DECLARE @DB varchar(50)
SELECT @DB = DB_NAME()
ALTER DATABASE @DB SET ENABLE_BROKER
But I just get a syntax error. Presumably this also rules out setting the database name as a parameter to the script (SqlParameter stuff)
The only option I can think of is dynamically creating the statement, either in T-SQL or in the calling .NET environment.
Can someone please help me with the following error?
I am trying to create a trigger that copies data from a table in one database to another. However I am getting the error 'Cannot create trigger on databasename.dbo.tablename as the target is not in the current database.
Below is the SQL I am using to create the trigger:
Hi, I've just started using SQL Server Express with VWDE 2005 and I have a database with one table called EVENTS which has a datetime column called DATE. I want to select records where the DATE value is in the next two months.First I tried selecting records where the DATE value is >= today. I used the query builder to produce... SELECT [Date], [Title] FROM [Events] WHERE [Date] >= GETDATE()but got the error "Undefined function 'GETDATE' in expression".Am I missing something obvious?
Hi, I'm trying to create an SQL script that creates a new login account for each of our databases. The one remaining problem with the sql is it could not set the current database using a variable(e.g. USE @dbasename). I tried looking for another way of setting the current database but could not find one.
Is there any other way of setting the current database other than the USE command?
Hello,I want to select the names and number of rows of all tables in thecurrent database, whose name starts with 'sys'my query is:select o.[name], sum(i.[rows]) as numbersfrom sysobjects oinner join sysindexes ion (o.[id] = i.[id])where o.[name] like 'sys%'and i.indid=1group by o.[name]the result of the query is (depending of the database) :name numberssysaltfiles 14syscharsets 114syscolumns 4934syscomments 2035sysconfigures 38sysdatabases 7sysdepends 5524sysdevices 6sysfilegroups 1sysfulltextcatalogs 0sysfulltextnotify 0sysindexes 98syslanguages 33sysmessages 3795sysobjects 1285syspermissions 806sysproperties 0sysreferences 0sysservers 1systypes 26sysusers 14sysxlogins 3But when I count the number of rows of "sysobjects", I getnumbers=1298, which is different from the result displayed above andsame goes for "syscolumns".I also tried "dbcc updateusage [0]" to update the SQL Server but Ididnt help me.Can anyone please advice me on this behaviour?
I've this query SELECT t1.ID, t1.Date, t1.Time, t1.VALUE FROM RESULT WHERE t1.Date>=CONVERT(VARCHAR(10),DATEADD(d,-7,GETDATE()),101) AND t1.Date<CONVERT(VARCHAR(10), GETDATE(), 101)
Let's say, current date is 8 AUG 2005 and current time is 2045 So, i will get ID | Date (this is datetime) | Time (this is integer) | Value -------------------------------------------------- 204 | 8/1/2005| 2359 | 90 205 | 8/1/2005| 2250 | 99 206 | 8/1/2005| 1950 | 88 ... ... 207 | 8/7/2005| 1845 | 77 208 | 8/7/2005| 2255 | 77 209 | 8/7/2005| 2140 | 77
Can someone can show me to filter data between t1.Date>=CONVERT(VARCHAR(10),DATEADD(d,-7,GETDATE()),101) AND TIME>=CurrentTime t1.Date<CONVERT(VARCHAR(10), GETDATE(), 101) AND TIME<=CurrentTime
If current date is 8 AUG 2005 and current time is 2045, so the result shown as follow
ID | Date (this is datetime) | Time (this is integer) | Value -------------------------------------------------- 204 | 8/1/2005| 2359 | 90 205 | 8/1/2005| 2250 | 99 ... ... 207 | 8/7/2005| 1845 | 77
I only have this query, SELECT t1.ID, t1.Date, t1.Time, t1.VALUE FROM RESULT WHERE t1.Date>=CONVERT(VARCHAR(10),DATEADD(d,-7,GETDATE()),101) AND t1.Date<CONVERT(VARCHAR(10), GETDATE(), 101)
I have recently installed SQL 2005 I use the SQL ecpress and management studio. In the SQL server Management Studio, when I right click on the database and use the SQL Server Import and Export wizard, I can import databases from the web hosting I'm workign with to my mycomputer, but cannot export them. When I try to export database tables with the SQL Server Import and Export wizard The error says: The current user is not an owner of the selected database. This might cause some operations with this database to fail.I use sa to log into the management studio aand creat teh database after I login. How do I create a database so that the user and owner match and will allow me to export tables and data?
I am making a web site in which i need to update a database with certain values which include the current user who invoked the insert command so how to retrieve the current userID
Hi, I want to write a sp with structure: PROCEDURE TEST @dbname1 varchar(25) @dbname2 varchar(25) AS 1. change current database to another, example for DB1 database 2. execute commands or block commands in DB1 3. change current database to another, example for DB2 database 4. execute commands or block commands in DB2 ... Can I do that? Who can help me? Thanks. qhbaby@hotmail.com
Hi! I've restored a backup from server A in server B. In server A I used to have a db_owner, called sitebase. After restoring the backup in server B, I created the user sitebase again and tried to grant him db_owner through Server Manager but I got the error: user or role already exists in the current database. How can I avoid this situation and grant db_owner to him? Thanks, Fábio
I have a SQL 2005 question for you. In the SQL server Management Studio. When I right click on the database and use the SQL Server Import and Export wizard, I can import databases but cannot export them.
The error says:
The current user is not an owner of the selected database. This might cause some operations with this database to fail.
I use sa to loginto the server Management Studio, does anyone know how to create a database where the user and owner are the smae so I can export?
In my application (infopath browser forms), First I get current user name thru System.Environment.UserName, after that, i want to check whether this user has access (read & write) permission to certain database, How do I do that? One way I tried before is to get all the users and stored the names in an array for comparing in my program, but can not figure out the SQL command (to get the database users), if there it is, please kindly tell me. Or if I can just pass the current user to check? Thanks in advance.
Hi all--I am writing a DTS package looking to back up all databases in sequence. I am calling CDOSYS in a stored procedure from support.microsoft.com to send email upon failure. I have two types of tasks I am writing into the DTS package:
1. backup database <database> to disk = '<drive>:<path>.bak' with init
select @srvname=srvname from master.dbo.sysservers print @srvname select @db_name=name from master.dbo.sysdatabases print @db_name set @msg='Hi--check your database backups. The ' + @db_name + ' database backup on ' + @srvname + ' has failed.
Thanks--SQL Server';
Upon success, go to the next database to back up, execute through these steps for the next database. Here's my problem: The @db_name variable captures the proper value for the current SQL Server, but I want to turn both @srvname and @db_name into global variables. I would like to pass in @db_name as the current database being backed up (and failing), and @srvname as the current SQL Server instance being backed up.
The above statement "select @db_name=name from master.dbo.sysdatabases" definitely does not capture the current database name value. Any suggestions as to how to pass this in, if not as a global variable?
I need an SQL string that inserts the current date into a database. So far I have tried: SQL = "INSERT INTO X (START_DATE) VALUES ('" & Date.Now & "')" mycomm = New SqlCommand(sql, myconn) mycomm.ExecuteNonQuery() However, there is a problem with the SQL command. The problem is related to the date. Is there a way of programatically inserting the current date/time into the SQL database? Language used is VB.
i have database and set default table schema to "ray" and me must input ruy.TABLE-NAME for retrive data !!! , i need set Default Schema to current user for call database as just database name(for my program) , how changed it ? (i change default schema for current user by alter command but not worked !)
Hi all--I am writing a DTS package looking to back up all databases in sequence from a specific instance of SQL Server. I am calling CDOSYS in a stored procedure from support.microsoft.com to send email upon failure. I have two types of SQL tasks I am writing into the DTS package:
1. backup database <database> to disk = '<drive>:<path>.bak' with init
2. Upon failure of a particular database to back up to file, execute the following SQL task:
SELECT @srvname = @@servername select @db_name = db_name() set @msg = 'Hi--check your database backups. The ' + @db_name + ' database backup on ' + @srvname + ' has failed. Thanks--SQL Server';
SELECT @srvname, @db_name, @msg
(Many thanks to Whitney Weaver and SQL_Menace for help with sections of this code)
Upon success of #1 and/or #2, go on to the next database, repeating cycle of the SQL tasks above for the next database in the task. For example, this logic goes like:
1. Back up master database; if fails, send email in SQL task; if either task is successful, goto database #2;
2. Back up model database; ...<etc>.
My problem is the current database set in @db_name. I apparently need to reset this variable for each database that gets backed up, and I can't seem to get these set as values to be passed from SQL Task #1 to SQL Task #2. Any suggestions on how I might achieve this?