sql-server-2005

SQL Server: preventing dirty reads in a stored procedure

喜欢而已 提交于 2020-01-24 18:54:09
问题 Consider a SQL Server database and its two stored procs: *1. A proc that performs 3 important things in a transaction: Create a customer, call a sproc to perform another insert, and conditionally insert a third record with the new identity. BEGIN TRAN INSERT INTO Customer(CustName) (@CustomerName) SELECT @NewID = SCOPE_IDENTITY() EXEC CreateNewCustomerAccount @NewID, @CustomerPhoneNumber IF @InvoiceTotal > 100000 INSERT INTO PreferredCust(InvoiceTotal, CustID) VALUES (@InvoiceTotal, @NewID)

How to display roundof time

余生长醉 提交于 2020-01-24 18:12:57
问题 Using SQL Server 2005 Table1 ID Intime Outtime 001 00.21.00 00.48.00 002 08.23.00 13.45.00 003 00.34.00 00.18.00 I need to display the time time like 30 minutes or 1 Hours, it should display a roundoff time Expected Output ID Intime Outtime 001 00.30.00 01.00.00 002 08.30.00 14.00.00 003 01.00.00 00.30.00 How to make a query for the roundoff time. 回答1: You can round the current date to 30 minutes like: select dateadd(mi, datediff(mi,0,getdate())/30*30, 0) Explanation: this takes the number of

Is there a way to pivot a customer ID and a their most recent order dates?

北慕城南 提交于 2020-01-24 13:12:11
问题 I have a query that gives me all customer's and their last three order dates. EX: CustomerId DateOrdered 167 2006-09-16 01:25:38.060 167 2006-09-21 13:11:53.530 171 2006-08-31 15:19:22.543 171 2006-09-01 13:30:54.013 171 2006-09-01 13:34:36.483 178 2006-09-04 11:36:19.983 186 2006-09-05 12:50:27.153 186 2006-09-05 12:51:08.513 I want to know if there is a way for me to pivot it to display like this: [CustomerId] [Most Recent] [Middle] [Oldest] '167' '2006-09-21 13:11:53.530' '2006-09-16 01:25

Is there a way to pivot a customer ID and a their most recent order dates?

非 Y 不嫁゛ 提交于 2020-01-24 13:12:09
问题 I have a query that gives me all customer's and their last three order dates. EX: CustomerId DateOrdered 167 2006-09-16 01:25:38.060 167 2006-09-21 13:11:53.530 171 2006-08-31 15:19:22.543 171 2006-09-01 13:30:54.013 171 2006-09-01 13:34:36.483 178 2006-09-04 11:36:19.983 186 2006-09-05 12:50:27.153 186 2006-09-05 12:51:08.513 I want to know if there is a way for me to pivot it to display like this: [CustomerId] [Most Recent] [Middle] [Oldest] '167' '2006-09-21 13:11:53.530' '2006-09-16 01:25

Unique Index Error when creating non-unique index - SQL Server

故事扮演 提交于 2020-01-23 17:35:50
问题 I trying to and a non-unique index to a table table in SQL Server 2005. I am getting the following error when I try to create it. Msg 1505, Level 16, State 1, Line 1 The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.oe_pick_ticket' and the index name 'idx_pick_ticket_popup_wmms'. The duplicate key value is (1093066, N, N, N, , FBF, 100001, 1074359, 1118930). My create statement is as follows: CREATE NONCLUSTERED INDEX idx_pick_ticket_popup

SQL order by and left outer join doesn't have correct order

社会主义新天地 提交于 2020-01-23 13:04:18
问题 I have a view that is joining two tables and ordering by the first table. Except that the order isn't correct. It misses an occasional record, and then at the end, most of those records exist in order, and then at that end, the rest of the records exist in order. So it has records such as 1 (most of the records in order) 2 4 5 6 7 8 10 11 13 15 3 (the first set of missing records) 12 9 (the rest of the missing records) 14 My view is below. Do I need to do the order by before I do the join?

SQL Server 2005 stored procedure performance problem

佐手、 提交于 2020-01-23 09:30:45
问题 I have the following issue: when a stored proc is called from my application, every now and then (like 1 time out of 1000 calls), it takes 10-30 seconds to finish. Typically, the sproc runs in under a second. It's a fairly simply proc with a single select that ties together a couple of tables. All the table names are set with a (NOLOCK) hint, so it probably isn't locking. The indexes are all in place too, otherwise it would be slow all the time. The problem is that I can't replicate this

Convert datetime to nvarchar but keep format

笑着哭i 提交于 2020-01-22 17:49:45
问题 I am returning either a DATETIME or the NVARCHAR = 'MULTIPLE' depending on whether or not an action has been performed more than one time. So I am trying to store the DATETIME in its normal format '2012-10-23 13:59:47.000' but as an NVARCHAR. SQL wants to make it 'Oct 23 2012 12:40PM' How can I do this? Right now I am doing: CAST(r.Date_And_Time) AS NVARCHAR(30)) 回答1: Declare @CreatedDate datetime Select @CreatedDate='20121210' Select CONVERT(VARCHAR,@createdDate, 21) 回答2: Use CONVERT . It

Convert datetime to nvarchar but keep format

狂风中的少年 提交于 2020-01-22 17:48:05
问题 I am returning either a DATETIME or the NVARCHAR = 'MULTIPLE' depending on whether or not an action has been performed more than one time. So I am trying to store the DATETIME in its normal format '2012-10-23 13:59:47.000' but as an NVARCHAR. SQL wants to make it 'Oct 23 2012 12:40PM' How can I do this? Right now I am doing: CAST(r.Date_And_Time) AS NVARCHAR(30)) 回答1: Declare @CreatedDate datetime Select @CreatedDate='20121210' Select CONVERT(VARCHAR,@createdDate, 21) 回答2: Use CONVERT . It

Exit and rollback everything in script on error

与世无争的帅哥 提交于 2020-01-22 14:17:07
问题 I have a TSQL script that does a lot of database structure adjustments but it's not really safe to just let it go through when something fails. to make things clear: using MS SQL 2005 it's NOT a stored procedure, just a script file (.sql) what I have is something in the following order BEGIN TRANSACTION ALTER Stuff GO CREATE New Stuff GO DROP Old Stuff GO IF @@ERROR != 0 BEGIN PRINT 'Errors Found ... Rolling back' ROLLBACK TRANSACTION RETURN END ELSE PRINT 'No Errors ... Committing changes'