I have the following stored procedure. I would like it to take all the results and insert them into a table. The current script only inserts one result. How can I make it return and insert all the results? If I just run the select statement, without the variables, I receive about 30 rows.
CREATE PROCEDURE [sp_logged_in_users]
AS
DECLARE @login_name2 [varchar] (10)
DECLARE @database_name2 [varchar] (15)
DECLARE @login_time2 [datetime]
DECLARE @last_batch2 [datetime]
BEGIN
SELECT @login_name2 = master.dbo.sysprocesses.loginame,
@database_name2 = master.dbo.sysdatabases.name,
@login_time2 = master.dbo.sysprocesses.login_time,
@last_batch2 = master.dbo.sysprocesses.last_batch
FROM master.dbo.sysprocesses INNER JOIN
master.dbo.sysdatabases ON master.dbo.sysprocesses.dbid = master.dbo.sysdatabases.dbid
WHERE (master.dbo.sysdatabases.name = N'Tidemark')
INSERT INTO [Tidemark].[dbo].[logged_in_users]
( [Login_Name],
[DBName],
[LoginTime],
[Lastbatch])
VALUES
( @login_name2,
@database_name2,
@login_time2,
@last_batch2)
END
GO
CREATE PROCEDURE [sp_logged_in_users]
AS
DECLARE @login_name2 [varchar] (10)
DECLARE @database_name2 [varchar] (15)
DECLARE @login_time2 [datetime]
DECLARE @last_batch2 [datetime]
BEGIN
SELECT @login_name2 = master.dbo.sysprocesses.loginame,
@database_name2 = master.dbo.sysdatabases.name,
@login_time2 = master.dbo.sysprocesses.login_time,
@last_batch2 = master.dbo.sysprocesses.last_batch
FROM master.dbo.sysprocesses INNER JOIN
master.dbo.sysdatabases ON master.dbo.sysprocesses.dbid = master.dbo.sysdatabases.dbid
WHERE (master.dbo.sysdatabases.name = N'Tidemark')
INSERT INTO [Tidemark].[dbo].[logged_in_users]
( [Login_Name],
[DBName],
[LoginTime],
[Lastbatch])
VALUES
( @login_name2,
@database_name2,
@login_time2,
@last_batch2)
END
GO