sql-server-2008

How to delete large data of table in SQL without log?

纵然是瞬间 提交于 2021-01-29 11:43:32
问题 I have a large data table. There are 10 million records in this table. What is the best way for this query Delete LargeTable where readTime < dateadd(MONTH,-7,GETDATE()) 回答1: If you are Deleting All the rows in that table the simplest option is to Truncate table, something like TRUNCATE TABLE LargeTable GO Truncate table will simply empty the table, you cannot use WHERE clause to limit the rows being deleted and no triggers will be fired. On the other hand if you are deleting more than 80-90

how to show which user connect to Database X in sql-server 2008?

荒凉一梦 提交于 2021-01-29 10:16:22
问题 How can I show which user connect to Database X in sql-server 2008 ? I have many databases in my sql-server 2008 and I need to show only the users that connect and works on database X Thanks in advance 回答1: Look at the procedure sp_who - http://msdn.microsoft.com/en-us/library/ms174313.aspx 回答2: You can also use Ctrl+Alt+A to open recourse monitor and see which user is connected to your Database X 回答3: @gabriel-adams provides a nice script over on this similar question. If you're like me and

How to split a comma-separated value to columns

末鹿安然 提交于 2021-01-29 09:24:34
问题 I have a table like this Value String ------------------- 1 Cleo, Smith I want to separate the comma delimited string into two columns Value Name Surname ------------------- 1 Cleo Smith I need only two fixed extra columns 回答1: CREATE FUNCTION [dbo].[fn_split_string_to_column] ( @string NVARCHAR(MAX), @delimiter CHAR(1) ) RETURNS @out_put TABLE ( [column_id] INT IDENTITY(1, 1) NOT NULL, [value] NVARCHAR(MAX) ) AS BEGIN DECLARE @value NVARCHAR(MAX), @pos INT = 0, @len INT = 0 SET @string =

Efficiently convert rows to columns in sql server

若如初见. 提交于 2021-01-29 07:35:53
问题 I'm looking for an efficient way to convert rows to columns in SQL server, I heard that PIVOT is not very fast, and I need to deal with lot of records. This is my example: ------------------------------- | Id | Value | ColumnName | ------------------------------- | 1 | John | FirstName | | 2 | 2.4 | Amount | | 3 | ZH1E4A | PostalCode | | 4 | Fork | LastName | | 5 | 857685 | AccountNumber | ------------------------------- This is my result: -----------------------------------------------------

SQL agent job failed with an error 0x80131904

☆樱花仙子☆ 提交于 2021-01-29 07:23:50
问题 I have one SQL agent job that executing the SSIS package. When the job is running through schedule then it is giving an error: Failed to execute IS server package because of error 0x80131904. Server: XXXXX Package path: "XXXX" Environment reference Id: 2. Description: The operation failed because the execution timed out. But when I manually running the job by right clicking on it then the job runs successfully. Could anyone help me on this to find out the root cause and solution of the issue.

Milliseconds from DateTime in SQL Server and C# are not the same

落花浮王杯 提交于 2021-01-29 06:57:59
问题 I'm getting dates from a database in SQL Server. When I get the DateTime from the DB and I compare the milliseconds in C# and SQL Server. I see that they are not the same. Why is that? 回答1: SQL Server's datetime data type is not accurate at the milliseconds level. Official documentation provides a list of properties of the data time data type. In that list, you will find the following row: Accuracy Rounded to increments of .000, .003, or .007 seconds You will also find, in that same page, the

How to make insert after insert and combine data with result ID's from first insert

爱⌒轻易说出口 提交于 2021-01-29 06:14:52
问题 DECLARE @tmpTable TABLE (UserId INT, Name nvarchar(50), Department nvarchar(50)) DECLARE @xml XML= N'<user><userId>1</userId><name>John</name><department>A</department></user> <user><userId>2</userId><name>Jane</name><department>B</department></user>'; insert into @tmpTable SELECT a.b.value('(./userId)[1]', 'int') AS UserId, a.b.value('(./name)[1]', 'nvarchar(50)') AS Name, a.b.value('(./department)[1]', 'nvarchar(50)') AS Department FROM @xml.nodes('/user') a(b) INSERT INTO members (name)

Updating Identity Column of a table with consecutive numbers through SQL Stored Procedure

别等时光非礼了梦想. 提交于 2021-01-29 02:07:13
问题 After deleting the duplicate records from the table, I want to update Identity column of a table with consecutive numbering starting with 1. Here is my table details id(identity(1,1)), EmployeeID(int), Punch_Time(datetime), Deviceid(int) I need to perform this action through a stored procedure. When i tried following statement in stored procedure DECLARE @myVar int SET @myVar = 0 set identity_insert TempTrans_Raw# ON UPDATE TempTrans_Raw# SET @myvar = Id = @myVar + 1 set identity_insert

Updating Identity Column of a table with consecutive numbers through SQL Stored Procedure

依然范特西╮ 提交于 2021-01-29 02:05:36
问题 After deleting the duplicate records from the table, I want to update Identity column of a table with consecutive numbering starting with 1. Here is my table details id(identity(1,1)), EmployeeID(int), Punch_Time(datetime), Deviceid(int) I need to perform this action through a stored procedure. When i tried following statement in stored procedure DECLARE @myVar int SET @myVar = 0 set identity_insert TempTrans_Raw# ON UPDATE TempTrans_Raw# SET @myvar = Id = @myVar + 1 set identity_insert

Get rows that contain only certain characters

我是研究僧i 提交于 2021-01-29 01:56:40
问题 I want to get only those rows that contain ONLY certain characters in a column. Let's say the column name is DATA. I want to get all rows where in DATA are ONLY (must have all three conditions!): Numeric characters ( 1 2 3 4 5 6 7 8 9 0 ) Dash ( - ) Comma ( , ) For instance: Value "10,20,20-30,30" IS OK Value "10,20A,20-30,30Z" IS NOT OK Value "30" IS NOT OK Value "AAAA" IS NOT OK Value "30-" IS NOT OK Value "30," IS NOT OK Value "-," IS NOT OK 回答1: Try patindex: select * from( select '10,20