sql-server-2008-r2

SQL Server - get all databases with MDF and LDF File Location

我的未来我决定 提交于 2019-11-28 22:51:41
问题 I need a T-SQL query for a list of all databases in SQL Server 2008 showing the name of the database and the location of the .mdf and .ldf files. 回答1: SELECT db.name AS DBName, type_desc AS FileType, Physical_Name AS Location FROM sys.master_files mf INNER JOIN sys.databases db ON db.database_id = mf.database_id 回答2: select d.name as 'database', mdf.physical_name as 'mdf_file', ldf.physical_name as 'log_file' from sys.databases d inner join sys.master_files mdf on d.database_id = mdf.database

Fastest way to find string by substring in SQL?

≯℡__Kan透↙ 提交于 2019-11-28 21:15:30
I have huge table with 2 columns: Id and Title. Id is bigint and I'm free to choose type of Title column: varchar, char, text, whatever. Column Title contains random text strings like "abcdefg", "q", "allyourbasebelongtous" with maximum of 255 chars. My task is to get strings by given substring. Substrings also have random length and can be start, middle or end of strings. The most obvious way to perform it: SELECT * FROM t LIKE '%abc%' I don't care about INSERT, I need only to do fast selects. What can I do to perform search as fast as possible? I use MS SQL Server 2008 R2, full text search

SQL Server Find What Jobs Are Running a Procedure

久未见 提交于 2019-11-28 17:48:17
Is there a way to find out what jobs are using a certain stored procedure? This will capture instances where the procedure is explicitly referenced in the job step: SELECT j.name FROM msdb.dbo.sysjobs AS j WHERE EXISTS ( SELECT 1 FROM msdb.dbo.sysjobsteps AS s WHERE s.job_id = j.job_id AND s.command LIKE '%procedurename%' ); If it is called by something else that is called from the job, or the command is constructed with dynamic SQL, this might be a little more difficult to track down. Note also that if your procedure name can also appear naturally in other code, comments, etc. that it may

fastest way to export blobs from table into individual files

我们两清 提交于 2019-11-28 17:30:37
What is the fastest way to export files (blobs) stored in a SQL Server table into a file on the hard drive? I have over 2.5 TB of files (90 kb avg) stored as varbinary and I need to extract each one to a local hard drive as quickly as possible. BCP seems to work but it will take over 45 days with the speed I'm seeing, and I'm worried that my script will fail at some point because Management Studio will run out of memory. I tried using a CLR function and it was more than twice as fast as BCP. Here's my code. Original Method : SET @bcpCommand = 'bcp "SELECT blobcolumn FROM blobtable WHERE ID = '

SQL Server stops loading assembly

梦想的初衷 提交于 2019-11-28 17:11:49
We have developed an assembly for SQL Server 2008 R2. The assembly has been working for a week. The managed stored proc inside the assembly was working fine for the whole week and then it stops working. We have been seeing this problem couple times. The way to make it work again is to restart the SQL Server. Msg 10314, Level 16, State 11, Line 4 An error occurred in the Microsoft .NET Framework while trying to load assembly id 65536. The server may be running out of resources, or the assembly may not be trusted with PERMISSION_SET = EXTERNAL_ACCESS or UNSAFE. Run the query again, or check

Check if table exists and if it doesn't exist, create it in SQL Server 2008

╄→гoц情女王★ 提交于 2019-11-28 16:16:21
I am writing a Stored procedure in SQL Server 2008. I need to check if a table exists in the database. If it doesn't then I need to create it. How do I do this? Something like this IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[YourTable]') AND type in (N'U')) BEGIN CREATE TABLE [dbo].[YourTable]( .... .... .... ) END Just for contrast, I like using the object_id function as shown below. It's a bit easier to read, and you don't have to worry about sys.objects vs. sysobjects vs. sys.all_objects vs. sys.tables. Basic form: IF object_id('MyTable') is not null PRINT

Getting error while running 50 MB script on SQL Server 2008 R2

℡╲_俬逩灬. 提交于 2019-11-28 15:56:56
问题 I am using SQL Server 2008 R2, I have a script to update the DB, that script is approx 50 MB in size and contains some about 800,000 lines. Error: TITLE: Microsoft SQL Server Management Studio Cannot execute script. ADDITIONAL INFORMATION: Insufficient memory to continue the execution of the program. (mscorlib) Can somebody please help me to run this script without getting this error? 回答1: use the command-line tool SQLCMD which is much leaner on memory. It is as simple as: SQLCMD -d <database

How to solve SQL Server Error 1222 i.e Unlock a SQL Server table

主宰稳场 提交于 2019-11-28 15:25:02
I am working in a database where I load data in a raw table by a data loader. But today the data loader got stuck for unknown reasons. Then I stopped the data loader from windows task manager. But then I again tried to load data in the raw table but found its locked and I can't do any operation on it. I tried restarting SQL Server service but it was not resolved. And I have no permission to kill processes on this server. Below is the message showed by SQL Server. An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo) Program Location: at

Exit single-user mode

倾然丶 夕夏残阳落幕 提交于 2019-11-28 15:10:20
Currently, my database is in Single User mode. When I try to expand me database, I get an error: The database 'my_db' is not accessible.(ObjectExplorer) Also, when I try to delete the database, I get the error: Changes to the state or options of database 'my_db' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it. How do I exit out of single-user mode? I don't have any user using this database. When I try to browse my site with IIS, the error I get is: An unhandled exception was generated during the execution of the current web request.

Select last records from table using group by [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-28 14:50:56
This question already has an answer here: Retrieving last record in each group from database - SQL Server 2005/2008 2 answers I need to select last records from my table using group by UserID I need to select StartDate,EndDate from last records grouping by userID I am using code below select StartDate,EndDate from usersworktime group by userid,StartDate,EndDate The problem is that i am getting all rows instead only the last two of my table Assuming that the start and end dates will always be the highest values then you need to drop some of the columns from the GROUP BY (having all the columns