Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored procedure - Insert Table

Status
Not open for further replies.

ramsfield

MIS
Jun 30, 2004
28
US
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
 
INSERT INTO [Tidemark].[dbo].[logged_in_users]
( [Login_Name],[DBName], [LoginTime],[Lastbatch])
SELECT mp.loginame, ms.name,mp.login_time,mp.last_batch
FROM master.dbo.sysprocesses mp INNER JOIN
master.dbo.sysdatabases ms ON mp.dbid = ms.dbid
WHERE (ms.name = N'Tidemark')

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top