sql-server-2005

RAISERROR―How to distinguish with SqlException?

馋奶兔 提交于 2019-12-30 02:20:10
问题 I have some 3-4 stored procedures ― which I can modify if needed ― that use RAISERROR to inform my application of some fatal errors on the database side. Some of these stored procedures are executed from the C# side with ExecuteNonQuery , while other are executed with ExecuteReader . At the moment, I am wrapping these command in a try { ... } catch (SqlException ThisSqlException) { ... } block, but the problem is that this exception will be thrown in at least two scenarios I must deal with

How to deliberately cause a deadlock?

时光总嘲笑我的痴心妄想 提交于 2019-12-30 02:02:31
问题 So I'm trying to track down what looks like a deadlock problem here. I've enabled deadlock logging using DBCC TRACEON(1222,-1) and DBCC TRACEON(1204 ,-1). I'd like to test to make sure the logging catches the deadlock, so how can I cause one to occur in MS SQL 2005? Thanks, 回答1: Here's some T-SQL to deliberately cause a deadlock. Object creation: CREATE TABLE dbo.DeadLockTest (col1 INT) INSERT dbo.DeadLockTest SELECT 1 CREATE TABLE dbo.DeadLockTest2 (col1 INT) INSERT dbo.DeadLockTest2 SELECT

When restoring a backup, how do I disconnect all active connections?

谁说我不能喝 提交于 2019-12-29 10:07:18
问题 My SQL Server 2005 doesn't restore a backup because of active connections. How can I force it? 回答1: SQL Server Management Studio 2005 When you right click on a database and click Tasks and then click Detach Database , it brings up a dialog with the active connections. By clicking on the hyperlink under "Messages" you can kill the active connections. You can then kill those connections without detaching the database. More information here. SQL Server Management Studio 2008 The interface has

How to use PIVOT in SQL Server 2005 Stored Procedure Joining Two Views

十年热恋 提交于 2019-12-29 09:07:07
问题 Good Morning, I have 2 views: ICCUDays which contains one record per account with fields ACCOUNT and ICCUDays, ICCUEnctrSelectedRevCatsDirCost which contains multiple records per account with fields ACCOUNT, UBCATEGORY, and DirectCost. My Goal: To create a stored procedure that outputs one record per ACCOUNT with ICCUDays and DirectCost by UBCATEGORY. This will be a crosstab or pivot and has to allow for the possibility of nulls in one or more direct cost ubcategory bucket. Finally, this

SqlException: System.Data.SqlClient.SqlException (0x80131904)

被刻印的时光 ゝ 提交于 2019-12-29 09:06:44
问题 I wrote a code in C#, which works great at my computer, with Windows 7 (MS SQL Server 2008) but not at the other with Windows Vista (MS SQL Server 2005). I can not change system on the second computer ;) I'm using Visual Studio 2010. So this is the part of code, from my class "obSQL": private SqlConnection connection; public obSQL(string user, string pass, string instance, string dbdir) //sql server authentication { connection = new SqlConnection(); connection.ConnectionString = "user id=" +

Ignore XML namespace in T-SQL

拥有回忆 提交于 2019-12-29 08:32:29
问题 How do I remove/ignore the XML namespace in an xml file when querying the data with T-SQL? I’m loading an xml file into a variable, and it works just fine. But the xml has a namespace set, and unless I remove it, my queries come up empty. T-SQL: DECLARE @xml xml SELECT @xml = BulkColumn FROM OPENROWSET(BULK 'C:\myfile.xml', SINGLE_BLOB) AS A SELECT X.z.value('ID[1]', 'VARCHAR(3)') FROM @xml.nodes('myroot/element') AS X(z) XML sample: <?xml version="1.0" encoding="utf-8"?> <myroot xmlns:xsi=

What is the best way to refresh a rollup table under load?

廉价感情. 提交于 2019-12-29 08:07:13
问题 I created a table in my SQL Server 2005 database and populated it with summary and calculated values. The purpose is to avoid extensive joins and groupings on every call to the database. I would like this table to refresh every hour, but I am not sure the best way to do this while the website is under load. If I delete every record and repopulate the table in one transaction will that do the trick or will there be deadlocks and other trouble lurking? 回答1: The way I have done this in a few

SQL Server 2005 Transaction Level and Stored Procedures

拜拜、爱过 提交于 2019-12-29 07:19:10
问题 If I use the command SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED and then execute a stored procedure using the EXEC storedProcedureName on the same context, will the stored procedure use the transaction level stated previously or will use a default one? If I want to force every stored procedure to use on transaction level do I have to include at the top of the code the same statement ( SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED )? PS.: the system is built on top of .NET 2.0 and

How to copy a table schema and constraints to a table of different database? [closed]

◇◆丶佛笑我妖孽 提交于 2019-12-29 05:00:30
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . What SQL can be used to copy the schema of a specified table to a table in different database? 回答1: SELECT INTO will create a new table with the same schema. So you could: SELECT * INTO newdb.dbo.newtable FROM olddb.dbo.oldtable To just copy the schema and not the data: SELECT TOP 0 * INTO newdb.dbo.newtable

Move row from one table to another?

时光毁灭记忆、已成空白 提交于 2019-12-29 04:36:10
问题 I have two tables with the same column definitions. I need to move (not copy) a row from one table to another. Before I go off and use INSERT INTO/DELETE (in a transaction), is there a smarter way? SQL Server 2005 回答1: for SQL Server 2005 and up, try the OUTPUT Clause (Transact-SQL) clause: DELETE OldTable OUTPUT DELETED.col1, DELETED.col2... INTO NewTable WHERE ID=... Working example: DECLARE @OldTable table(col1 int, col2 varchar(5), col3 char(5), col4 datetime) DECLARE @NewTable table(col1