Friday, February 14, 2014

EXEC Stored Procedure on a row without cursor

DECLARE @StudentID INT = -1
WHILE (= 1) 
BEGIN
 SELECT TOP 1 @StudentID = StudentId
 FROM Students
 WHERE StudentId > @StudentID
 ORDER BY StudentId
 IF @@ROWCOUNT = 0
  BREAK;
 EXEC dbo.ProcedureName @StudentId
END

Saturday, February 8, 2014

How to see active SQL Server connections for database

Using simple select statements
SELECT 
    DB_NAME(dbid) as DBName, 
    COUNT(dbid) as NumberOfConnections,
    loginame as LoginName
FROM
    sys.sysprocesses
WHERE 
    dbid > 0 AND DB_NAME(dbid) LIkE '{DBNAME}'
GROUP BY 
    dbid, loginame
;
or using stored procedure
sp_who or sp_who