How to reproduce “Cannot drop database because it is currently in use”? - sql

Can anyone please let me know how can I reproduce the error “Cannot drop database because it is currently in use” ?
Thanks

Open up the database in SQL Server Management Studio and do say a select top 1000 rows on an arbitrary table and leave the window / connection open.

Example:
USE {yourDb}
It will select your database,then execute
DROP DATABASE {yourDb}
returns a message:
Cannot drop database "{yourDb}" because it is currently in use.

for example:
query database in sql server management studio and dont close query tab. Then try to drop t

Related

there a way to drop 'sysmessages' table from one database?

unfortunately i create table call 'sysmessages' in SQL Server 2008. when i restore the DB to SQL Server 2012 i realize that i have two Tables call 'sysmessages'.
i don't want to change my table name because it using in the code.
can i remove only from specific database system table?
it is not a table, but a view
of course you cannot remove it, but you don't need to. It is in a different schema. You will not address it like select * from sys.sysmessages, you will address it like select * from dbo.sysmessages
"i don't want to change my table name because it is used in the code" - you can/should change the code as well :)
edit - no. 2. is not applicable in SQL 2012, however it is tested and working in SQL 2008R2
You cant drop system tables,your best bet is to change your code

Copying an SQL table from one Server to another on SQL Server 2000 / 2005

I’m trying to copy a SQL Server table, schema and data, from Server A to Server B. The SQL Server table is just a reference table which hasn't been populated for some reason on Server B. Can anyone advise how the entire table could be copied across please? On SQL Server 2000/2005.
So far we've tried a long-winded approach by copying the .mdf and .ldf files from Server A to Server B with a plan to then copy the table across into the Server B database but we are having some difficulty re-attaching the database to Server B.
Please can anyone help?
Kind Regards
James
Using SQL Server Management Studio (SSMS):
In Object Explorer right click on source database name, Tasks.. -> Generate Scripts.. - opens Generate and Publish Scripts dialog. Click Next to choose objects, choose "Select specific DB objects", expand Tables, choose your table. Next, setup script destination, for example New query window and (important step!!) - click Advanced, and set "Types of data to script"="Schema and data" and "Script USE DATABASE"=False, OK, Next, Next, .. wait .. Finish. Now you have got complete SQL script to reproduce this table with data. Connect to destination DB and run it.
Tested with SSMS 2014, but as I recall this feature should be available starting from SSMS 2005.
you can use the import/export data wizard in management studio, the wizard will create for you a new table in the server B with the same structure of the table in the server A. before using it you need to have at least one database in sever B.
This confirms why this is one of favourite forums.
Both these methods work beautifully :
Generate Scripts (when altering Types of data to script"="Schema and
data")
Export and Import
Interestingly Generate Scripts works with SQL Express perfectly but the Export method does not save unless you have at least SQL Server Standard Edition.
Thanks so much everyone
Cheers
James
Try this:
SELECT * INTO destination FROM source
But, it will not copy the indexes and key information or you can also try import/export data task from SSMS.

How to generate "Create Table" script, when I only have read only permission?

I have read only permission on a SQL Server 2005 database, and I'm looking to get a table schema locally to work with. With my current access it won't let me right click on the table and choose 'Create Table' to get the script for this.
Is there a way to generate the create table script from a select statement or by some other mechanism?
Thank you!
You could create a linked server on your local machine. Then, you can use select ... into to copy the table, including data:
select *
into NewTableName
from [LinkedServerName].[DatabaseName].dbo.TableName
This will not copy indexes or constraints. To exclude the data, use select top 0 *.
It's a bit long but the script in Script Table Definitions using TSQL could be addapted to work without needing to create the stored procedure mentioned.
I am assuming you are working with SQL Server 2005

timeout on query against small table

I'm trying to do a query that gets all the ids from a table where the column document(varbinary(max)) is null.
The query always timesout, and I'm running it against a 5000-row table.
select ID from Invoice where Document is null
Im using SQL Express 2008 R2 and Sql Management Studio. Is this the right way? Am I missing something? Even if I add top 1 the query time out
Sometimes the tables gets locked. Try clicking away from the selected Table in your interface.
What RDBMS are you using? SQLserver? Sybase? or..
Does it still time out when you do:?
SELECT TOP 10 ID
from Invoice where Document is null
Do it this way, for a read-only look
select ID from Invoice (nolock) where Document is null
If connecting on localhost Then
If connecting from Management Studio Then
Try restarting SQL Server services...could be locks.
Try restarting machine...could be locks.
ElseIf connecting from remote app code Then
Check if SQL Server is setup for remote connections.
Check connection strings.
Check seccurity privleges.
Check log file.
End If
Else
Check if SQL Server is setup for remote connections
Check connection strings.
Check seccurity privledges.
End If

Documentation of a table of database

Can anyone suggest me the right way to begin with the documentation of the table of database I am working on. What actually I need to show to the evaluator?
Open SQL Server Management studio.
Then paste your table name, select the table name and then press ALT+F1. You will get all the details of the table as shown below in the image.