Adding percentages to multiple counts in one SQL SELECT Query

I have a SELECT statement similar to the one below which returns several counts in one query. SELECT invalidCount = (SELECT COUNT(*) FROM <…a…> WHERE <…b…>), unknownCount = (SELECT COUNT(*) FROM <…c…> WHERE <…d…>), totalCount = (SELECT COUNT(*) FROM <…e…> WHERE <…f…>) This works fine but I wanted to add two percentage columns to the […]

Async Stored Procedure Call in T-SQL

How one can make an async call to a stored procedure from another one? Assume I have two stored procedures, SP1 and SP2 (this is a long running stored procedure, takes much time to execute, and doesn’t return any result). The stored procedure SP1 is defined like this: CREATE PROCEDURE SP1 AS BEGIN –custom business […]

Can we cancel query issued to sql server?

I have a complex piece of SQL and it involves lot of calculations etc. I want to know whether it is possible to cancel the query that is issued to the SQL server? Ex. There is a button named Search and user clicks on Search. I want to show a button named “Cancel” and that […]

SQL Query to group items by time, but only if near each other?

I am trying to craft a SQL statement to pull sample values from a DB. The table contains values that pertain to tool changes in a CNC machine. The current statement I have pulls values properly, but only if there is one occurrence of the tool in for a given program. If the tool appears […]

SQL Server Management Studio connection defaults to 'master' when selecting a database-specific object

In SQL Server 2008 R2 Management Studio, if I right-click on an object inside a specific database and choose “Select top 1000 rows ..”, the database connection for the query window always opens on ‘master’ while the table name is fully qualified as [database].[dbo].[table]. This makes it impossible to jump in and tweak out this […]

What are SSIS packages deployment and change-tracking best practices?

I’ve got quite a robust development environment regarding my SQL database Schema and Data (everything is source controlled, deployment is automated, etc), but when it comes to SSIS packages, the process is quite tedious and totally manual. How can I automate the SSIS package upgrade process (right now, I copy my packages manually to each […]

How To Get All children and itself from Hierarchical data with CTE In SQL Server 2005 using stored procedure

I have many similar structure tables like this: CREATE TABLE [dbo].[tbl_Hierarchy]( [ID] [int] NOT NULL, [ParentID] [int] NOT NULL, [Text] [nvarchar](100) NOT NULL, –other field irrelevant to my question ) INSERT INTO dbo.tbl_Hierarchy VALUES(1,0,’parent1′) INSERT INTO dbo.tbl_Hierarchy VALUES(2,0,’parent2′) INSERT INTO tbl_Hierarchy VALUES(3,1,’child1′) INSERT INTO tbl_Hierarchy VALUES(4,3,’grandchild1′) INSERT INTO tbl_Hierarchy VALUES(5,2,’child2′) Can you help me writing […]

Find out when a database backup was made

Folks, Assume you receive a disconnected backup of a SQL Server database (2005 or 2008) and you restore that to your SQL Server instance. Is there a way, is there a system catalog or something, to find out when the last write operation occured on that particular database? I’d like to be able to find […]

SQL – select distinct records in one field with highest records from another field

In a scenario where I have a table like so: int id (PK) int staff_id int skill_id bit mainskill I want to select only ONE record for each staff member (represented by staff_id) listing their main skill as represented by a (1) in mainskill. If no main skill is present, I want to return any […]

Query to find all matching rows of a substring

Here is the column containing my row entries for skills C,C++ P,H,D ASP,.net,C,C#,C++,R+ C++ I need to find all entries that contain C. So I formatted a query by using Skills LIKE (‘%’+@Skill+’%’) and this gives me all entries including C++ when I just want to get the result of C alone. Searching from the […]

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.