Calculate execution time of a SQL query?
Well, If you really want to do it in your DB there is a more accurate way as given in MSDN: SET STATISTICS TIME ON You can read this information from your application as well.
Well, If you really want to do it in your DB there is a more accurate way as given in MSDN: SET STATISTICS TIME ON You can read this information from your application as well.
How do you unit test your T-SQL? Which libraries/tools do you use? Have a look at this TSQLUnit and this tSQLt. I have experimented with using a few different frameworks in many languages to test T-SQL, such a junit or nunit. The reason I went this route was to take advantage of the rich testing … Read more
Simply call test2 from test1 like: EXEC test2 @newId, @prod, @desc; Make sure to get @id using SCOPE_IDENTITY(), which gets the last identity value inserted into an identity column in the same scope: SELECT @newId = SCOPE_IDENTITY()
You can try this DECLARE @Table TABLE( ID INT, ParentID INT, NAME VARCHAR(20) ) INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 1, NULL, ‘A’ INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 2, 1, ‘B-1’ INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 3, 1, ‘B-2’ INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 4, 2, ‘C-1’ INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 5, 2, ‘C-2’ DECLARE … Read more
Very close approximate (ignoring any in-flight transactions) would be: SELECT SUM(p.rows) FROM sys.partitions AS p INNER JOIN sys.tables AS t ON p.[object_id] = t.[object_id] INNER JOIN sys.schemas AS s ON s.[schema_id] = t.[schema_id] WHERE t.name = N’myTable’ AND s.name = N’dbo’ AND p.index_id IN (0,1); This will return much, much quicker than COUNT(*), and if … Read more
The most complete list I can find (from here) is as follows: The projected columns in the SELECT statement must be explicitly stated, and table names must be qualified with two-part names. Notice that this means that all tables referenced in the statement must be in the same database. The statement may not use the … Read more
;with cteRowNumber as ( select COMPUTERNAME, SERIALNUMBER, USERNAME, LASTIP, LASTUPDATE, SOURCE, row_number() over(partition by COMPUTERNAME order by LASTUPDATE desc) as RowNum from YourTable ) select COMPUTERNAME, SERIALNUMBER, USERNAME, LASTIP, LASTUPDATE, SOURCE from cteRowNumber where RowNum = 1
If the two databases are on the same server, you should be able to create a SQL statement something like this: UPDATE Test1.dbo.Employee SET DeptID = emp2.DeptID FROM Test2.dbo.Employee as ’emp2′ WHERE Test1.dbo.Employee.EmployeeID = emp2.EmployeeID From your post, I’m not quite clear whether you want to update Test1.dbo.Employee with the values from Test2.dbo.Employee (that’s what … Read more
Find all products that has been ordered 1 or more times… (kind of duplicate records) SELECT DISTINCT * from [order_items] where productid in (SELECT productid FROM [order_items] group by productid having COUNT(*)>0) order by productid To select the last inserted of those… SELECT DISTINCT productid, MAX(id) OVER (PARTITION BY productid) AS LastRowId from [order_items] where … Read more
From DBCC CHECKIDENT DBCC CHECKIDENT ( table_name, RESEED, new_reseed_value ) If no rows have been inserted to the table since it was created, or all rows have been removed by using the TRUNCATE TABLE statement, the first row inserted after you run DBCC CHECKIDENT uses new_reseed_value as the identity. Otherwise, the next row inserted uses … Read more